首页 > 代码库 > STL vector常用API

STL vector常用API

1、容器:序列容器(时间决定)、关联式容器(容器中的数据有一定规则)

2、迭代器:通过迭代器寻找、遍历容器中的数据

vetor的使用:数据遍历与输出

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector> //向量 动态数组
#include<algorithm> //算法头文件
#include<string>
using namespace std;

void myPrint(int val){
    cout << val << " ";
}

//1. STL中的容器算法迭代器
void test01(){

    //容器
    vector<int> v;
    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40); 
    v.push_back(50);


    //获得开始迭代器
    vector<int>::iterator begin = v.begin();
    //获得结束位置迭代器
    vector<int>::iterator end = v.end();


    //遍历算法
    for_each(begin, end, myPrint);
    cout << endl;
}

//2. 容器可以存放对象
class Person{
    friend ostream& operator<<(ostream& out, Person &person);
public:
    Person(string name,int age){
        this->mName = name;
        this->mAge = age;
    }
public:
    string mName;
    int mAge;
};

ostream& operator<<(ostream& out, Person &person){
    out << "Name:" << person.mName << " Age:" << person.mAge << endl;
    return out;
}
void test02(){
    
    vector<Person> v;
    //在向容器中插入元素的时候,一定要保证元素能够被拷贝。
    v.push_back(Person("aaa", 10));
    v.push_back(Person("bbb", 20));
    v.push_back(Person("ccc", 30));
    v.push_back(Person("ddd", 40));
    v.push_back(Person("eee", 50));


    vector<Person>::iterator begin = v.begin();
    vector<Person>::iterator end = v.end();

    while (begin != end){
        cout << (*begin);
        ++begin;
    }
}

//3. 存放对象指针
void test03(){
    
    vector<Person *> v;

    //创建数据
    Person *p1 = new Person("aaa", 10);
    Person *p2 = new Person("bbb", 20);
    Person *p3 = new Person("ccc", 30);
    Person *p4 = new Person("ddd", 40);
    Person *p5 = new Person("eee", 50);

    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);


    vector<Person *>::iterator begin = v.begin();
    vector<Person *>::iterator end = v.end();

    while (begin != end){
        cout << (*begin)->mName << " " << (*begin)->mAge << endl;
        ++begin;
    }

    delete p5;
    delete p4;
    delete p3;
    delete p2;
    delete p1;

}


//4. 容器可以嵌套容器
void test04(){
    
    vector<vector<int>> vs;

    vector<int> v1;
    vector<int> v2;
    vector<int> v3;
    vector<int> v4;
    vector<int> v5;

    for (int i = 0; i < 5;i ++){
        v1.push_back(i + 10);
        v2.push_back(i + 20);
        v3.push_back(i + 30);
        v4.push_back(i + 40);
        v5.push_back(i + 50);
    }

    vs.push_back(v1);
    vs.push_back(v2);
    vs.push_back(v3);
    vs.push_back(v4);
    vs.push_back(v5);

    vector<vector<int>>::iterator begin = vs.begin();
    vector<vector<int>>::iterator end = vs.end();

    while (begin != end){
        
        vector<int>::iterator sbegin = (*begin).begin();
        vector<int>::iterator send = (*begin).end();

        while (sbegin != send){
            cout << *sbegin << " ";
            ++sbegin;
        }
        cout << endl;

        ++begin;
    }

    cout << "-------------------" << endl;

    for (vector<vector<int>>::iterator it = vs.begin(); it != vs.end(); ++it){
        for (vector<int>::iterator sit = it->begin(); sit != it->end(); ++sit){
            cout << *sit << " ";
        }
        cout << endl;
    }

}


int main(){

    //test01();
    //test02();
    //test03();
    test04();


    system("pause");
    return EXIT_SUCCESS;
}

 Vector常用API

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
using namespace std;

void printVector(const vector<int> &vec){
    for (vector<int>::const_iterator it = vec.begin(); it != vec.end(); ++it){
        cout << *it << " ";
    }
    cout << endl;
}

void printReverseVector(vector<int> &vec){
    for (vector<int>::reverse_iterator it = vec.rbegin(); it != vec.rend(); ++it){
        cout << *it << " ";
    }
    cout << endl;
}


//1. vector构造
/*
vector<T> v; //采用模板实现类实现,默认构造函数
vector(v.begin(), v.end());//将v[begin(), end())区间中的元素拷贝给本身。
vector(n, elem);//构造函数将n个elem拷贝给本身。
vector(const vector &vec);//拷贝构造函数。

//例子 使用第二个构造函数 我们可以...
int arr[] = {2,3,4,1,9};
vector<int> v1(arr, arr + sizeof(arr) / sizeof(int));
*/
void test01(){

    int arr[] = { 2, 3, 4, 1, 9 };
    vector<int> v(arr,arr+ sizeof(arr)/sizeof(int));

    printVector(v);
    printReverseVector(v);

    vector<int> v2(10, 6);
    printVector(v2);
}

