c++ vector容器基本用法

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

    for (int i = 0; i < a.size(); i++)
    {
        cout << a[i] << "    ";
    }
    cout << endl;
}

输出结果:

 

  •  常见花式操作
#include<iostream>
#include<vector>
using namespace std;
void main()
{
    int mynum[] = {8,9,12,24,35};
    int i = 0;
    vector<int> a(mynum,mynum+5);//初始化容器,开辟10个单位空间·元素初始化为1
    for (i=0;i<a.size();i++)
    {
        cout << a[i] << "    ";
    }
    cout <<endl;
    vector<int> b(a.begin(), a.begin()+3);//借助另一容器的开始,及后面连续的n个单位
    for (i = 0; i < b.size(); i++)
    {
        cout << b[i] << "    ";
    }
    cout << endl;
    vector<int> c(&mynum[3], &mynum[5]);//以数组的第三个元素地址起,3个单位
    for (i = 0; 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(4,vector<int>(4,8));
    int i = 0;
    int j = 0;
    for (i=0;i<a.size();i++)
    {
        for (j=0;j<a[i].size();j++)
        {
            cout << a[i][j] << "   ";
        }
        cout << endl;
    }
    cin >> a[0][0];
    cin >> a[1][1];
    cin >> a[2][2];
    cin >> a[3][3];
    cout << "赋值后的语句"<<endl;
    for (i = 0; i < a.size(); i++)
    {
        for (j = 0; 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("陈培昌", 22), fgf("付高峰", 30), xxd("徐晓冬", 40), mjx("明佳新", 22);
    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("陈培昌", 22), fgf("付高峰", 30), xxd("徐晓冬", 40), mjx("明佳新", 22);
    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(1);
    v1.push_back(3);
    v1.push_back(5);
    v1.push_back(5);
    v1.push_back(7);
    v1.push_back(8);
    for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++)
    {
        cout << *it << endl;
    }
    int num = count(v1.begin(),v1.end(),5);
    cout << "5出现了" <<num<<""<< endl;
    system("pause");
}

输出结果:

posted @ 2019-12-23 10:56  saintdingtheGreat  阅读(2368)  评论(0编辑  收藏  举报