Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //List
- //===============
- // Declaration
- //=============
- list <int> L; // List of Size Zero of 'int' type
- list <double> L; // List of Size Zero of 'double' type
- list <int> L(N); // List of Size 'N' initialized with all '0'
- list <int> L(N,x); // List of Size 'N' initialized with all 'x'
- // Some References
- //=================
- L.begin(); // Iterators to the beginning
- L.end(); // Iterators to the end
- // Iterations
- //============
- // Declaration of Iterators
- list <int>::iterator it; // 'int' type iterator
- list <int>::iterator it = L.begin(); // Asigning iterators
- auto it = L.begin(); // Use of 'auto' keyword
- //Traverse of Iterators
- auto it = L.begin();
- advance(it, k); // forward traverse of 'k' iteration, Note that
- // 'it++' is equivalent to 'advance(it,1)'
- // But 'it = it + 1' is not same as 'it++'
- // Access Data O(n)
- //==================
- L.front(); // First Element
- L.back(); // Last Element
- // 'i'th Index
- auto it=L.begin();
- advance(it, i);
- cout << *it; // This is equivalent to 'L[i]' in array
- // Note that L[i] does not work
- // Printing All elements using Iterators
- for(list <int>::iterator it=L.begin(); it!=L.end(); advance(it, 1)){
- cout << *it << " ";
- }
- // Using 'auto' keyword
- for(auto it=L.begin(); it!=L.end(); advance(it, 1)){
- cout << *it << " ";
- }
- // it++ works, but it = it + 1 does not work
- for(auto it=L.begin(); it!=L.end(); it++){
- cout << *it << " ";
- }
- // Loop Iterator (Shortcut Method)
- for(auto it:L){
- cout << it << " "; // Note: here 'it' is a value, not a pointer.
- }
- // Input Data
- //============
- L.push_back(x); // Insert item 'x' at the end
- L.push_front(x); // Insert item 'x' at the beginning
- L.insert(L.begin(), x); // Insert 'x' at the beginning
- auto it = v.begin();
- advance(it, i);
- L.insert(it, x); // Insert 'x' at the 'i'th index
- auto it = v.begin();
- advance(it, i);
- L.insert(it, n, x); // Insert 'n' copies of 'x' at the 'i'th index
- // Delete Data
- //=============
- L.pop_back(); // Deletes the last item
- L.pop_front(); // Deletes the first item
- L.clear(); // Clears the whole list
- auto it = v.begin();
- advance(it, i);
- L.erase(it); // Deletes the 'i'th indexed element
- auto it_1 = v.begin();
- auto it_2 = v.begin();
- advance(it_1, i);
- advance(it_2, j);
- L.erase(it_1, it_2); // Deletes from index 'i' to 'j-1'
- L.remove(x) // Removes all elements with specific value 'x'
- // Miscellaneous Functions
- //=========================
- L.size(); // return the size of the list
- L.empty(); // return 'true' if empty, returns 'false' if non-empty
- L.sort(); //sorts the list in ascending order
- L.reverse(); //reverse the list
- L1.swap(L2) // Swaps the lists 'L1' with 'L2'
- L1.merge(L2) // Merges the lists 'L1' and 'L2' then asigns to L1
- // Here L2 will be empty
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement