In this part of my C++ tutorial I’ll cover Sequence Containers which contain data that is stored in order. Previously I covered Vectors and will now cover Deques, Lists and Forward Lists.
I’m getting near the end of my core coverage of C++. Once I finish I will then move on to GUI development, Algorithms and all the other things that have been requested. All of the code follows below.
If you like videos like this consider donating $1, or simply turn off Ad Blocking software. Either helps me to keep making free tutorials for all.
Code From the Video
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
#include <cstdlib> #include <iostream> #include <string> #include <vector> #include <ctime> #include <numeric> #include <cmath> #include <sstream> #include <thread> #include <ctime> #include <deque> #include <list> #include<forward_list> bool isEven(const int& val){ return (val % 2) == 0; } int main() { // ---------- SEQUENCE CONTAINERS ---------- // Contains data stored in order // ---------- DEQUES ---------- // A deque (Deck) is a dynamic array like vectors // except it also allows for insertion or deletion // from the front std::deque<int> deq1; // Add to the end and front deq1.push_back(5); deq1.push_front(1); // Add values with assign deq1.assign({11,12}); // Get the size std::cout << "Size : " << deq1.size() << "\n"; // Access by index std::cout << deq1[0] << "\n"; std::cout << deq1.at(1) << "\n"; // Add at an index using an iterator std::deque<int>::iterator it = deq1.begin() + 1; deq1.insert(it, 3); // Add multiple values int tempArr[5] = {6,7,8,9,10}; deq1.insert(deq1.end(), tempArr, tempArr+5); // Erase at an index deq1.erase(deq1.end()); // Erase 1st 2 elements deq1.erase(deq1.begin(), deq1.begin()+2); // Pop first value deq1.pop_front(); // Pop last deq1.pop_back(); // Create a deque with 2 50s std::deque<int> deq2(2,50); // Swap values in deques deq1.swap(deq2); // Delete all values deq1.clear(); // Cycle through the deque for(int i : deq1) std::cout << i << "\n"; // ---------- END DEQUES ---------- // ---------- LIST ---------- // Lists are the most efficient at inserting, // moving and extracting elements, but lack // direct access to elements // Add values int arr[5] = {1,2,3,4,5}; std::list<int> list1; list1.insert(list1.begin(), arr, arr+5); // Adding values with assign list1.assign({10,20,30}); // Add to end and front list1.push_back(5); list1.push_front(1); // Get the size std::cout << "Size : " << list1.size() << "\n"; // Can't access index // std::cout << list1[0] << "\n"; // You can access the index with an iterator std::list<int>::iterator it2 = list1.begin(); std::advance(it2, 1); std::cout << "2nd Index : " << *it2 << "\n"; // Insert at an index it2 = list1.begin(); list1.insert(it2, 8); // Erase at an index list1.erase(list1.begin()); // Erase 1st 2 elements it2 = list1.begin(); std::list<int>::iterator it3 = list1.begin(); std::advance(it3, 2); list1.erase(it2, it3); // Pop first value list1.pop_front(); // Pop last list1.pop_back(); // Create another list int arr2[6] = {10,9,8,7,6,6}; std::list<int> list2; list2.insert(list2.begin(), arr2, arr2+5); // Sort the list list2.sort(); // Reverse the list list2.reverse(); // Remove duplicates list2.unique(); // Remove a value list2.remove(6); // Remove if a condition is true list2.remove_if (isEven); // Merge lists list1.merge(list2); for(int i : list2) std::cout << i << "\n"; std::cout << "\n"; // Cycle through the list for(int i : list1) std::cout << i << "\n"; std::cout << "\n"; // ---------- END LIST ---------- // ---------- FORWARD_LIST ---------- // A forward list is like a list, but each list // item only has a link to the next item in the // list and not to the item that proceeds it. // This make them the quickest of the sequence // containers std::forward_list<int> fl1; // Assign values fl1.assign({1,2,3,4}); // Push and pop front fl1.push_front(0); fl1.pop_front(); // Get 1st std::cout << "Front : " << fl1.front(); // Get iterator for 1st element std::forward_list<int>::iterator it4 = fl1.begin(); // Insert after 1st element it4 = fl1.insert_after(it4, 5); // Delete just entered 5 it4 = fl1.erase_after(fl1.begin()); // Place in 1st position fl1.emplace_front(6); // Remove a value fl1.remove(6); // Remove if a condition is true fl1.remove_if (isEven); std::forward_list<int> fl2; fl2.assign({9,8,7,6,6}); // Remove duplicates fl2.unique(); // Sort fl2.sort(); // Reverse fl2.reverse(); // Merge lists fl1.merge(fl2); // Clear fl1.clear(); for(int i : fl1) std::cout << i << "\n"; std::cout << "\n"; for(int i : fl2) std::cout << i << "\n"; // ---------- END FORWARD_LIST ---------- return 0; } |
Leave a Reply