reading multiple lines into arrays
Hello,
I am just starting to solve a problem, with multiple arrays, something like:
2,2
1,2,3,4
6,7,8,9,10
0,2
1,1
+++++
output:
3, 7
first line no of arrays and no of queries. 2nd and 3rd line, array contents. 4th and 5th querirs (i.e 0th array, 2and elem, 1st array, 1st elem)
for that, i what is the good way to read input , ie ,may be use istream_iterator. But follwing startup program fails to compile !
Code:
std::vector<int> vec;
std::string stringvalues = "125 320 512 750 333";
std::istringstream iss(stringvalues);
vec.emplace_back(std::istream_iterator<int>{ iss }, std::istream_iterator<int>{});
Error C2440 'initializing': cannot convert from 'initializer list' to 'int'
But when i try :
Code:
std::string stringvalues = "125 320 512 750 333";
std::istringstream iss(stringvalues);
std::vector<int> vec(std::istream_iterator<int>{ iss }, std::istream_iterator<int>{});
works ok !
thanks a lot for comments and inputs
Pdk
Re: reading multiple lines into arrays
Code:
vec.emplace_back(std::istream_iterator<int>{ iss }, std::istream_iterator<int>{});
try:
Code:
vec.assign(std::istream_iterator<int>{ iss }, std::istream_iterator<int>{});
Look it up :)
Re: reading multiple lines into arrays
Thanks a lot kaud.
Sorry for not doing proper home work
I was googling yesterday (reg emplace_back ), and i think i need , vector of vectors for emplace_back to work :
Code:
std::vector<std::vector<int>> vec;
std::string stringvalues = "125 320 512 750 333";
std::istringstream iss(stringvalues);
vec.emplace_back(std::istream_iterator<int>{ iss }, std::istream_iterator<int>{});
The above code works now with emplace_back :)