Apr 11, 2017 at 12:05pm
I am not sure if I understand correctly. If you need to store some strings in a list you could do it like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#include <iostream>
#include <string>
#include <list>
using namespace std;
int main()
{
const int NUM_LINES = 5;
string line;
list <string> myList;
cout << "Enter 5 lines." << endl;
for (int i = 0; i < NUM_LINES; i++)
{
cout << "Enter line " << i + 1 << " ";
getline(cin, line);
myList.push_back(line);
}
// display the lines
for (const string& line : myList)
cout << line << "\n";
return 0;
}
|
EDIT reason - see gunnerfunner's post below
Last edited on Apr 11, 2017 at 2:46pm
Apr 11, 2017 at 1:19pm
Thomas - you're pushing back a list that has already allocated some memory
Apr 11, 2017 at 2:47pm
@gunnerfunner,
thanks, you were right. Corrected now