vector容器
注意:所谓动态增加大小,并不是在原空间之后续接新空间(因为无法保证原空间之后尚有可配置的空间),而是一块更大的内存空间,然后将原数据拷贝新空间,并释放原空间。因此,对vector的任何操作,一旦引起空间的重新配置,指向原vector的所有迭代器就都失效了。
常用API操作
#include <iostream>
#include <vector>
using namespace std;
void test01()
{
vector<int> v;
for (int i = 0;i<10;i++)
{
v.push_back(i);
cout << v.capacity() << endl; //容器的容量
}
}
void test02()
{
vector<int>v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
cout << "v.front()" << v.front() << endl; //10
cout << "v.back()" << v.back() << endl; //40
//插入,注意,索引处是一个迭代器
v.insert(v.begin(),100);
for (vector<int>::iterator it = v.begin();it != v.end();it++)
{
cout << *it << endl;
}
//删除
v.erase(v.begin()); //删除首元素
v.erase(v.begin(), v.end()); //删除所有元素
v.clear(); //删除所有元素
}
//逆序遍历
void test03()
{
vector<int>v;
for (int i =0;i<10;i++)
{
v.push_back(i);
}
//逆序迭代器
for (vector<int>::reverse_iterator it = v.rbegin();it != v.rend();it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test03();
return 0;
}
扩展:
count 函数:返回元素值为 target 的元素个数。
count_if 函数:返回符合一定条件的元素个数。compare () 函数是自定义的,返回值是 true 就是表示符合要求。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int>v{1,2,3,4,5,6,7,8,9,10};
int target1 = 3,target2 = 5;
int num_items1 = count(v.begin(),v.end(),target1); //返回target1出现的次数
int num_items2 = count(v.begin(), v.end(), target2); //返回target2出现的次数
cout << "target1 num is:" << num_items1 << endl;
cout << "target2 num is:" << num_items2 << endl;
//计算能被3整除的元素数
int num_items3 = count_if(v.begin(), v.end(), [](int i) {return i % 3 == 0;});
return 0;
}
deque容器
#include <iostream>
#include <deque>
using namespace std;
void myPrint(deque<int> &q)
{
//只读迭代器
for (deque<int>::const_iterator it = q.begin();it != q.end();it++)
{
//*it = 100; 不可以修改
cout << *it << " ";
}
cout << endl;
}
void test01()
{
deque<int>q;
q.push_back(10);
q.push_back(20);
q.push_back(30);
q.push_back(40);
myPrint(q);
deque<int>q2(q.begin(), q.end());
q2.push_back(1000);
myPrint(q2);
//交换
q.swap(q2);
myPrint(q2);
}
//插入、删除
void test02()
{
deque<int>q;
q.push_back(10);
q.push_back(20);
q.push_back(30);
q.push_back(40);
q.pop_front(); //头删
q.pop_back(); //尾删
}
int main()
{
test01();
return 0;
}
案例
有5名选手:选手ABCDE,10个评委分别对每一名选手打分,去除最高分,去除评委中最低分,取平均分J
1.创建五名选手,放到vector中。
2.遍历vector容器,取出来每一个选手,执行for循环,可以把10个评分打分存到deque容器中
3. sort算法对deque容器中分数排序,pop_back pop_front去除最高和最低分v
4.deque容器遍历一遍,累加分数,累加分数/d.size()w
5. person.score =平均分·
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <algorithm>
#include <ctime>
using namespace std;
class Person {
public:
Person(string name,int score)
{
this->m_name = name;
this->m_score = score;
}
public:
string m_name;
int m_score;
};
void createPerson(vector<Person> &v)
{
string nameSeed = "ABCDE";
for (int i = 0;i<5;i++)
{
string name = "选手";
name += nameSeed[i];
int score = 0;
Person p(name,score);
v.push_back(p);
}
}
void setScore(vector<Person> &v)
{
for (vector<Person>::iterator it = v.begin();it != v.end();it++)
{
//对5个人进行打分
deque<int>d; //存储10位评委的分数
for (int i =0;i<10;i++)
{
//rand () 产生的随机数在每次运行的时候都是与上一次相同的
//若要不同,用函数 srand () 初始化它
int score = rand() % 41 + 60; //60~100
d.push_back(score);
}
//排序
sort(d.begin(), d.end());
//去除最高和最低
d.pop_front();
d.pop_back();
//求平均分
int sum = 0;
for (deque<int>::iterator it = d.begin(); it != d.end(); it++)
{
sum += *it;
}
int ave = (int)(sum / d.size());
(*it).m_score = ave;
}
}
void showScore(vector<Person> &v)
{
//测试
for (vector<Person>::const_iterator it = v.begin(); it != v.end(); it++)
{
cout << "姓名:" << (*it).m_name << "分数:" << it->m_score << endl;
}
}
int main()
{
//设置随机数种子,让rand每次运行产生不同的随机数
srand((unsigned int)time(NULL));
//创建5名选手
vector<Person> v;
createPerson(v);
showScore(v);
//打分
setScore(v);
showScore(v);
return 0;
}