istream_iterator ... segmentation fault ?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 119
Reputation: kux is on a distinguished road 
Solved Threads: 10
kux kux is offline Offline
Junior Poster

istream_iterator ... segmentation fault ?

 
0
  #1
Feb 12th, 2008
i have a problem... i wanna make a function that takes a string as input splits it, storing the resulting strings in a vector<string> and then sort it
  1. #include <iostream>
  2. #include <vector>
  3. #include <iterator>
  4. #include <sstream>
  5.  
  6. using namespace std;
  7.  
  8. vector<string> split( const string &sInput )
  9. {
  10. istringstream is( sInput );
  11. vector<string> vect;
  12. copy( istream_iterator<string>(is) , istream_iterator<string>() , vect.begin() );
  13. sort( vect.begin() , vect.end() );
  14. return vect;
  15.  
  16. }
  17.  
  18. int main(int argc, char* argv[])
  19. {
  20. if(argc != 2) return 1;
  21. string sForSplit = argv[1];
  22. vector<string> vect;
  23. vect = split( sForSplit );
  24. ostream_iterator<string> os( cout ,"\n");
  25. copy ( vect.begin(), vect.end() , os );
  26.  
  27. }

it compiles, but when i run it i get Segmentation Fault

can anyone explain me why? i don't want to implement the function without the istream_iterator, done that before, i just want to know where does the runtime error occur and why.
thx
Last edited by Narue; Feb 12th, 2008 at 5:26 pm. Reason: Fixed code tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,740
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 739
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: istream_iterator ... segmentation fault ?

 
0
  #2
Feb 12th, 2008
>vector<string> vect;
>copy( istream_iterator<string>(is) , istream_iterator<string>() , vect.begin() );
The vector is empty, and copy won't call push_back for you. You need to add a back_inserter to the destination or copy will write to memory that you don't own:
  1. copy( istream_iterator<string>(is) ,
  2. istream_iterator<string>() , back_inserter( vect ) );
You also need to include <algorithm> if you plan to use the sort function.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 119
Reputation: kux is on a distinguished road 
Solved Threads: 10
kux kux is offline Offline
Junior Poster

Re: istream_iterator ... segmentation fault ?

 
0
  #3
Feb 12th, 2008
Originally Posted by Narue View Post
>vector<string> vect;
>copy( istream_iterator<string>(is) , istream_iterator<string>() , vect.begin() );
The vector is empty, and copy won't call push_back for you. You need to add a back_inserter to the destination or copy will write to memory that you don't own:
  1. copy( istream_iterator<string>(is) ,
  2. istream_iterator<string>() , back_inserter( vect ) );
You also need to include <algorithm> if you plan to use the sort function.
true, thx
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC