D
dragoncoder
Hi,
I am just a newbie in STL and trying to learn istream_iterator. My
problem is I want a program which will take a comma separated string
from the command line, tokenize it and copies into a vector. Then I am
printing the vector. Here is the code I tried.
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
#include <functional>
#include <cassert>
int main(int argc, char* argv[])
{
assert ( argv[1] );
std::vector <std::string> v;
std::istringstream iss_st ( argv[2] );
std::istringstream iss_en ( "\n" );
std::istream_iterator < std::string > st ( iss_st );
std::istream_iterator < std::string > en ( iss_en );
std::copy ( st, en, std::back_inserter ( v ) );
std::copy ( v.begin(), v.end(), std:stream_iterator <std::string> (
std::cout, "-" ) );
return 0;
}
But this code is aborting while running. I know I am missing something
very silly but not sure what.
Is there a better way of achieving the same thing though a different
way ?
Please help.
Thanks
I am just a newbie in STL and trying to learn istream_iterator. My
problem is I want a program which will take a comma separated string
from the command line, tokenize it and copies into a vector. Then I am
printing the vector. Here is the code I tried.
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
#include <functional>
#include <cassert>
int main(int argc, char* argv[])
{
assert ( argv[1] );
std::vector <std::string> v;
std::istringstream iss_st ( argv[2] );
std::istringstream iss_en ( "\n" );
std::istream_iterator < std::string > st ( iss_st );
std::istream_iterator < std::string > en ( iss_en );
std::copy ( st, en, std::back_inserter ( v ) );
std::copy ( v.begin(), v.end(), std:stream_iterator <std::string> (
std::cout, "-" ) );
return 0;
}
But this code is aborting while running. I know I am missing something
very silly but not sure what.
Is there a better way of achieving the same thing though a different
way ?
Please help.
Thanks