-
September 21st, 2020, 03:37 AM
#1
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
Last edited by pdk5; September 21st, 2020 at 03:55 AM.
-
September 21st, 2020, 04:47 AM
#2
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
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
September 21st, 2020, 05:23 AM
#3
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
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|