Jump to content
  • Advertisement
Sign in to follow this  
valla2

copy doesn't work

This topic is 4891 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

why the output i get is different when i use copy()? It's like copy() doesn't write to the file anything :(
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 :(



what's the problem? thx

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
i made the modification and it still doesn' work :(

It should be flushed when the object out gets out of scope anyways...

?

Share this post


Link to post
Share on other sites
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:


#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.

Share this post


Link to post
Share on other sites
Why not try:

copy( answer.begin(), answer.end(), ostream_iterator<int>( cout, "\n" ) );

And see what it spits out onto the console?

Share this post


Link to post
Share on other sites
it works now! i dont' know what happened before but now it works, without any modifications! It seems in my code i had
ostream_iterator<int>( cout, "\n" )
instead of
ostream_iterator<int>( out, "\n" )

...

thx guys and sorry for being an idiot :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!