One: dynamic array
1. How to build a dynamic array?
1.1: Vector method
Vector <T> vec 2. Use the #include<vector> header fileCopy the code
C++ uses the vector statement to build dynamic arrays. Vec is the variable name and T is the variable type. Vec is initially empty.
1.2: Insert elements
In C++, we use push_back() to insert an element at the end of an array.Copy the code
Example:
#include<iostream>
#include<vector>using namespace std; int main{ vector<int> vec; //[ ] vec.push_back(1); //[1] vec.push_back(2); / / [1, 2] vec. Push_back (3); / / [1, 2, 3]return 0;
}
Copy the code
1.3: Gets the length and accesses the element
In C++, the size() method is used to get the length of a vector, and the elements of the array are accessed directly by the [] operation.Copy the code
Example:
#include<vector>
#include<iostream>
using namespace std;
int main(){
vector<int> vec;
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
for(int i = 0; i < vec.size(); i++){ cout << vec[i] <<' ';
}
return 0;
}
Copy the code
1.4: Deletes elements
In C++, the pop_back() method is used to remove the last element of a dynamic array.Copy the code
Example:
#include<iostream>
#include<vector>using namespace std; int main{ vector<int> vec; //[ ] vec.push_back(1); //[1] vec.push_back(2); / / [1, 2] vec. Push_back (3); / / [1, 2, 3] vec) pop_back (); / / [1, 2] vec) pop_back (); / / [1]return 0;
}
Copy the code
1.4: empty
We can use the clear() method in C++ to clear a vector.Copy the code
The clear method simply empties the vector, not the open memory.
2: constructor
If we need n ones we can write it like this:
int n = 10;
vector<int> vec
for(int i = 0; i < n; i++){ vec.push_back(1); }Copy the code
We can also use constructors to quickly build dynamic arrays:
int n = 10;
vector<int> vec(n,1)
Copy the code
If we use the constructor to define a vector, the first argument represents the length of the initial dynamic array, the second argument represents the value of each element in the initial array, and if we do not pass the second argument, the initial value is 0.
1.5: Two-dimensional dynamic array
vector<vector<int> > vec2
Copy the code
Defining a two-dimensional dynamic array requires a one-dimensional one-dimensional assignment.
2: a collection of
2.1: Insert elements
In C++, we use the insert() function to insert a new element into a collection. * if the collection already has an element, inserting it again has no effect.Copy the code
Example:
#include<set>
using namespace std;
int main() {set<string> country;
country.insert("China");
country.insert("American");
return 0;
}
Copy the code
2.2: Delete elements
C++ uses erase() to remove an element from a collection.Copy the code
**Example : **
#include<set>
using namespace std;
int main() {set<string> country;
country.insert("China");
country.insert("American");
country.erase("China");
return 0;
}
Copy the code
2.3: Determine whether elements exist
C++ uses the count function to find if an element is present in a collection, returning 1 if it is present and 0 otherwise.Copy the code
Example :
#include<set>
using namespace std;
int main() {set<string> country;
country.insert("China");
country.insert("American");
country.erase("China");
if(country.count("China")){
cout << China belong to country << endl;
}
else{
cout << "error";
}
return 0;
}
Copy the code
2.4: iterator
Iterators are used in C++ to access each element in a collection, and the ++ operation makes the iterator point to the next element.Copy the code
Writing:
set<T>::iterator it defines a pointersetThe <T> iterator it, ::iterator is a fixed notation.Copy the code
Usage:
#include<set>
#include<iostream>
using namespace std;
int main() {set<string> country;
country.insert("China");
country.insert("American");
country.insert("Canada");
for(set<string>::iterator it = country.begin(); it ! = country.end(); it++){ cout << *it << endl; }return 0;
}
Copy the code
Three: mapping
3.1: Construct a map
In C++, we construct a mapping using map<T1,T2> m, so we define a T1 to T2 mapping named m.Copy the code
For example, map<string, int> m builds a mapping of strings to integers.
3.2: Insert a pair of mappings
In C++ we insert a new mapping using the insert() function, which takes a pair.Copy the code
Pair is a library type defined in header utility that can be thought of as a structure consisting of two first and second, with the < operator overloaded.
The make_pair(v1,v2) function returns the pair initialized by v1 and v2.Copy the code
When we add a new mapping pair to the map, we do this by inserting a pair. If the inserted key already exists, the value will not be replaced by the new value inserted, that is, the insert will be invalid.
#include<map>
#include<string>
#include<utility>
using namespace std;
int main(){
map<string,int> dict;
dict.insert(make_pair("Tom", 1)); dict.insert(make_pair("Jone", 2)); dict.insert(make_pair("Alex", 3));return 0;
}
Copy the code
3.3: Access mapping
In C++, you can access maps as well as arrays by using [].Copy the code
Normally, we use subscript access to insert mappings rather than insert a pair.
#include<map>
#include<string>
#include<utility>
#include<iostream>
using namespace std;
int main(){
map<string,int> dict;
dict.insert(make_pair("Tom", 1)); dict.insert(make_pair("Jone", 2)); dict.insert(make_pair("Alex", 3)); cout << dict["Tom"];
return 0;
}
Copy the code
3.4: Check whether the keyword exists
In C++, you can use the count() function to determine whether a keyword is mapped, returning 1 if the keyword exists and 0 otherwise.
#include<iostream>
#include<map>
#include<string>
using namespace std;
int main(){
map<string,int> dict;
dict["Tom"] = 1;
dict["Alex"] = 2;
dict["Ros"] = 3;
if(dict.count("Tom")){
cout << "Yes" << dict["Tom"] << endl;
}
return 0;
}
Copy the code
Four: the stack
Stack, like vector, map, and set, is implemented in the C++ standard library.Copy the code
Stack not only supports push(),pop() and other basic operations, but also supports top() to obtain the top element of the stack, empty() to determine whether the element is empty, and size() to calculate the number of elements in the stack.
#include<stack>
#include<string>
#include<iostream>
using namespace std;
int main(){
stack<string> s;
s.push("123456");
s.push("zwl123");
s.push("whatheis");
while(! s.empty()){ cout << s.top() << endl; s.pop(); }return 0;
}
Copy the code