Learning video: dark horse programmers originality from 0 to 1 work | c + + tutorial introduction to programming, learning to program is no longer difficult Because may have different header files because of different containers, in order to convenient we directly use the header file:

#include <bits/stdc++.h>
Copy the code

Because some of the code is too long for the table to fully represent, I’ll use the name instead, and you can find the corresponding code below. For example, vector_1, the constructor for vector can be found below. = =

Container name vector deque stack queue
Underlying data structure Single-ended array Double side array The stack The queue
The constructor vector_1 deque_1 stack_1 queue_1
Whether traversal is allowed is is no no
The assignment operation vector_2 deque_2 stack_2 queue_2
Store operation push_back(ele);

// Insert ele data

pop_back();

// Delete the last data
push_back(elem);

// Add data at the end

push_front(elem);

// Insert data in the header

pop_back();

// Delete the last data from the container

pop_front();

// Delete the container’s first data
push(elem);

// Add an element to the top of the stack

pop();

// Remove the first element from the top

top();

// Return the top element of the stack
push(elem);

// Add elements to the end of the queue

pop();

// Remove the first element from the team head

back();

// Returns the last element

front();

Return the first element
The size of the operating vector_3

Because vector can be largeThe dynamic extension.

So there’s also a capacity point
deque_3 stack_3 queue_3
Insert and delete vector_4 deque_4 There is no There is no
Element acquisition [] or at method [] or at method There is no There is no
Other algorithms inside the container vector< int>v1;

vector< int> v2;

// Container swap

v1.swap(v2);

deque< int> d1;

deque< int> d2;

// Container swap

d1.swap(d2);
stack s1;

stack s2;

// Container swap

s1.swap(s2);
deque d1;

deque d2;

// Container swap

d1.swap(d2);
C++ standard algorithms :sort,for_each,find, etc can can can can
Container name list set map
Underlying data structure The list Binary tree

Automatic sorting and removal of duplicate elements
Binary tree

All elements arePair (key-value pair)

Duplicate key elements are not allowed
The constructor list_1 set_1 map_1
Whether traversal is allowed is is is
The assignment operation list_2 set_2 map_2
Store operation push_back(ele);

// Insert ele data

pop_back();

// Delete the last data
insert(ele);

Insert ELE data
insert(pair<int, int>(1, 10));

Insert key value pair 1:10
The size of the operating list_3 set_3 map_3
Insert and delete list_4 insert(ele);

Insert ele element

erase(ele);

Delete ele element
map_4
Element acquisition Get by traversing Get by traversing Get by traversing
Other algorithms inside the container list_5 set_4 map_5
C++ standard algorithms :sort,for_each,find, etc Can’t Can’t Can’t

1. The vector container

Vector_1: Vector constructor:

vector<int> v1; // no arguments; vector<int> v2(v1.begin(), v1.end()); // interval copy constructor; vector<int> v3(v2); // Copy the constructorCopy the code

Vector_2: Assignment operation

// Vector <int> v1; // Construct vector<int>v2 without arguments; v2 = v1; // interval copy assignment is rarely used. vector<int>v3; v3.assign(v1.begin(), v1.end());Copy the code

Vector_3: The size and capacity operations of a vector

vector<int> v1; // Check whether the container is empty v1. Empty (); // The capacity of the container v1.capacity(); V1.size (); V1.resize (2);Copy the code

Vector_4: Delete and insert operations in vector

// Insert the element before v1.begin(). 100 vector<int> v1; v1.insert(v1.begin(), 100); V1. Insert (v1.begin(), 2, 1000); V1.erase (v1.begin()); / / delete v1. The begin () to v1. The begin () + 1 elements v1. Erase (v1. The begin (), v1. The begin () + 2); / / to empty v1. The clear ();Copy the code

2. A deque container

Deque_1: Constructor of a deque

deque<int> d1; // Deque <int> d2(d1.begin(), d1.end()); // interval copy constructor; deque<int> d3(d2); // Copy the constructorCopy the code

Deque_2: assignment operation

Deque <int> d1; // deque<int>d2; d2 = d1; // interval copy assignment is rarely used. deque<int>d3; d3.assign(d1.begin(), d1.end());Copy the code

Deque_3: Size operation

deque<int> d1; // Check whether the container is empty d1.empty(); D1.size (); D1.resize (2);Copy the code

