How to populate List (string) items on console window in c++cli.
Thanks in Advance
How to populate List (string) items on console window in c++cli.
Thanks in Advance
Are you after the following or do you want to know how to read from console input and store in string (which is then stored in list I assume), this is the first case, how to store strings in list (and output the contents of list):
#include <iostream>
#include <string>
#include <list>
using std::string;
using std::list;
using std::cout;
using std::endl;
list<string> strlist;
strlist.push_back(string("string one"));
strlist.push_back(string("string two"));
strlist.push_back(string("and so on"));
for (list<string>::iterator it = strlist.begin(); it != strlist.end(); ++it)
{
std::cout << (*it) << std::endl;
}
Note: the list holds copy of strings, meaning you end up copying strings when you assign them to the list and when you return them from list. To avoid that you could allocate memory for string and keep only pointers in list:
list<string*> ls;
ls.push_back(new string("string one"));
and so on.
This works as well the above is more verbose to see how to access the list elements, in copy algorithm it all happens behind the scenes:
copy( strlist.begin(), strlist.end(), ostream_iterator<string>( cout, ", " ) );
You can use std::copy in and std::ostream_iterator in to copy elements into the output like so:
#include <iostream>
#include <string>
#include <list>
#include <algorithm>
int main( int, char*[] )
{
using namespace std;
list<string> mylist;
mylist.push_back( "String1" );
mylist.push_back( "String2" );
mylist.push_back( "String3" );
copy( mylist.begin(), mylist.end(), ostream_iterator<string>( cout, ", " ) );
}