//2. vector常用赋值操作
/*
assign(beg, end);//将[beg, end)区间中的数据拷贝赋值给本身。
assign(n, elem);//将n个elem拷贝赋值给本身。
vector& operator=(const vector  &vec);//重载等号操作符
swap(vec);// 将vec与本身的元素互换。
*/
void test02(){
    vector<int> v;
    v.assign(10, 6);

    
    vector<int> v2;
    v2.push_back(1);
    v2.push_back(2);
    v2.push_back(3);

    printVector(v);
    printVector(v2);

    cout << "-----------------" << endl;
    v.swap(v2);
    printVector(v);
    printVector(v2);

    //v.assign(v2.begin(),v2.end());
    //printVector(v);
}

//3. 大小操作
/*
size();//返回容器中元素的个数
empty();//判断容器是否为空
resize(int num);//重新指定容器的长度为num,若容器变长,则以默认值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。
resize(int num, elem);//重新指定容器的长度为num,若容器变长,则以elem值填充新位置。如果容器变短,则末尾超出容器长>度的元素被删除。
capacity();//容器的容量
reserve(int len);//容器预留len个元素长度,预留位置不初始化,元素不可访问。
*/

void test03(){

    //1. resize开辟空间,并且初始化
    //2. reserve只开辟空间,没有初始化
    vector<int> v2;
    v2.push_back(1);
    v2.push_back(2);
    v2.push_back(3);

    cout << "size:" << v2.size() << endl;
    v2.resize(5); //改变容器中元素的个数,多余的扔掉
    cout << "size:" << v2.size() << endl;
    printVector(v2);

    v2.reserve(20); //预留空间
    v2.push_back(10);
    printVector(v2);
    cout << "size:" << v2.size() << endl;
}

void test04(){

    vector<int> v;
    v.resize(5);
    v.push_back(10);
    cout << "capacity:" << v.capacity() << endl;
    cout << "size:" << v.size() << endl;

    cout << "----------" << endl;
    vector<int> v2;
    v2.reserve(5);
    v2.push_back(10);
    cout << "capacity:" << v2.capacity() << endl;
    cout << "size:" << v2.size() << endl;

    //cout << v2[2] << endl;
}

void test05(){
    
    vector<int> v;

    v.reserve(100000);

    int *p = NULL;
    int count = 0;
    for (int i = 0; i < 100000;i ++){
        v.push_back(i);
        if (p != &v[0]){
            p = &v[0];
            count++;
        }
    }
    cout << count << endl;
}

//swap用法
void test06(){

    vector<int> v;
    for (int i = 0; i < 100000; i++){
        v.push_back(i);
    }

    cout << "容量:" << v.capacity() << endl;
    cout << "大小:" << v.size() << endl;

    cout << "---------------" << endl;
    v.resize(10);
    cout << "容量:" << v.capacity() << endl;
    cout << "大小:" << v.size() << endl;

    cout << "---------------" << endl;
    vector<int>(v).swap(v);
    cout << "容量:" << v.capacity() << endl;
    cout << "大小:" << v.size() << endl;
}


//7. vector数据存取操作
/*
at(int idx); //返回索引idx所指的数据,如果idx越界,抛出out_of_range异常。
operator[];//返回索引idx所指的数据,越界时,运行直接报错
front();//返回容器中第一个数据元素
back();//返回容器中最后一个数据元素
*/
void test07(){


    vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);

    cout << v.front() << endl;
    cout << v.back() << endl;

    v.front() = 100;
    v.back() = 200;

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

//8. vector插入删除
/*
insert(const_iterator pos, int count,ele);//迭代器指向位置pos插入count个元素ele.
push_back(ele); //尾部插入元素ele
pop_back();//删除最后一个元素
erase(const_iterator start, const_iterator end);//删除迭代器从start到end之间的元素
erase(const_iterator pos);//删除迭代器指向的元素
clear();//删除容器中所有元素
*/
void test08(){
    
    vector<int> v;
    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);

    v.insert(v.begin()+2,100);
    printVector(v);

    v.pop_back();
    printVector(v);
    v.erase(v.begin());
    printVector(v);
    v.erase(v.begin(),v.end());
    cout << "size:" << v.size() << endl;

}


int main(){

    //test01();
    //test02();
    //test03();
    //test04();
    //test05();
    //test06();
    //test07();
    test08();

    system("pause");
    return EXIT_SUCCESS;
}

 

STL vector常用API