Deque_4: Deletes and inserts deques

D1.insert (d1.begin(),1000); // Insert a 1000 deque before d1.begin(). // Insert two 100 bytes at d1.begin() with no return value. D1. Insert (d1. The begin (), 2100); / / to empty d1. The clear (); D1.erase (d1.begin(),d1.end()); d1.erase(d1.begin(),d1.end()); d1.erase(d1.begin()); d1.erase(d1.begin());Copy the code

3. The stack container

Stack_1: The constructor of stack

Because only the top element of the stack can be used by the outside world, the stack cannot be traversed. The begin and end methods cannot be used, so there is no interval copy constructor.

stack<int> s1; // stack<int> s2(s1); // Copy the constructorCopy the code

Stack_2: Assignment operation

Stack <int> s1; stack<int> s1; stack<int> s2; s2=s1;Copy the code

Stack_3: Size operation

Stack <int> s1; s1.empty(); S1.size ();Copy the code

4. The queue containers

Queue_1: The constructor of queue

Since only the head and tail of a queue can be used, no traversal is allowed, neither begin nor end can be used, and there is no interval copy constructor.

queue<int> q1; Queue <int> q2(q1); // Copy the constructorCopy the code

Queue_2: Assignment operation

Queue <int> q1; Queue <int> q2; q2 = q1;Copy the code

Queue_3: size operation

Queue <int> q1; q1.empty(); Q1.size ();Copy the code

5. The list container

List_1: constructor of list

List <int> l1; List <int> l2(l1.begin(),l1.end())); // copy constructor list<int> l3(l2);Copy the code

List_2: assignment operation

List <int> l1; List <int> l2; l2 = l1; // interval copy assignment is rarely used. list<int> l3; l3.assign(l1.begin(), l1.end());Copy the code

List_3: size operation

list<int>L1; L1.empty(); L1.size(); L1.resize(10);Copy the code

List_4: insert and delete

list<int> L; / / head delete L.p op_front (); // insert list<int>::iterator it = l.biegin (); L.insert(++it, 1000); // delete it = l.bagin (); L.erase(++it); // Delete all 1000 l.emove (10000) from container; / / to empty L.c Lear ();Copy the code

List_5: Other algorithms inside the list

list<int> l1; list<int> l2; L1.swap (l2); L1.reverse (); L1.sort (); l1.sort(); Bool myCompare(int v1,int v2){return v1>v2; } l1.sort(myCompare);Copy the code

6. Set the container

Set_1: The constructor of set

// set<int> s1; // copy the constructor set<int>s2(s1);Copy the code

Set_2 :set assignment operation

set<int> s1;
set<int>s3;
s3 = s1;
Copy the code

Set_3: Size operation

set<int> s1; // Whether the container is empty s1.empty(); S1.size (); / / to empty s1. The clear ();Copy the code

Set_4: Other algorithms inside the container

set<int> s1; set<int> s2; S1. swap(s2); Int num=s1.cout(3); Set <int>::iterator pos = s1.find(30); if (pos ! = s1. The end ()) {cout < < "found elements:" < < * pos < < endl; } else {cout << "element not found" << endl; }Copy the code

7. The map container

Map_1: map constructor

// The default constructor map<int,int> m; //// copy construct map<int, int> m2(m);Copy the code

Map_2: assignment operation

map<int> m1;
map<int> m3;
m3 = m1;
Copy the code

Map_3: size operation

map<int,int> m; // Whether the container is empty map.empty(); // How many container elements m.size(); / / to empty m.c Lear ();Copy the code

Map_4: Insert and delete

map<int, int> m; Int (pair<int, int>(1, 10)); .insert(make_pair(2, 20)); .insert(map<int, int>::value_type(3, 30)); M [4] = 40; // Delete the first element m.ase (m.bigin ()); // delete 3 m.ase (3);Copy the code

Map_5: other algorithms inside the container

map<int.int>m;
map<int.int>m1;
// Container swap
m.swap(m1);

/ / to find
map<int.int>::iterator pos = m.find(3);
if(pos ! = m.end())
{
	cout << "Found element key =" << (*pos).first << " value = " << (*pos).second << endl;
}
else
{
	cout << "Element not found" << endl;
}

/ / statistics
int num = m.count(3);

Copy the code