| | |
istream_iterator ... segmentation fault ?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 119
Reputation:
Solved Threads: 10
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
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
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <vector> #include <iterator> #include <sstream> using namespace std; vector<string> split( const string &sInput ) { istringstream is( sInput ); vector<string> vect; copy( istream_iterator<string>(is) , istream_iterator<string>() , vect.begin() ); sort( vect.begin() , vect.end() ); return vect; } int main(int argc, char* argv[]) { if(argc != 2) return 1; string sForSplit = argv[1]; vector<string> vect; vect = split( sForSplit ); ostream_iterator<string> os( cout ,"\n"); copy ( vect.begin(), vect.end() , os ); }
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
>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:
You also need to include <algorithm> if you plan to use the sort function.
>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:
C++ Syntax (Toggle Plain Text)
copy( istream_iterator<string>(is) , istream_iterator<string>() , back_inserter( vect ) );
I'm here to prove you wrong.
•
•
Join Date: Jan 2008
Posts: 119
Reputation:
Solved Threads: 10
•
•
•
•
>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:
You also need to include <algorithm> if you plan to use the sort function.C++ Syntax (Toggle Plain Text)
copy( istream_iterator<string>(is) , istream_iterator<string>() , back_inserter( vect ) );
thx ![]() |
Other Threads in the C++ Forum
- Previous Thread: C++ GUI framework help??
- Next Thread: Vector of stringstreams
| Thread Tools | Search this Thread |
api array arrays based binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






