As keskiverto said, stringstream is over the top here as you're just working with various strings.
But this works ok (newstring ends up as "eb")
1 2 3 4 5 6 7 8 9 10 11 12
|
vector<string> listOfFiles;
listOfFiles.push_back("hello.txt");
listOfFiles.push_back("big.txt");
stringstream ss;
string newstring;
ss << listOfFiles[0][1];
ss << listOfFiles[1][0];
ss >> newstring;
cout << newstring << endl;
|
so I guess you're doing something differently.
Are you extracting the first char immediately after inserting it, and then trying to continue? Like
1 2 3 4
|
ss << listOfFiles[0][1];
ss >> newstring;
ss << listOfFiles[1][0];
ss >> newstring;
|
Here the first two lines behave ok. But note that because 'e' is the last character in the stream (well, it's the only one), then the call on line 2 of this last fragment causes the eof flag to be set on the stream which means that it doesn't respond to the subsequent input and output calls.
Adding a clear() call to reset the eof flag
1 2 3 4 5
|
ss << listOfFiles[0][1];
ss >> newstring;
ss.clear(); // reset flags
ss << listOfFiles[1][0];
ss >> newstring;
|
changes the output to
this is because the extraction of 'e' also moved the stream position, which determines which char will be extracted next. As you've already extracted 'e', the next char available is the 'b'. But if we set the positions (seek it back) to the start of the stream, then you get "eb" as you wanted.
1 2 3 4 5 6
|
ss << listOfFiles[0][1];
ss >> newstring;
ss.clear(); // reset flags
ss.seekg(0); // seek back to start of stream
ss << listOfFiles[1][0];
ss >> newstring;
|
but this code is far messier than
1 2
|
newstring += listOfFiles[0][1];
newstring += listOfFiles[1][0];
|
Andy
PS @aggsyb could you correct your tagging in your opening post so the fragments make more sense. Thanks!