#include#include using namespace std; int main() { // Create a string list list
mylist; mylist.push_back("Java"); mylist.push_back("Python"); mylist.push_back("C++"); mylist.push_back("JavaScript"); mylist.push_back("Scala"); cout << "List of Programming languages :: \n\n"; // Traverse and print the elements of the list using Iterator for (list ::iterator it = mylist.begin(); it != mylist.end(); ++it) cout << *it << "\n"; return 0; }
List of Programming languages :: Java Python C++ JavaScript Scala
#include#include #include using namespace std; int main() { // Creating a string list using initializer list list
mylist {"Red", "Blue", "Green", "Black", "White"}; cout << "List of Colors :: \n\n"; // Traverse and print the elements of the list using Iterator for (list ::iterator it = mylist.begin(); it != mylist.end(); ++it) cout << *it << "\n"; // Sorting the list in descending order mylist.sort(); mylist.reverse(); cout << "\nList of Colors After Sorting :: \n\n"; // Traverse and print the elements of the list after sorting for (list ::iterator it = mylist.begin(); it != mylist.end(); ++it) cout << *it << "\n"; return 0; }
List of Colors :: Red Blue Green Black White List of Colors After Sorting :: White Red Green Blue BlackIn the above example, we create a string list using an initializer list and print all the elements present in the list using the end() function. We sort the list using the sort() function and then rearrange it in the descending order using the reverse() function. The package library used in the example is