vector的常见用法
程序员文章站
2022-03-23 09:19:12
...
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
//push_back pop_back clear size操作
vector<int> a;
cout << "a.size() " << a.size() << endl; //0
for (int i = 0; i < 10; i++) {
a.push_back(i);
}
cout << "after push_back a.size() " << a.size() << endl; //10
a.pop_back();
cout << "after pop_back a.size() " << a.size() << endl; //9
a.clear();
cout << "after clear a.size() " << a.size() << endl; //0
//auto迭代器遍历
vector<int> c;
for (int i = 1; i <= 5; i++) {
c.push_back(i);
}
cout << "*it" << ' ';
for (auto it = c.begin(); it != c.end(); it++) {
cout << *it << ' '; //1 2 3 4 5
}
cout << endl;
//迭代器遍历
vector<int> d;
for (int i = 1; i <= 5; i++) {
d.push_back(i);
}
cout << "*k" << ' ';
for (vector<int>::iterator k = d.begin(); k != d.end(); k++) {
cout << *k << ' '; //1 2 3 4 5
}
cout << endl;
//insert erase操作
vector<int> ins;
for (int i = 1; i <= 5; i++) {
ins.push_back(i);
}
cout << "before insert: ";
for (auto i = ins.begin(); i != ins.end(); i++) {
cout << *i << ' '; //1 2 3 4 5
}
cout << endl;
cout << "after insert: ";
ins.insert(ins.begin() + 2, -1); //ins[i]和*(ins.begin()+i)是等价的
for (auto i = ins.begin(); i != ins.end(); i++) {
cout << *i << ' '; //1 2 -1 3 4 5
}
cout << endl;
ins.erase(ins.begin() + 2); //erase(it)即删除迭代器为it处的元素
cout << "after erase: ";
for (auto i = ins.begin(); i != ins.end(); i++) {
cout << *i << ' '; //1 2 3 4 5
}
cout << endl;
ins.erase(ins.begin()+1, ins.begin()+4); //erase(first,last)即删除左闭右开内所有元素
cout << "after erase from 1 to 4 : ";
for (auto i = ins.begin(); i != ins.end(); i++) {
cout << *i << ' '; //1 5
}
cout << endl;
//reverse
vector<int>rev;
for (int i = 1; i <= 5; i++) {
rev.push_back(i);
}
for (auto st = rev.begin(); st != rev.end(); st++) {
cout << *st << ' '; //1 2 3 4 5
}
cout << endl;
reverse(rev.begin(),rev.end());
for (auto st = rev.begin(); st != rev.end(); st++) {
cout << *st << ' '; //5 4 3 2 1
}
cout << endl;
//unique erase sort
//返回去重之后的尾迭代器,仍然为前闭后开,即这个迭代器是去重之后末尾元素的下一个位置。
//该函数用于离散化,利用迭代器的减法,可计算出去重后的元素个数。
//unique函数属于STL中比较常用函数,它的功能是元素去重。即”删除”序列中所有相邻的重复元素(只保留一个)。
//此处的删除,并不是真的删除,而是指重复元素的位置被不重复的元素给占领了。
//由于它”删除”的是相邻的重复元素,所以在使用unique函数之前,一般都会将目标序列进行排序。
vector<int> uni;
for (int i = 1; i <= 5; i++) {
uni.push_back(i);
uni.push_back(i);
}
for (auto st = uni.begin(); st != uni.end(); st++) {
cout << *st << ' '; //1 1 2 2 3 3 4 4 5 5
}
cout << endl;
int m = unique(uni.begin(), uni.end()) - uni.begin(); //去重后元素的个数
cout << m << endl; //5
//把一个vector去重,多余的删掉
vector<int> uniq; //初始化uniq
for (int i = 1; i <= 5; i++) {
uniq.push_back(i);
uniq.push_back(i);
}
uniq.erase(unique(uniq.begin(), uniq.end()), uniq.end());
for (auto st = uniq.begin(); st != uniq.end(); st++) {
cout << *st << ' '; //1 2 3 4 5
}
cout << endl;
//sort+unique+erase
vector<int> sor({ 4,4,5,6,7,3,4,3,4,3,5,3,5,5,6,2,2,1,3,4,5,6,6 });
sort(sor.begin(), sor.end());
sor.erase(unique(sor.begin(), sor.end()), sor.end());
for (auto st = sor.begin(); st != sor.end(); st++) {
cout << *st << ' '; //1 2 3 4 5 6 7
}
//有些场合需要根据一些条件把部分数据输出在同一行,数据中间用空格隔开。
//由于输出数据的个数是不确定的,为了更方便地处理最后一个满足条件的数据后面
//不输出额外的空格,可以先用vector记录所有需要输出的数据,然后一次性输出
return 0;
}
上一篇: 数据结构--错题集(-日更-)