Structure Diagram#
set (集合)#
Container with internal automatic ordering and no duplicate elements
#include <set>
set<int> name;
set<double> name;
set<char> name;
set<struct node> name;
set<set<int>> name; // Note: Add a space between > >
set<int> arr[10];
Accessing Elements in the set Container#
set can only be accessed through iterators (except for STL containers other than vector and string that do not support the access method *(it+i)):
set<int>::iterator it;
set<char>::iterator it;
for (set<int>::iterator it = st.begin(); it != st.end(); it++)
{
cout << *it << endl;
}
Common Functions#
insert(value)#
st.insert('C');
find(value)#
find(value) returns the iterator corresponding to the value in the set, which is the pointer (address) of the value
cout << *(st.find(2)) << endl;
erase(it)#
st.erase(st.find(100)); // Find 100 using the find() function and then erase it using erase()
erase(iteratorBegin, iteratorEnd)#
Left-closed and right-open
st.erase(it, st.end());
size()#
cout << st.size() << endl;
vector (矢量)#
Variable-length array
#include <vector>
vector<int> name;
vector<double> name;
vector<char> name;
vector<struct node> name;
vector<vector<int>> name; // Note: Add a space between > >
In a two-dimensional array, its one-dimensional form is an address. For example:
int arr[3][2]; // Define an address with 3 rows and 2 columns
cout << arr[0] << endl; // Output the address of the first row of arr
output:
0x61fe00 // Address of the first row of arr
Accessing Elements in the vector Container#
(1) Accessing by Subscript
cout << vi[0] << endl;
(2) Accessing by Iterator
An iterator can be understood as a pointer
vector<type>::iterator variable;
vector<int>::iterator it;
vector<double>::iterator it;
vector<int>::iterator it = v.begin();
for (int i = 0; i < v.size(); i++)
{
cout << it[i] << " ";
}
vector<int>::iterator it = v.begin();
for (int i = 0; i < v.size(); i++)
{
cout << *(it + i) << " ";
}
// The iterator of vector does not support the writing of it < v.end(), so the loop condition can only be it != v.end()
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
Analysis of Common vector Functions#
(1) push_back(item)#
v.push_back(i);
(2) void pop_back()#
v.push_back(i);
(3) size()#
cout << v.size() << endl;
(4) clear()#
v.clear();
(5) insert()#
insert(__position, __x);
v.insert(v.begin() + 2, -1); // Insert -1 at the position of v[2]
(6) erase() Left-closed and right-open#
erase(__position); // Delete an element
v.erase(v.begin() + 3);
erase(__positionBegin, __positionEnd); // Delete elements in a range
v.erase(v.begin() + 1, v.begin() + 4);