C++ STL vector

转载请注明: http://blog.csdn.net/c602273091/article/details/50722709

introduction

vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于一个动态的数组,当程序员无法知道自己需要的数组的规模多大时,用其来解决问题可以达到最大节约空间的目的。

#include<vector>

one dimension:

vector <int> a

two dimension:

vector <int *> a

three dimension:

vector <int**>a
vector <int *> a

int b = 5;

a.push_back(b);//该函数下面有详解

cout<<a[0];       //输出结果为5

http://blog.csdn.net/hancunai0017/article/details/7032383

function

1.push_back 在数组的最后添加一个数据
2.pop_back 去掉数组的最后一个数据
3.at 得到编号位置的数据
4.begin 得到数组头的指针
5.end 得到数组的最后一个单元+1的指针
6.front 得到数组头的引用
7.back 得到数组的最后一个单元的引用
8.max_size 得到vector最大可以是多大
9.capacity 当前vector分配的大小
10.size 当前使用数据的大小
11.resize 改变当前使用数据的大小,如果它比当前使用的大,者填充默认值
12.reserve 改变当前vecotr所分配空间的大小
13.erase 删除指针指向的数据项
14.clear 清空当前的vector
15.rbegin 将vector反转后的开始指针返回(其实就是原来的end-1)
16.rend 将vector反转构的结束指针返回(其实就是原来的begin-1)
17.empty 判断vector是否为空
18.swap 与另一个vector交换数据

vector<int> c.

c.clear() 移除容器中所有数据。

c.empty() 判断容器是否为空。

c.erase(pos) 删除pos位置的数据

c.erase(beg,end) 删除[beg,end)区间的数据

c.front() 传回第一个数据。

c.insert(pos,elem) 在pos位置插入一个elem拷贝

c.pop_back() 删除最后一个数据。

c.push_back(elem) 在尾部加入一个数据。

c.resize(num) 重新设置该容器的大小

c.size() 回容器中实际数据的个数。

c.begin() 返回指向容器第一个元素的迭代器

c.end() 返回指向容器最后一个元素的迭代器

memory management

使用reserve()函数提前设定容量大小,避免多次容量扩充操作导致效率低下。

    关于STL容器,最令人称赞的特性之一就是是只要不超过它们的最大大小,它们就可以自动增长到足以容纳你放进去的数据。(要知道这个最大值,只要调用名叫max_size的成员函数。)对于vector和string,如果需要更多空间,就以类似realloc的思想来增长大小。vector容器支持随机访问,因此为了提高效率,它内部使用动态数组的方式实现的。在通过 reserve() 来申请特定大小的时候总是按指数边界来增大其内部缓冲区。当进行insert或push_back等增加元素的操作时,如果此时动态数组的内存不够用,就要动态的重新分配当前大小的1.5~2倍的新内存区,再把原数组的内容复制过去。所以,在一般情况下,其访问速度同一般数组,只有在重新分配发生时,其性能才会下降。正如上面的代码告诉你的那样。而进行pop_back操作时,capacity并不会因为vector容器里的元素减少而有所下降,还会维持操作之前的大小。对于vector容器来说,如果有大量的数据需要进行push_back,应当使用reserve()函数提前设定其容量大小,否则会出现许多次容量扩充操作,导致效率低下。

example

1 基本操作

(1)头文件#include.

(2)创建vector对象,vector vec;

(3)尾部插入数字:vec.push_back(a);

(4)使用下标访问元素,cout<

 std::vector<std::vector<TComDataCU*> > m_vSliceCUDataLink;

(1) 使用reverse将元素翻转:需要头文件#include< algorithm>

reverse(vec.begin(),vec.end());将元素翻转(在vector中,如果一个函数中需要两个迭代器,

一般后一个都不包含.)

(2)使用sort排序:需要头文件#include< algorithm>,

sort(vec.begin(),vec.end());(默认是按升序排列,即从小到大).

可以通过重写排序比较函数按照降序比较,如下:

定义排序比较函数:

bool Comp(const int &a,const int &b)
{
    return a>b;
}

调用时:sort(vec.begin(),vec.end(),Comp),这样就降序排序。

http://www.cnblogs.com/wang7/archive/2012/04/27/2474138.html

high level using vector < vector< int>>

vector < vector< int>>,结果出错了。原因是c++有>>的运算符,编译器把vector< vector< int>>的最后两个字符解析成了>>运算符,当中加一个空格后就没有问题了。

