从零开始学C++之标准库类型(二):vector 类简介和例程

来源:互联网 发布:vmware 教程 网络 编辑:程序博客网 时间:2024/05/29 16:55

一、标准库的vector类型

vector是同一种类型的对象的集合

vector的数据结构很像数组,能非常高效和方便地访问单个元素

vector是一个类模板(class template) 

vector不能存放引用。

template <   class Type,    class Allocator = allocator<Type> >class vector

要使用vector必须包含相关头文件
#include <vector>

using std::vector; 

vector对象的初始化:

vector类定义了好几种构造函数

vector<T>  v1; 

//vector保存类型为T的对象。默认构造函数v1为空

vector<T> v2(v1);// v2是v1的一个副本 

vector<T> v3(n, i); //v3包含n个值为i的元素 

vector<T> v4(n); //v4含有值初始化的元素的n个副本

vector<T> v5(v1.begin(), v1.end());  // iterating through v1


vector常用成员函数:



resize 和 reserve的区别:


void reserve(size_type n);

(1)如果n大于容器现有的容量(即capacity()),则需要在自由内存区为整个容器重新分配一块更大的连续空间,其大小为sizeof(T)*n,然后将容器内所有有效元素全部复制到新位置(调用拷贝构造函数),最后释放旧位置的所有存储空间并调整容器的成员指针。注意:容器的大小(即size())并没有发生改变。

(2)否则,什么也不做。


void resize(size_type n, const T& c = T());

(1)如果n大于容器当前的大小(即size()),则在容器的末尾插入n-size()个初值为c的元素,如果不指定初值,则用元素类型的默认构造函数来初始化(这可能引起内存重分配以及容器容量的扩张)。

(2)如果n小于容器当前的大小,则从容器的末尾删除size()-n 个元素,但不释放元素本身的内存空间,因此容量不变。

(3)否则,什么也不做。


e.g

vector<char> vec;
printf(”%zd %zd\n”, vec.size(), vec.capacity());
vec.resize(1024);
printf(”%zd %zd\n”, vec.size(), vec.capacity());
vec.resize(1300);
printf(”%zd %zd\n”, vec.size(), vec.capacity());

运行结果:
0 0 # 一开始size() 和capacity() 都是0
1024 1024 # resize(1024) 之后size() 和capacity() 都是1024
1300 2048 # resize(稍大) 之后capacity() 翻倍,相当于reserve(2048)


例程1:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <vector>
#include <iostream>
using namespace std;


typedef vector<int> INTVEC;

//void ShowVec(const INTVEC& v)
//{
//  unsigned int i;
//  for (i=0; i<v.size(); i++)
//  {
//      cout<<v[i]<<" ";
//  }
//  cout<<endl;
//}


//void ShowVec(INTVEC& v)
//{
//  INTVEC::iterator it;
//  for (it=v.begin(); it!=v.end(); ++it)
//  {
//      cout<<*it<<" ";
//  }
//
//  cout<<endl;
//}

void ShowVec(const INTVEC &v)
{
    INTVEC::const_iterator it;
    for (it = v.begin(); it != v.end(); ++it) //所有迭代器都重载了!=运算符,但有些没有重载<运算符。
    {
        cout << *it << " ";
    }

    cout << endl;
}

int main(void)
{
    INTVEC v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);

    ShowVec(v);

    return 0;
}

例程2:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


typedef vector<int> INTVEC;

void ShowVec(const INTVEC &v)
{
    INTVEC::const_iterator it;
    for (it = v.begin(); it != v.end(); ++it)
    {
        cout << *it << " ";
    }

    cout << endl;
}

int main(void)
{
    INTVEC v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);
    v.push_back(5);
    v.push_back(3);
    //cout<<v.back()<<endl;
    //v.pop_back();

    ShowVec(v);

    //v.erase(v.begin()+2);
    //v.erase(v.begin(), v.begin()+2);

    v.erase(remove(v.begin(), v.end(), 3), v.end());
    ShowVec(v);

    return 0;
}


例程3:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


typedef vector<int> INTVEC;

void ShowVec(const INTVEC &v)
{
    INTVEC::const_iterator it;
    for (it = v.begin(); it != v.end(); ++it)
    {
        cout << *it << " ";
    }

    cout << endl;
}

int main(void)
{
    INTVEC v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);
    v.push_back(5);
    v.push_back(3);

    ShowVec(v);

    //v.erase(remove(v.begin(), v.end(), 3), v.end());
    INTVEC::iterator it;
    for (it = v.begin(); it != v.end(); /*++it*/)
    {
        if (*it == 3)
        {
            it = v.erase(it);   // erase返回的是当前删除元素的下一个元素
        }
        else
            ++it;
    }
    ShowVec(v);

    return 0;
}



参考:

C++ primer 第四版
Effective C++ 3rd
C++编程规范


原创粉丝点击