vector<int>G[] 和 vector<vector<int> G 的区别

来自:http://stackoverflow.com/questions/28712364/difference-between-vector-int-v-and-vector-vectorint-v

stackflow 网站

1. Using arrays are C-style coding, using vectors are C++-style coding.

Vectors are sequence containers representing arrays that can change in size.

Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container.


When you want to work with a fixed number of std::vector elements, you can use vector <int> V[].

When you want to work with a dynamic array of std::vector, you can use vector< vector<int> > V.


2.




One difference would be that although both can be initialized in the same way, e.g.

vector<int> V1[]        {{1,2,3}, {4,5,6}};
vector<vector<int>> V2  {{1,2,3}, {4,5,6}};

and accessed

cout << V1[0].back() << endl;
cout << V2[0].back() << endl;

the V1 can't grow. You cannot make V1.push_back(...) as its not a vector object. Its just an array. Second one is dynamic. You can grow it as you please




One difference would be that although both can be initialized in the same way, e.g.

vector<int> V1[]        {{1,2,3}, {4,5,6}};
vector<vector<int>> V2  {{1,2,3}, {4,5,6}};

and accessed

cout << V1[0].back() << endl;
cout << V2[0].back() << endl;

the V1 can't grow. You cannot make V1.push_back(...) as its not a vector object. Its just an array. Second one is dynamic. You can grow it as you please

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值