c++ vector容器基本用法

2023-06-13,,

基本用法

#include<iostream>
#include<vector>
using namespace std;
void main()
{
vector<int> a(,);//初始化容器,开辟10个单位空间·元素初始化为1
int i;
cout << "初始化变量" << endl;
for (int i=;i<a.size();i++)
{
cout << a[i] << " ";
}
cout << "插入数据" << endl;
cin >> a[];
cin>> a[];
cin >> a[];
cout << "赋值之后的变量" << endl; for (int i = ; i < a.size(); i++)
{
cout << a[i] << " ";
}
cout << endl;
}

输出结果:

常见花式操作

#include<iostream>
#include<vector>
using namespace std;
void main()
{
int mynum[] = {,,,,};
int i = ;
vector<int> a(mynum,mynum+);//初始化容器,开辟10个单位空间·元素初始化为1
for (i=;i<a.size();i++)
{
cout << a[i] << " ";
}
cout <<endl;
vector<int> b(a.begin(), a.begin()+);//借助另一容器的开始,及后面连续的n个单位
for (i = ; i < b.size(); i++)
{
cout << b[i] << " ";
}
cout << endl;
vector<int> c(&mynum[], &mynum[]);//以数组的第三个元素地址起,3个单位
for (i = ; i < c.size(); i++)
{
cout << c[i] << " ";
}
}

输出结果:

二维数组vector<vector<int>>a(4,vector<int>(4,8))

#include<iostream>
#include<vector>
using namespace std;
void main()
{
//用vector声明一个4*4的矩阵
vector<vector <int>>a(,vector<int>(,));
int i = ;
int j = ;
for (i=;i<a.size();i++)
{
for (j=;j<a[i].size();j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
cin >> a[][];
cin >> a[][];
cin >> a[][];
cin >> a[][];
cout << "赋值后的语句"<<endl;
for (i = ; i < a.size(); i++)
{
for (j = ; j < a[i].size(); j++)
{
cout << a[i][j] << " ";
}
cout << endl;
} }

输出结果:

用vector容器盛放一个类

#include<iostream>
#include<string>
#include<vector>
using namespace std;
class mycoach
{
public:
friend ostream &operator<<(ostream &out, mycoach &t);
mycoach(string name,int age)
{
this->name = name;
this->age = age;
}
~mycoach()
{
//cout << "回中式宿舍休息去了" << endl;
}
private:
string name;
int age;
}; ostream &operator<<(ostream &out,mycoach &t)
{
out<< t.name << "......" << t.age << endl;
return out;
}
void main()
{
vector<mycoach> v1;
mycoach cpc("陈培昌", ), fgf("付高峰", ), xxd("徐晓冬", ), mjx("明佳新", );
v1.push_back(cpc);//把类对象压入vector容器
v1.push_back(fgf);
v1.push_back(xxd);
v1.push_back(mjx);
for (vector<mycoach>::iterator it= v1.begin(); it!=v1.end(); it++)
{
cout << *it << endl;
} }

步骤一:声明vector变量v1

步骤二:通过迭代器循环遍历vector容器,for(vector<类型名>::iterator it(迭代器变量名) =v1.begin(); it!=v1.end();it++)

输出结果:

把指针装入vector容器

#include<iostream>
#include<string>
#include<vector>
using namespace std;
class mycoach
{
public:
friend ostream &operator<<(ostream &out, mycoach &t);
mycoach(string name,int age)
{
this->name = name;
this->age = age;
}
mycoach(const mycoach &t)
{
this->name = t.name;
this->age = t.age;
} string name;
int age;
}; ostream &operator<<(ostream &out, mycoach &t)
{
out << t.name << "......" << t.age << endl;
return out;
} void main()
{
mycoach cpc("陈培昌", ), fgf("付高峰", ), xxd("徐晓冬", ), mjx("明佳新", );
mycoach *m1,*m2, *m3, *m4;
m1 = &cpc;
m2 = &fgf;
m3 = &xxd;
m4 = &mjx;
vector<mycoach *> v1;
v1.push_back(m1);
v1.push_back(m2);
v1.push_back(m3);
v1.push_back(m4);
for (vector<mycoach *>::iterator it=v1.begin();it!=v1.end();it++)
{
cout << (*it)->name << endl;//注意!把属性声明为public,否则无法通过指针直接访问到
cout << (**it);
}
}

输出结果:

查询某一元素在容器中出现的次数

void main()
{
vector<int> v1;
v1.push_back();
v1.push_back();
v1.push_back();
v1.push_back();
v1.push_back();
v1.push_back();
for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++)
{
cout << *it << endl;
}
int num = count(v1.begin(),v1.end(),);
cout << "5出现了" <<num<<"次"<< endl;
system("pause");
}

输出结果:

c++ vector容器基本用法的相关教程结束。

《c++ vector容器基本用法.doc》

下载本文的Word格式文档,以方便收藏与打印。