The Standard Template Library Common Classes
List
Example 1:
Create a list that holds string values. Print the size and first and last elements. Finally using a for each loop print all the elements to the console screen,
#include <iostream>
#include <list>
#include <string>
using namespace std;
int main()
{
list<string> strList = { "john", "Paul", "Jesse", "Patty" };
cout << "Size of the list is " << strList.size() << endl;
cout << "The first item in the list
is " << strList.front() << endl;
//The following
are not allowed as list does not allow random access
//cout <<
"The second item in the list is " << strList[1] << endl;
//using substring
//cout <<
"The second item in the list is " << strList.at(2) <<
endl; //using at()
cout << "The last item in the list is
" << strList.back() << endl;
for (auto&&
s : strList)
{
cout << s <<endl;
}
system("pause");
return 0;
}
Comments
Post a Comment