The main contents of this article are as follows:
1.vector
Definition of 1.1vector
Access to elements within a 1.2vector container
1.3vector Common functions
2.set
Definition of 2.1 Set
Access to elements within a 2.2set container
2.3set Common functions
3.string
3.1 Definition of string
Access to elements within a 3.2string container
3.3string Common functions
4.map
Definition of 4.1 map
4.2 Access to elements within a map container
4.3 Map common functions
5. Queue
Definition of the 5.1 queue
5.2 Access to elements within a queue container
5.3 Queue Common functions
5.4 Queue Common Uses
6.priority_queue
Definition of 6.1 priority_queue
6.2 Access to elements within a priority_queue container
6.3 Priority_queue Common functions
6.4 Setting of element priority within Priority_queue
7. Stack
Definition of 7.1stack
Access to elements within a 7.2stack container
8. Pair
Definition of 8.1 pair
8.2 Access to elements in the pair
8.3 Pair common functions
Common uses of 8.4 pair
9. Algorithm header File
1.vector
Vector, variable-length array
Header file
#include <vector>
Definition of 1.1vector
Vector<typename> name;
For example:
Vector<int> name;
If TypeName is a vector
vector<vector<int>>name;
Equivalent to a two-D array
Vector<typename> Arrayname[arraysize];
For example:
Vector<int> vi[100];
Access to elements within a 1.2vector container
(1) Access by subscript
Vi[index];
(2) Access via iterators
Vector<typename>::iterator it;
For example:
Vector<int>::iterator it;
When there is a context, you can directly use Auto It=vi.begin ();
For example:
for (auto It=vi.begin (); It!=vi.end (); it++)
printf ("%d", *it);
Note: Vi.begin () and Vi.end () left closed right.
1.3vector Common functions
(1) push_back ()
Adds an element at the end.
Vi.push_back (i);
(2) Pop_back ()
Deletes a trailing element.
Vi.pop_back ();
(3) Size ()
Returns the number of vector elements, note the unsigned type.
Vi.size ();
(4) Clear ()
Clears all elements in the vector.
Vi.clear ();
(5) Insert ()
Insert (IT,X) inserts an element x before the iterator it.
Vi:1 2 3 4 5
Vi.insert (Vi.begin () +2,-1);
Vi:1 2-1 3 4 5
(6) Erase ()
1. Delete a single element
Vi:5 6 7 8 9
Vi.erase (Vi.begin () +3)
Vi:5 6 7 9
2. Delete all elements in a range
Erase[first,last); Left closed right Open
Vi:5 6 7 8 9
Vi.erase (Vi.begin () +1,vi.begin+4)
Vi:5 9
2.set
Collection, a container that is automatically ordered internally and does not contain duplicate elements
Header file
#include <set>
Definition of 2.1 Set
Set<typename> name;
For example:
Set<int> name;
Set array
set<typename>arrayname[arraysize];
For example:
Set<int> a[100];
Access to elements within a 2.2set container
Set can only be accessed with iterators
Set<typename>::iterator it;
For example:
Set<int>::iterator it;
Set<int> St;
for (auto It=st.begin (); It!=st.end (); it++)
Print ("%d", *it);
St:2 3 5
The set element automatically increments the sort and automatically removes the repeating element.
Note: cannot use It<st.end ();
cannot be used except vectors and string containers * (it+i)
2.3set Common functions
(1) Insert ()
Insert (x) inserts x into the set container and automatically increments the sort and de-weight.
St.insert (x)
(2) Find ()
Find (value) returns an iterator in set that corresponds to value.
Set<int>::iterator it = st.find (2);
(3) Erase ()
1. Delete a single element
St.erase (IT); It is an iterator
St.erase (Find (100));
St.erase (value), which is the value of the element to be deleted.
St.erase (100);
2. Delete all elements in the interval
Erase[first,last); Left closed right Open
St.erase (First,last);
St.erase (It,st.end ());
(4) Size ()
Returns the number of elements within a set, unsigned type
(5) Clear ()
Clears all the elements in the set
3.string
Array of strings
3.1 Definition of string
String str= "ABCD";
Access to elements within a 3.2string container
(1) Access by subscript
Str[index];
Output str
cout<<str;
printf ("%s", Str.c_str ());
(2) Access via iterators
String iterators do not require parameters
String::iterator it;
For (It=str.begin (); It!=str.end (); it++)
printf ("%c", *it);
3.3string Common functions
(1) operator+=
STR1+=STR2;
(2) Compare operator
can be used directly = =,!=,<=,<,>,>= comparison size, comparison rules are dictionary order
(3) Length ()/size ()
Returns the length of the string, which is essentially the same.
(4) Insert ()
1.insert (pos.string);
Inserts a string in the POS number position
String str1= "abcxyz";
String str2= "OPQ";
Str1.insert (3,STR2);
Str1:abcopqxyz
2.insert (IT,IT2,IT3); It is the original string to be inserted in the position, it2,it3 bit to insert the string end-to-end iterator. [It2,it3] left open right closed
String str1= "abcxyz";
String str2= "OPQ";
Str1.insert (Str1.begin () +3,str2.begin (), Str2.end ())
Str1:abcopqxyz
(5) Erase ()
1. Delete a single element
Str.erase (IT)//it iterator for the location of the element to be deleted
2. Delete all elements in a range
(1) str.erase (first,last); [First,last] left open right closed
String str= "ABCDEFG";
Str.erase (Str.begin () +2,str.end ()-1);
Str:abg
(2) Str.erase (pos,length);
The POS is the starting position, and the string with length is removed later.
String str= "ABCDEFG";
Str.erase (3,2);
Str:abcfg
(6) Clear ()
Str.clear (); Empty string
(7) substr ()
SUBSTR (Pos,len); Returns a substring that starts at the POS number bit and is Len long.
String str= "Thank you for your Smile";
Str.substr (0,5);
Thank
Str.substr (14,4);
Your
Str.substr (19,5);
Smile
(8) String::npos
String::npos is a constant whose value is the -1,unsigned_int type as the return value when the Find function is mismatch.
(9) Find ()
Str1.find (STR2); Returns the position of the first occurrence of the str2 in str1 when STR2 is a substring of str1, or str2 if str1 is not a substring of string::npos.
Str1.find (Str2,pos); The return value is the same as above, starting with the str1 POS number bit to match the str2.
String str1= "Thank you for your Smile";
String str2= "You";
String str3= "Me";
Cout<<str1.find (STR2);
6
Cout<<str1.find (str2,7);
14
(ten) Replace ()
1.str1.replace (POS,LEN,STR2); Replace the str1 from the POS number bit with the length Len string as str2.
2.str1.replace (IT1,IT2,STR2); Replace the substring of the STR1 [It1,it2] range with the str2.
String Str1= "Maybe you'll turn around";
String str2= "would not";
Striing str3= "surely";
Cout<<str1.replace (10,4,STR2);
Maybe you'll not turn around
Cout<<str1.replace (Str1.begin (), Str1.begin () +5,STR3);
Surely you'll not turn around
4.map
Map, map can map any base type (including STL containers) to any basic type (including STL containers).
For example:
string-to-page.
Header file
#include <map>
Definition of 4.1 map
Map<typename1,typename2> MP;
Map<key,value> MP;
The Map<int,int> MP is equivalent to a normal array of int types.
If it is a string-to-integer mapping, it must be in string and not in a char array.
Map<string,int> MP;
The keys and values of the map can also be STL containers.
For example:
Map<set<int>,string> MP;
4.2 Access to elements within a map container
1. Access by subscript
Like accessing a normal array, for example
Map<char,int> MP;
mp[' C ') to access its corresponding integer.
mp[' C ']=20;
Note: The key value of map is unique.
2. Access via iterators
Map<typename1,typename2>::iterator it;
Using the It->first access key
Accessing Values with It->second
map<char,int>mp;
mp[' m ']=20;
mp[' R ']=30;
mp[' a ']=40;
for (auto It=mp.begin (); It!=mp.end (); it++)
printf ("%c%d\n", It->first,it->second);
Map will automatically sort the key values from small to large.
4.3 Map common functions
(1) Find ()
Find (Key) returns an iterator for the mapping of key values.
Auto it = mp.find (' C ');
(2) Erase ()
1. Delete a single element.
Mp.erase (IT); It is an iterator.
Mp.erase (key); Key for the map you want to delete.
2. Delete all elements within a range.
Map.erase (First,last) [First,last] Removes the iterator interval and left-closes to the right.
(3) Size ()
Returns the logarithm of the map in map.
(4) Clear ()
Clears all the elements in the map.
5.queue
Queue, a FIFO container.
Header file
#include <queue>
Definition of the 5.1 queue
Queue<typename> name;
5.2 Access to elements within a queue container
Queue is a first-in-a-kind data structure that restricts access.
Only front () access to the first element of the team, back () access to the tail element.
Queue<int> Q;
Q.front ();
Q.back ();
5.3 Queue Common functions
(1) Push ()
Q.push (x) queue X
(2) Front ()/Back ()
Access the first and the end elements of the team.
(3) Pop ()
Q.pop () Order the first element of the team out of the team.
(4) Empty ()
Detects whether the queue is empty, returns true to NULL, and returns false for non-null.
(5) Size ()
Returns the number of elements within a queue.
5.4 Queue Common Uses
such as breadth-first search.
6.priority_queue
Priority queue, which is implemented by heap. In the priority queue, the team first element is the highest priority in the current queue.
Header file
#include <queue>
Definition of 6.1 priority_queue
Priority_queue<typename> name;
6.2 Access to elements within a priority_queue container
can only be accessed with the Q.top () function.
6.3 Priority_queue Common functions
(1) Push ()
Q.push (x) queue x.
(2) Top ()
Q.top () can get the first element of the team.
(3) Pop ()
Make team first element (heap top element) out of the team.
Q.pop ();
(4) Empty ()
Detects if the priority queue is empty, returns true if it is empty, and returns false to null.
(5) Size ()
Returns the number of elements in the priority queue.
6.4 Setting of element priority within Priority_queue
1. Priority settings for basic data types
Basic data types are data types such as int, double, char, and so on.
The following two priority queues are defined as equivalent.
Priority_queue<int> Q;
priority_queue<int,vector<int>,less<int> > Q;
Vector<int> is the container that hosts the underlying data structure heap (heap), while Less<int> is the comparison class.
Less<int> indicates that the higher the priority of the number, the greater the priority of the greater<int> representing the smaller the number. The char type is ordered by dictionary.
2. Priority settings for structs
The comparison symbol needs to be overloaded.
For example:
struct fruit{
String name;
int price;
friend bool operator< (Fruit f1,fruit f2) {
Return f1.price<f2.price;
}
};
Judge F1==f2, then the equivalent! (F1<F2) &&! (F2<F1).
So priority_queue<fruit> Q; The high-priced fruit has a high priority on the inside.
Similarly, if you want to lower the price of the fruit priority, then the < to >.
struct fruit{
String name;
int price;
friend bool operator< (Fruit f1,fruit f2) {
Return f1.price>f2.price;
}
};
Note: Only < can be overloaded, and overloaded > compiles incorrectly.
Do not greater<typename> do not overload < Do not use the friend function
With CMP.
#include <iostream>
#include <queue>
#include <string>
using namespace Std;
struct fruit{
String name;
int price;
}F1,F2,F3;
struct cmp{
BOOL operator () (fruit f1,fruit F2) {
Return f1.price>f2.price;
}
};
int main ()
{
Priority_queue<fruit,vector<fruit>,cmp> Q;
F1.name= "a";
f1.price=3;
F2.name= "B";
f2.price=2;
F3.name= "C";
f3.price=1;
Q.push (F1);
Q.push (F2);
Q.push (F3);
Cout<<q.top () .name<< "" <<q.top (). Price;
return 0;
}
Results: C 1
Even basic data types or other STL containers, such as set, can define precedence in the same way. If the structure body data is large, the previous const & reference to improve efficiency.
friend bool operator< (const & Fruit F1,const & fruit F2) {
Return f1.price>f2.price;
}
struct cmp{
BOOL Operator () (const & Fruit F1,const & fruit F2) {
Return f1.price>f2.price;
}
};
The essence of Priority_queue is the heap.
7.stack
Stack, a last-in, first-out container.
Header file
#include <stack>
Definition of 7.1stack
Stack<typename> name;
Access to elements within a 7.2stack container
Because the stack is a last-in-first-out data structure, only top () can be used to access the top element of the stack.
St.top ();
7.3 Stack Common functions
(1) Push ()
St.push (x) put x into the stack
(2) Top ()
St.top () accesses the top element of the stack.
(3) Pop ()
St.pop () deletes the top element of the stack.
(4) Empty ()
St.empty () detects if NULL, returns true for NULL, otherwise returns FALSE.
(5) Size ()
St.size () returns the number of elements in the stack.
8.pair
You can use pair when you want to tie two elements together as a composition element, and you don't want to define a struct body.
Header file
#include <utility>
The map implementation uses the pair, so #include<map> contains #include<utility>, and the map does not need to include the utility header file.
Pair equals
struct pair{
TYPENAME1 first;
TypeName2 second;
};
Definition of 8.1 pair
Pair<typename1,typename2> name;
For example:
Pair<string,int> p;
Initialization
Pair<string,int> p ("haha", 5);
Pair<string,int> p=make_pair ("haha", 5);
8.2 Access to elements in the pair
Accessed with P.first and P.second.
8.3 Pair common functions
(1) Compare operands, pair can use ==,!=,<,<=,>,>= to compare size directly.
The comparison rule is to first the size of the standard, only when it is equal to compare the size of the second.
Common uses of 8.4 pair
For example:
#include <iostream>
#include <map>
using namespace Std;
int main () {
map<string,int>mp;
Mp.insert (Make_pair ("Heihei", 5));
Mp.insert (pair<string,int> ("haha", 10));
Auto it ==map<string,int>::iterator it
for (Auto it = Mp.begin (); It!=mp.end (); it++)
cout<<it->first<< "" <<it->second<<endl;
return 0;
}
9.algorithm header File
(1) Max ()/min ()/abs ()
Max and Min return the maximum minimum value in two numbers x, Y. ABS (x) must be an integer in X, and the floating-point number is fabs () under the Math header file.
(2) Swap ()
Swap (x, y) swaps the values of x and Y.
(3) Reverse ()
Reverse (IT,IT2) can invert an element in the array pointer between [IT1,IT2] or an element in the container's iterator [it1,it2].
For example:
int a[10]={10,11,12,13,14,15};
Reverse (a,a+4); Reverses the a[0]~a[3} 4 elements.
13 12 11 10 14 15
String str = "Abcdefghi"
Reverse (Str.begin () +2,str.begin () +6);
Abfedcghi
(4) Next_permutation ()
Next_permutation () gives the next sequence in a sequence in full order.
For example:
N=3 's Full row column
123 132 213 231 312 321
The next sequence of 231 is 312.
int a[10]={1,2,3};
bo=
cout<<a[0]<<a[1]<<a[2]<< "";
}while (Next_permutation (a,a+3));
Output: 123 132 213 231 312 321
(5) Fill ()
Fill () assigns an array or a segment of a container to an identical value, unlike Memset, where the assignment can be any value in the corresponding range of the array type.
int a[5];
Fill (a,a+5,233);
(6) Sort ()
Sort (a,a+4); Default Increment sort
CMP functions
BOOL CMP (int a,int b) {
Return a>b; The order from big to small is the opposite of priority_queue.
}
Structural body
struct node{
int x;
int y;
}
BOOL CMP (node A,node b) {
if (a.x!=b.x)
Return a.x>b.x; Press X from large to small to sort the opposite of priority_queue.
Else
Return a.y<b.y; If x is equal, it is ordered by y from small to large.
}
In STL, only vector,string,deque can be used sort, like set, Map container is implemented with red-black tree, the elements themselves are ordered, and the sort is not allowed.
(7) Lower_bound ()/upper_bound ()
Lower_bound (First,last,val) is used to find the position of the first value greater than or equal to Val in the range of [First,last] in the array or container, if it is an array, returns the position pointer, or, if it is a container, returns an iterator to that location.
Upper_bound (First,last,val) is used to find the array or container of the [First,last] range where the first value is greater than the position of the element in Val, if it is an array, returns the position pointer, or, if it is a container, returns an iterator to that location.
If the element is not found in the array or container, then Lower_bound () and Upper_bound () return a pointer or iterator where the element can be inserted.
C++stl notes