算法中的STL(1)
2022.03.14
为了准备蓝桥杯最近学习了算法中的部分知识,今天学习了C++中的STL模板库。
今天学习的内容有vector容器,其中常用的函数有
push_back():在容器的末尾添加一个数据
pop_back():弹出容器中一个数据
size():返回容器的大小
clear():清空容器
insert():在指定位置添加数据
erase():删除指定位置的数据
下面是两种vector容器的输出方式:
vector<int> num;
int m;
cin >> m;
for (int i = 0; i < m; i++) {
int temp;
cin >> temp;
num.push_back(temp);
}
//vector的第一种输出方式num[].
for (int i = 0; i < num.size(); i++) {
int temp = num[i];
cout << temp << endl;
}
sort(num.begin(), num.end());
for (int i = 0; i < num.size(); i++) {
int temp = num[i];
cout << temp << " 1" << endl;
}
//迭代器的输出方式iterator
vector<int>::iterator it;
for (it = num.begin(); it != num.end(); it++) {
cout << *it << " 2" << endl;
}
其中的迭代器相当与C++中的指针。
浙公网安备 33010602011771号