C++ std list is a library that provides a highly efficient doubly linked list implementation. It is a part of the C++ Standard Template Library (STL) and is used for fast insertion and deletion of elements in a list container.
Example 1:
#include #include using namespace std;
int main() { list myList; myList.push_back(5); myList.push_back(10); myList.push_back(15);
cout << "Elements of the list: "; for(auto it = myList.begin(); it != myList.end(); it++) { cout << *it << " "; }
return 0; }
Output: Elements of the list: 5 10 15
In this example, we create a list container named myList and push three integers. We then traverse through the list using an iterator and print its elements.
Package Library: STL
Example 2:
#include #include using namespace std;
int main() { list myList1 = {1, 2, 3, 4}; list myList2 = {5, 6, 7, 8};
myList1.splice(myList1.begin(), myList2);
cout << "Elements of the list after splice operation: "; for(auto it = myList1.begin(); it != myList1.end(); it++) { cout << *it << " "; }
return 0; }
Output: Elements of the list after splice operation: 5 6 7 8 1 2 3 4
In this example, we create two list containers named myList1 and myList2 and assign them with some values. We then use the splice() function to merge the two lists. This function is used to transfer elements from one list to another. In this case, we merge myList2 into myList1 at the beginning. We then traverse through the merged list and print its elements.
Package Library: STL
C++ (Cpp) list - 30 examples found. These are the top rated real world C++ (Cpp) examples of std::list extracted from open source projects. You can rate examples to help us improve the quality of examples.