set<int> answer;
.....
ofstream out ( "OUTPUT.TXT" );
out << answer.size() << endl;
for( set<int>::iterator it = answer.begin(); it != answer.end(); ++it )
out << *it << endl;
//copy( answer.begin(), answer.end(), ostream_iterator<int>( out, "\n" ) );
//if i use copy() insread of the for loop it doesn't work :(
copy doesn't work
why the output i get is different when i use copy()? It's like copy() doesn't write to the file anything :(
what's the problem? thx
This might sound stupid but... Are you flushing the stream after you write to it?
copy( answer.begin(), answer.end(), ostream_iterator<int>( out, "\n" ) );
out.flush();
I suspect if you do that you will see something in the file.
copy( answer.begin(), answer.end(), ostream_iterator<int>( out, "\n" ) );
out.flush();
I suspect if you do that you will see something in the file.
i made the modification and it still doesn' work :(
It should be flushed when the object out gets out of scope anyways...
?
It should be flushed when the object out gets out of scope anyways...
?
It should be.
I don't know, it might have to do with some other part of the code... I just made this simple test program here:
Works perfectly fine, and seems to be equivalent to what you posted.
I don't know, it might have to do with some other part of the code... I just made this simple test program here:
#include <iostream>#include <set>#include <time.h>using namespace std;void main() { srand((unsigned int) time(NULL)); set<int> answer; set<int>::iterator iter; for (int i = 0; i < 10; i++) { answer.insert(rand() % 255); } cout << "For loop:" << endl; for (iter = answer.begin(); iter != answer.end(); iter++) { cout << (*iter) << endl; } cout << endl << endl; cout << "copy():" << endl; copy(answer.begin(), answer.end(), ostream_iterator<int>(cout, "\n"));}
Works perfectly fine, and seems to be equivalent to what you posted.
Why not try:
copy( answer.begin(), answer.end(), ostream_iterator<int>( cout, "\n" ) );
And see what it spits out onto the console?
copy( answer.begin(), answer.end(), ostream_iterator<int>( cout, "\n" ) );
And see what it spits out onto the console?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement