STL(vector, list, map)
Overview:
STL is the soul of C++. Remember this, mastering the STL is master the most part of C++. Though array is more efficient than vector, vector is still the feature of C++. In this sector, we will discuss the basic usage of vector, list, and map. Simply explain these three STL. Vector is array with intelligent memory allocation/deallocation, which saves us from the size of container; List can be regarded as doubly linked list, specific feature will be covered in the code; Map is hash table, which will be frequently used in algorithm problem.
Vector:
// vector<int>: vector of int
// vector<vector<int>>: vector of (vector of int)
// vector<int *>: the vector of pointers to int
// vector<vector<int> *>: vector of pointers to vector of int
// vector
vector<int> V1 {3, 4, 5, 6, 7};
// 5 5 3 7
cout << V1.size() << " " << V1[2] << " " << V1.front() << " " << V1.back() << endl;
// 10 elements; all with initial value 3
vector<int> V2(10, 3);
// 6 elements; all with initial value 0
vector<int> V3(6);
// add new element 8 to the back of the vector
V1.push_back(8);
//remove the last element of the vector
V1.pop_back();
// print the vector
// call by value
// 3 4 5 6 7 8
for(int i : V1){ cout << i << " "; i++; }
cout << endl;
// The same as above
// 3 4 5 6 7 8
for(int i : V1){ cout << i << " "; }
cout << endl;
// call by reference
// 3 4 5 6 7 8
for(int &i : V1){ cout << i << " "; i++; }
cout << endl;
// 4 5 6 7 8 9
for(int i : V1){ cout << i << " "; }
cout << endl;
// Iterator
// begin() returns the address of the first element
// endI() returns the address AFTER the last element
vector<int>::iterator it1 = V1.begin(), it2 = V1.end();
auto it3 = V1.begin();
for(auto it6 = V1.begin(); it6 != V1.end(); it6++){
cout << *it6 << " ";
}
auto it10 = V1.begin() + 10;
// auto can be very handful
// map<int , vector<list<int*> *>:: iterator it5 = M1.begin();
// auto it5 = M1.end()
List:
// list: similar to douly linked list
list<int> L1{10, 11, 12, 13, 14};
//does not support subscript as ina array or vector
cout << L1.size() << " " << L1.front() << " " << L1.back() << endl;
L1.push_back(100);
L1.pop_back();
L1.push_front(200);
L1.pop_front();
for(auto i : L1){ cout << i << " "; }
cout << endl;
// only list support sort, not vector
L1.sort();
for(auto it7 = L1.begin(); it7 != L1.end(); it7++){ cout << *it7 << " ";}
cout << endl;
// return the first address of the first match or V1.end() if no match
auto it8 = find(V1.begin(), V1.end(), 5);
if(it8 != V1.end())
V1.erase(it8);
//v1.begin() can be written as begin(V1)
// [begin(), end())
auto it9 = find(L1.begin(), L1.end(), 12);
if(it9 != L1.end() ) L1.erase(it9);
// it would compile
// auto it11 = L1.begin() + 1;
// it only allow ++ / --, like linked list, it only allows forward or backward
auto it11 = L1.begin()++;
// only list support remove
// delete the first element with value 14
// doing nothing if no mtach
L1.remove(14);
Map:
// map
map<int, string> M1{{25, "Mary"}, {11, "Dan"}, {6, "Sanjay"} };
// Dan
cout << M1[11] << endl;
// the two data items are referred to as key and value
// adding {35, "Steve"} to M1
M1[35] = "Steve";
// update {25, "Nancy"} to M1
M2[25] = "Nancy";
// when you insert item to map, it will automatically sort by key
for(auto i : M1) {
cout << i.first << " " << i.second << " ";
}
for(auto it12 = M1.begin(); it12 != M1.end(); it12++){
cout << (*it12).first << " " << (*it12).second << " ";
}
for(auto it12 = M1.begin(); it12 != M1.end(); it12++){
cout << it12->first << " " << it12->second << " ";
}
Complete Code:
//STL Standard Template Liabrary
#include <iostream>
// container: vector, list, map, set, queue, stack, array, etc.
#include <vector>
#include <list>
#include <map>
#include <string>
using namespace std;
// the same as void f1(int* A)
void f1(int A[]){
// it won't work, because it only pass the address
// it's a MUST to pass the size to the function.
// the function should be:
// void f1(int A[], int size)
cout << sizeof(A) / sizeof(int) << endl;
}
//
int main(){
int A[10];
// to get the size of array A
cout << sizeof(A) / sizeof(int) << endl;
string s1 = "This is very useful!";
//================================STL===========================================
// vector<int>: vector of int
// vector<vector<int>>: vector of (vector of int)
// vector<int *>: the vector of pointers to int
// vector<vector<int> *>: vector of pointers to vector of int
// vector
vector<int> V1 {3, 4, 5, 6, 7};
// 5 5 3 7
cout << V1.size() << " " << V1[2] << " " << V1.front() << " " << V1.back() << endl;
// 10 elements; all with initial value 3
vector<int> V2(10, 3);
// 6 elements; all with initial value 0
vector<int> V3(6);
// add new element 8 to the back of the vector
V1.push_back(8);
//remove the last element of the vector
V1.pop_back();
// print the vector
// call by value
// 3 4 5 6 7 8
for(int i : V1){ cout << i << " "; i++; }
cout << endl;
// The same as above
// 3 4 5 6 7 8
for(int i : V1){ cout << i << " "; }
cout << endl;
// call by reference
// 3 4 5 6 7 8
for(int &i : V1){ cout << i << " "; i++; }
cout << endl;
// 4 5 6 7 8 9
for(int i : V1){ cout << i << " "; }
cout << endl;
// Iterator
// begin() returns the address of the first element
// endI() returns the address AFTER the last element
vector<int>::iterator it1 = V1.begin(), it2 = V1.end();
auto it3 = V1.begin();
for(auto it6 = V1.begin(); it6 != V1.end(); it6++){
cout << *it6 << " ";
}
auto it10 = V1.begin() + 10;
// auto can be very handful
// map<int , vector<list<int*> *>:: iterator it5 = M1.begin();
// auto it5 = M1.end()
// list: similar to douly linked list
list<int> L1{10, 11, 12, 13, 14};
//does not support subscript as ina array or vector
cout << L1.size() << " " << L1.front() << " " << L1.back() << endl;
L1.push_back(100);
L1.pop_back();
L1.push_front(200);
L1.pop_front();
for(auto i : L1){ cout << i << " "; }
cout << endl;
// only list support sort, not vector
L1.sort();
for(auto it7 = L1.begin(); it7 != L1.end(); it7++){ cout << *it7 << " ";}
cout << endl;
// return the first address of the first match or V1.end() if no match
auto it8 = find(V1.begin(), V1.end(), 5);
if(it8 != V1.end())
V1.erase(it8);
//v1.begin() can be written as begin(V1)
// [begin(), end())
auto it9 = find(L1.begin(), L1.end(), 12);
if(it9 != L1.end() ) L1.erase(it9);
// it would compile
// auto it11 = L1.begin() + 1;
// it only allow ++ / --, like linked list, it only allows forward or backward
auto it11 = L1.begin()++;
// only list support remove
// delete the first element with value 14
// doing nothing if no mtach
L1.remove(14);
// map
map<int, string> M1{{25, "Mary"}, {11, "Dan"}, {6, "Sanjay"} };
// Dan
cout << M1[11] << endl;
// the two data items are referred to as key and value
// adding {35, "Steve"} to M1
M1[35] = "Steve";
// update {25, "Nancy"} to M1
M2[25] = "Nancy";
// when you insert item to map, it will automatically sort by key
for(auto i : M1) {
cout << i.first << " " << i.second << " ";
}
for(auto it12 = M1.begin(); it12 != M1.end(); it12++){
cout << (*it12).first << " " << (*it12).second << " ";
}
for(auto it12 = M1.begin(); it12 != M1.end(); it12++){
cout << it12->first << " " << it12->second << " ";
}
return 0;
}