ostream_iterator<string>(cout," ")

This is a discussion on ostream_iterator<string>(cout," ") within the C++ Programming forums, part of the General Programming Boards category; Code: copy(sentence.begin(),sentence.end(), ostream_iterator<string>(cout," ")); cout << endl; error 26 C:\Dev-Cpp\vector1\main.cpp `ostream_iterator' undeclared (first use this function) I took this example ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    450

    ostream_iterator<string>(cout," ")

    Code:
    copy(sentence.begin(),sentence.end(),
           ostream_iterator<string>(cout," "));
      cout << endl;
    error

    26 C:\Dev-Cpp\vector1\main.cpp
    `ostream_iterator' undeclared (first use this function)

    I took this example directly from a book and do not know why this line gives me problems. Can anyone help?

    I'm using Dev_Cpp ver.4.9.8

    From the error code I guess this iterator is not "included"
    Here is the entire example:

    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    #include <algorithm>
    #include <stdlib.h>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
      //create an empty vector for strings
      vector<string> sentence;
      
      //reserve memory for 5 elements to avoid reallocation
      sentence.reserve(5);
      
      //append some elements
      sentence.push_back("Hello, ");
      sentence.push_back("how");
      sentence.push_back("are");
      sentence.push_back("you");
      sentence.push_back("?");
      
      //print elements seperated with spaces
      copy(sentence.begin(),sentence.end(),
           ostream_iterator<string>(cout," "));
      cout << endl;
      
      //print statistics
      cout << "   max_size(): "<<sentence.max_size()<<endl;
      cout << "   size():     "<<sentence.size()<<endl;
      cout << "   capacity(): "<<sentence.capacity()<<endl;
      
      //swap second and fourth element
      swap(sentence[1],sentence[3]);
      
      //insert element "always" before element "?"
      sentence.insert(find(sentence.begin(),sentence.end(),"?"),"always");
      
      //assign "!" to the last element
      sentence.back()="!";
      
      //print elements seperated by spaces
      copy(sentence.begin(),sentence.end(),
           ostream_iterator<string>(cout," "));
      cout << endl;
      
      //print statistics
      cout << "   max_size(): "<<sentence.max_size()<<endl;
      cout << "   size():     "<<sentence.size()<<endl;
      cout << "   capacity(): "<<sentence.capacity()<<endl;
      
      system("PAUSE");	
      return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148

    Re: ostream_iterator<string>(cout," ")

    Originally posted by curlious

    From the error code I guess this iterator is not "included"
    Jepp,include <iterator>

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    BTW thanks I included <iterator> and that fixed the problem strange, the example in The C++ Standard Library a tutorial and refrence by Nicholi M. Josuttis (6.2.5 pg156) doesn't include the library. Guess it's an error on their part.

  4. #4
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    I think not! I never had to include iterator myself. And I use Dev too.
    Nothing more to tell about me...
    Happy day =)

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Maybe you use a newer version. It was premature to blame it on an error in the book w/o more feed back from those who know more about the compiler.

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    148
    Originally posted by curlious
    Maybe you use a newer version. It was premature to blame it on an error in the book w/o more feed back from those who know more about the compiler.
    The 100% correct way is to include iterator.
    Dinkumware C++ Reference http://www.dinkumware.com/manuals/re...=iterator.html (click on the logo).

    In your code you include string,iostream,vector and algorithm,but none of these must include iterator.
    It depends heavily on your implementation.

    Another example (Mingw,gcc 3.2)
    Code:
    #include <fstream>
    
    int main() {
    	std::string str = "Hello World";
    }
    Compiles fine,but correct?

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21