1. Static array array
Static array (array), is a commonly used array, which belongs to the linear data structure, and the assignment directly uses the equal sign
a[1] = 3;
Define a static array as follows:
int a[空间];
Among them, the space is around 60000000
We can treat the array as a table
The array starts from position 0. When the array subscript<0, a RuntimeError will occur
This is a 1D array, followed by a 2D array
int a[空间][空间];
Among them, the space² is around 60000000
Expand the table size:
Let's take an example of an array
prefix and
One-dimensional prefix sums:
for (int i = 1; i <= n; i++)
d[i] = d[i - 1] + a[i];
When i = 1, the subscript of d[i - 1] in the statement is 0
If i starts from 0, it will RE.
2、vector
vector is similar to a static array, but you need to use push_back to push
Compared with array, vector does not need user assignment, and when a value is pushed by default, vector stores it into 0
#include <vector> //vector头文件
...
vector<存储的东西的类型> v;
...
cin >> n;
v.push_back(n);
cout << v[0];
Three, queue queue
queue queue
like queuing for a turtle
The queue uses push to push, and pop to pop up
define it:
queue<int> q;
push() inserts an element at the end of the queue
q.push(3);
pop() pops the first element
q.pop();
size() returns the number of elements
int t = q.size();
empty() is empty, true if empty
if (q.empty()) {
...
}
front() returns the first element
int t = q.front();
back() returns the last element
int t = q.back();
Don't forget the header file:
#include <queue>
Four, priority queue priority_queue
priority_queue adds a default sorting function on the basis of queue
By default the larger one is at the top:
priority_queue<int> q;
Special definition small at the top:
priority_queue<int, vector<int>, greater<int> > q;
Five, stack stack
Stack stack is usually mentioned together because they are very similar. The queue is first in, first out, which is fair, but the stack is very unfair. It is first in, first out (first in, first out?).
We can use a tunnel sealed at one end (the queue is a normal tunnel) to see it
this is in and out
head File:
#include <stack>
Define the stack:
stack<int> s;
The stack and queue manipulation statements are almost the same.
Six, map
map, a particularly powerful data structure
Talk about its storage:
1. When the number to be stored is within the range of int, map can realize
2. The subscript of the map can be of any type
!!!!!
Map traversal needs to use iterator
head File:
#include <map>
Define map:
There are two types in angle brackets when defining a map, the first is the subscript type, and the second is the type of the stored thing.
map<int, int> m;
//本map指int类型的下标存储着int类型(array中表示a[1] = 1)
map<string, double> m;
//本map指string类型的下标存储着double类型
(array中表示a["string"] = 1.1,在array中不可实现)
Use it to traverse:
for (map<类型,类型>::iterator it = m.begin(); it != m.end(); it++) {
...
}
Seven, set
set, slightly lower than map, also stores various types
#include <set> //头文件
using namespace std;
set<int> s;
int main () {
s.insert(1); //set使用插入
for (set<int>::iterator it = ...) {
...
}
return 0;
}
Of course, there are other data structures, such as linked lists, etc., you can check them yourself
These header files can be replaced by a header file, which is the universal header file
#include <bits/stdc++.h>