vector<vector<int> > matrix = {
    { 8, 1, 6 },
    { 3, 5, 7 },
    { 4, 9, 2 }
};
// C++
int A[][3] = {
    { 8, 1, 6 },
    { 3, 5, 7 },
    { 4, 9, 2 }
};
vector<vector<int> > matrix (3);
matrix[0].assign(A[0], A[0]+3);
matrix[1].assign(A[1], A[1]+3);
matrix[2].assign(A[2], A[2]+3);

Go through the vector like array:

int main(){
    std::vector< vector<int> > ph;

    std::vector<int> p;
    p.push_back(1);
    p.push_back(2);
    p.push_back(3);

    std::vector<int> q;
    q.push_back(10);
    q.push_back(20);
    q.push_back(30);
    q.push_back(40);

    ph.push_back(p);
    ph.push_back(q);

    for (std::vector< std::vector<int> >::size_type u = 0; u < ph.size(); u++) {
        for (std::vector<int>::size_type v = 0; v < ph[u].size(); v++) {
            std::cout << ph[u][v] << " ";
        }
        std::cout << endl;
    }

    return 0;
}

get two dimensions array:

#include <iostream>
  #include <vector>
  using namespace std;
  void main()
  {
      vector< vector<int> > array(3);
      for(int i=0;i<3;i++)
        array[i].resize(3);//设置数组的大小3X3
         //现在你可以和使用数组一样使用这个vector
      for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
          array[i][j]=(i*j);
      //输出
      for(int i=0;i<3;i++)
      {
         for(int j=0;j<3;j++)
           cout<<array[i][j]<<" ";
         cout<<endl;
       }
     array.resize(5);
     arry[3].resize(3);
     arry[4].resize(3);
      //现在是5X3的数组了
      for(int i=0;i<5;i++)
         for(int j=0;j<3;j++)
            array[i][j]=(i*j);
      for(int i=0;i<5;i++)
      {
         for(int j=0;j<3;j++)
            cout<<array[i][j]<<" ";
        cout<<endl;
      }
  }

another way to operate by []

typedef vector<int> vec_int;
// declare a 2-dimension "array" - matrix
vector<vec_int> mtr_int;

int N(10),M(10);  // the number of dimension.
int nDefault;  // default value for all elements in the matrix
// if you want to get a N*M matrix
vec_int vec_tmp;  // temporary 1-dimension "array"
for(int i = 0; i < N; ++i)
{
  vec_tmp.assign(M, nDefault);
  mtr_int.push_back(vec_tmp);
}

// now you can access the matrix with operator "[]"
// like generay 2-dimension array
// for example
...;
mtr_int[2][3] = 100;

go through vector i different ways:

方法一,for循环迭代器输出:(c++ 98/03 ,c++ 11 通用)

void ShowVec(const vector<int>& valList)
{
    for (vector<int>::const_iterator iter = valList.cbegin(); iter != valList.cend(); iter++)
    {
        cout << (*iter) << endl;
    }
}

方法二,与方法一差不多,不过能少打几个字:(仅c++ 11 可用)
void ShowVec(const vector<int>& valList)
{
    for (auto iter = valList.cbegin(); iter != valList.cend(); iter++)
    {
        cout << (*iter) << endl;
    }
}

方法三,for_each加函数:(c++ 98/03 ,c++ 11 通用)
// headfile:algorithm、functional
template<typename T>
void printer(const T& val)
{
    cout << val << endl;
}
void ShowVec(const vector<int>& valList)
{
    for_each(valList.cbegin(), valList.cend(), printer<int>);
}

方法四,for_each加仿函数:(c++ 98/03 ,c++ 11 通用)
template<typename T>
struct functor
{
    void operator()(const T& obj)
    {
        cout << obj << endl;
    }
};
void ShowVec(const vector<int>& valList)
{
    for_each(valList.cbegin(), valList.cend(), functor<int>());
}

方法五,for_each加Lambda函数:(仅c++ 11 可用)
void ShowVec(const vector<int>& valList)
{
    for_each(valList.cbegin(), valList.cend(), [](const int& val)->void{cout << val << endl; });
}

方法六,for区间遍历:(仅c++ 11 可用)
for (auto val : valList)
{
    cout << val << endl;
}
// functional:
// http://blog.csdn.net/wanglang1000/article/details/12057327
// http://www.2cto.com/kf/201404/291689.html
std::vector<std::vector<TComDataCU*> > m_vSliceCUDataLink;
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值