C++ vector容器的练习大全(增删查改,获取,长度,指定元素,交换,拷贝,插入,预留空间,容量是否为空,自定义等等)


#include 
#include 
#include 
using namespace std;
void fun01()
{
//    vectorvec;
//    vectorvec(5,4);
    int nums[]={1,2,3,4,5,6};
    vectorvec(nums,nums+5);
    vector::iterator it = vec.begin();
    while(it != vec.end())
    {
        cout << *it <vec2(vec);
    cout << "vector长度:"<< vec2.size() << endl;
    cout << "vector的值是" << vec2.at(5) << endl;
    vector::iterator it2 = vec2.begin();
    for(;it2 != vec2.end();it2++)
    {
        cout << *it2 << endl;
    }
    cout << endl;

}
void fun02()
{
  int nums[5]  = {1,3,5,7,9};
//  vector v1;
  vector v1(nums,nums+5);
  vector v2;
  /*v2.assign(v1.begin(),v1.end());*///拷贝元素


//  cout << v1.at(4) << endl;
//  cout << v2.at(4) << endl;
  v2.assign(6,2);
//  v2 = v1;
  v2.swap(v1);//交换
  vector::iterator it1 = v1.begin();
  for(;it1 != v1.end();it1++)
  {
      cout << *it1 << endl;
  }
  cout << endl;

  vector::iterator it2 = v2.begin();
  for(;it2 != v2.end();it2++)
  {
      cout << *it2 << endl;
  }
  cout << endl;

}
void print(vector &v)
{
    vector::iterator it = v.begin();
    for(;it != v.end();it++)
    {
        cout << *it << endl;
    }
    cout << endl;
}

void fun03()
{
    vectorv1;
    v1.push_back(2);
    v1.push_back(4);
    v1.push_back(6);
    v1.push_back(8);
    v1.push_back(10);
    v1.pop_back();//删除最后一个
    print(v1);
    v1.insert(v1.begin(),1,1);//第一个位置插入1
    print(v1);
    v1.erase(v1.begin(),v1.begin()+3);//删除一到三
    print(v1);
    v1.erase(v1.begin()+1);//删除第二个
    print(v1);
    v1.clear();
    print(v1);
    v1.push_back(2);
    v1.push_back(4);
    v1.push_back(6);
    v1.push_back(8);
    v1.push_back(10);
    cout << v1.at(3) << endl;
    cout << v1[2] << endl;
    cout << v1.front() << endl;
    cout << v1.back() << endl;
}
void fun04()//大小相关
{
    vectorv2;
//    vectorv2(2.2);
    v2.reserve(10);//预留空间
    cout << v2.size() << endl;

    cout << v2.empty() << endl;//是否为空//0代表不为空,非0为空
    cout << v2.capacity() << endl;//容量大小
    cout << "---------------------" << endl;

    v2.resize(4);
    cout << v2.size() << endl;
    cout << v2.empty() << endl;//是否为空//0代表不为空,非0为空
    cout << v2.capacity() << endl;//容量大小
    cout << "---------------------" << endl;

    v2.resize(5,8);
    cout << v2.size() << endl;
    cout << v2.empty() << endl;//是否为空//0代表不为空,非0为空
    cout << v2.capacity() << endl;//容量大小
//    vector::iterator it = v2.begin();
//    for(;it != v2.end();it++)
//    {
//        cout << *it << endl;
//    }
//    cout << endl;
    cout << "---------------------" << endl;
    v2.reserve(3);//预留空间
    cout << v2.size() << endl;
    cout << v2.empty() << endl;//是否为空//0代表不为空,非0为空
    cout << v2.capacity() << endl;//容量大小
    cout << "---------------------" << endl;
}


//存放自定义对象
class person{
    friend void printvectorperson(vector &v);
private:

    string name;
    int num;
public:
    person(){}
    person(string name,int age)
    {
        this->name = name;
        this->num = num;
    }
};
void printvectorperson(vector &v)
{
    vector::iterator it = v.begin();
    for(;it!=v.end();it++)
    {

        cout << (*it).name << " " << (*it).num << endl;
    }
}
void fun05()
{
    vector vec;
    vec.push_back(person("张三",18));
    vec.push_back(person("张三2",18));
    vec.push_back(person("张三3",18));
    vec.push_back(person("张三4",18));
    printvectorperson(vec);
}
void fun06()
{
    vector v1;
    v1.push_back(10);
    v1.push_back(20);
    v1.push_back(30);
    v1.push_back(40);
    v1.push_back(50);
    vector v2;
    v2.push_back(100);
    v2.push_back(200);
    v2.push_back(300);
    v2.push_back(400);
    v2.push_back(500);
    vector v3;
    v3.push_back(1000);
    v3.push_back(2000);
    v3.push_back(3000);
    v3.push_back(4000);
    v3.push_back(5000);

    vector> v;
    v.push_back(v1);
    v.push_back(v2);
    v.push_back(v3);

    vector>::iterator it=v.begin();
    for(;it!=v.end();it++)
    {
    //*it == vector
        vector::iterator mit=(*it).begin();
        for(;mit!=(*it).end(); mit++)
        {
            //*mit==int
            cout<<*mit<<" ";
        }
        cout<

提示:很全很全很全,看了就会

你可能感兴趣的:(c++,c++,开发语言)