vector<int>G[] 和 vector<vector<int> G 的区别
2016-11-13 11:17
393 查看
来自:http://stackoverflow.com/questions/28712364/difference-between-vector-int-v-and-vector-vectorint-v
stackflow 网站
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
you can use
When you want to work with a dynamic array of
you can use
2.
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::vectorelements,
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 |
11down vote | 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 |
相关文章推荐
- const vector<int>::iterator和vector<int>::const_iterator的区别
- C++中vector<int>和vector<int*>的区别
- vector<int>::size() function demo
- 2016.07.13-vector<vector<int>>应用2——Two Sum扩展
- int、string、vector<int>的转换
- vector<int>::iterator mid=(vi.begin()+vi.end())/2
- priority_queue<int,vector<int>,greater<int>>优先队列,按照从小到大
- vector使用方法,初始化vector<int> ivec( iarray,iarray+count );
- C++中的vector< vector<int> > v(m, vector<int>(n) );是什么意思
- priority_queue<int,vector<int>,greater<int>>优先队列 按照由小到大顺序
- c++ 中 const_iterator 和 const vector<>::iterator的区别
- c++中vector<int>和vector<int*>的用法比较
- 二维数组vector<vector<int> >的建立
- priority_queue<int,vector<int>,greater<int>>优先队列 按照由小到大顺序
- 关于vector的vector<vector<int>>的使用问题
- //编写程序判断一个vector<int>容器所包含的元素是否与一个list<int>容器的完全相同
- (转载)vector<int>::iterator mid=(vi.begin()+vi.end())/2
- C++ vector<int>::size_type的使用
- priority_queue<int,vector<int>,greater<int>>优先队列 按照由小到大顺序
- 编写函数,接受一对指向vector<int>的迭代器和一个int值。在两个迭代器指定的范围内查找给定的值,返回一个布尔值来指出是否找到。