public member function
<vector>
std::vector::resize
void resize (size_type n);
void resize (size_type n, const value_type& val);
Change size
Resizes the container so that it containsnelements.
调整容器使其包含n个元素。
Ifnis smaller than the current containersize, the content is reduced to its firstnelements,
removing those beyond (and destroying them).
如果n小于当前的容器大小(通过size获得,而不是capacity),那么将会只是保留前n个元素,移除多余的元素(并且销毁他们)
例子:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vi={1,2,3,4,5,6,7};
int mcapacity=vi.capacity();
int msize=vi.size();
cout<<"size="<<msize<<",capcity="<<mcapacity<<endl;
vi.resize(3);
cout<<"after resize(3):"<<endl;
for(auto it=vi.begin();it!=vi.end();++it)
cout<<*it<<endl;
cout<<"size="<<msize<<",capcity="<<mcapacity<<endl;
cout<<"try to access vi[5] now,vi[5]="<<vi[5]<<endl;
}
运行结果:
可以看到,resize(3)之后,只保留了前3个元素,其容量和大小都没有改变.
但是发现vi[5]还是可以访问并且得到正确的数据,并没有销毁里面的数据,这里可能是编译器的实现问题。(这样个人觉得可能会导致数据泄露,使用时需要注意)
Ifnis greater than the current containersize, the content is expanded by inserting
at the end as many elements as needed to reach a size ofn. Ifvalis specified, the new elements are initialized as copies ofval, otherwise, they are value-initialized.
如果n大于当前容器的大小,那么容器将会从尾部开始增长至大小n,如果指定了val的值,那么新增长的元素的值将会被初始化为val的一个拷贝,否则将会时元素值类型的默认值。
例子:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vi={1,2,3};
cout<<"at first ,vi:"<<endl;
for(auto it=vi.begin();it!=vi.end();++it)
cout<<*it<<" ";
cout<<endl;
vi.resize(7);
cout<<"after resize ,vi:"<<endl;
for(auto it=vi.begin();it!=vi.end();++it)
cout<<*it<<" ";
cout<<endl;
}
可以看到,如果不知道val的值,那么将会是int类型的默认值0!
下面使指定val为999的情况。即vi.resize(7,999);
Ifnis also greater than the current containercapacity, an automatic reallocation
of the allocated storage space takes place.
如果n大于当前容器的容量(通过capacity获得),那么容器将会自动重分配空间以适应其增长。
例子:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vi={1,2,3};
cout<<"size="<<vi.size()<<" ,capacity="<<vi.capacity()<<endl;
cout<<"at first ,vi:"<<endl;
for(auto it=vi.begin();it!=vi.end();++it)
cout<<*it<<" ";
cout<<endl;
vi.resize(30,999);
cout<<"size="<<vi.size()<<" ,capacity="<<vi.capacity()<<endl;
cout<<"after resize ,vi:"<<endl;
for(auto it=vi.begin();it!=vi.end();++it)
cout<<*it<<" ";
cout<<endl;
}
结果:
Notice that this function changes the actual content of the container by inserting or erasing elements from it.
需要注意的是如果增长或者截断数组会改变其实际的内容.(修改size以及capacity)。
下面使我测试的例子:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vi={1,2,3,5,6,7,8,9,10,11,12};
cout<<"vi adress="<<&vi<<endl;
cout<<"size="<<vi.size()<<" ,capacity="<<vi.capacity()<<endl;
vi.resize(1000);
cout<<"vi adress="<<&vi<<endl;
cout<<"size="<<vi.size()<<" ,capacity="<<vi.capacity()<<endl;
vi.resize(2);
cout<<"vi adress="<<&vi<<endl;
cout<<"size="<<vi.size()<<" ,capacity="<<vi.capacity()<<endl;
}
结果:
Parameters
参数
n
New container size, expressed in number of elements.
Member typesize_typeis an unsigned integral type.
新容器的大小,表现为元素的个数
n是一个无符号整型.
val
Object whose content is copied to the added elements in case thatnis greater than the current containersize.
If not specified, the default constructor is used instead.
Member typevalue_typeis the type of the elements in the container, defined invectoras
an alias of the first template parameter (T).
val为当n大于当前容器大小需要增长时,新的元素的值为val的拷贝。
如果不特别指定,那么将使用元素类型的默认构造器构造新的元素。
其类型由vector的第一个模版参数指定。
Return Value
none
If a reallocation happens, the storage is allocated using the container'sallocator, which may throw exceptions on
failure (for the defaultallocator,bad_allocis thrown if the allocation request does not succeed).
无返回值。
如果发生重分配,那么使用容器的分配器的过程中如果分配失败可能会发生异常(例如对于默认分配器allocator,当分配不成功使会抛出bad_alloc异常)。
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
// resizing vector
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myvector;
// set some initial content:
for (int i=1;i<10;i++) myvector.push_back(i);
myvector.resize(5);
myvector.resize(8,100);
myvector.resize(12);
std::cout << "myvector contains:";
for (int i=0;i<myvector.size();i++)
std::cout << ' ' << myvector[i];
std::cout << '\n';
return 0;
}
|
|
Output:
myvector contains: 1 2 3 4 5 100 100 100 0 0 0 0
|
Complexity
Linear on the number of elements inserted/erased (constructions/destructions).
增加或者截断时,时间或空间复杂度为线性复杂度(构造/析构)
If a reallocation happens, the reallocation is itself up to linear in the entirevector size.
如果发生重分配,重分配将复制整个数组到新数组也将需要线性时间//好像不太对。。。。
Iterator validity
In case the container shrinks, all iterators, pointers and references to elements that have not been removed remain valid after the resize and refer to the same elements they were referring to before the call.
假设是收缩的情况下,所有的迭代器,指针以及引用在resize之后并不会移动,因此都保此有效,都指向之前其所指向的元素。
If the container expands, theend iteratoris invalidated and, if it has to reallocate storage,
all iterators, pointers and references related to this container are also invalidated.
如果使扩展的情况,那么超尾迭代器将变得无效,并且如果发生了重分配(超出了capacity),那么所有的迭代器,指针以及引用都将失效。
Data races
The container is modified.
If a reallocation happens, all contained elements are modified.
Otherwise, none of the elements beforenis accessed, and concurrently accessing or modifying them is safe.
容器将被修改。
如果发生重分配,容器内所有元素都将被修改(这里修改的是地址么?);
否则,前n个元素不会被访问,同时访问以及修改他们都是安全的。
Exception safety
Ifnis less than or equal to thesizeof the container, the function never throws
exceptions (no-throw guarantee).
如果n小于或者等于容器的size,那么不会抛出异常。
Ifnis greater and a reallocation happens, there are no changes in the container in case of exception (strong guarantee) if the type of the elements is eithercopyableorno-throw moveable.
如果n大于容器的size,并且发生了重分配,如果元素的复制构造器以及移动构造器都不会抛出异常,那么其抛出异常的规则不变。//这句翻译的不太好。
Otherwise, if an exception is thrown, the container is left with a valid state (basic guarantee).
否则,如果抛出一个异常,容器将留在一个有效的状态???还是将这个异常留给
//翻译的不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
2014-8-13
于GDUT
分享到:
相关推荐
STL vector 知识详解 STL vector 知识详解 STL vector 知识详解 STL vector 知识详解 STL vector 知识详解
SGI STL之vector源码,带注释
c++的STL的vector的一个实现。使用了c++11的大部分特性,包含vector的几乎所有功能。仅作学习之用。
该文档详细讲解了C++中标准容器的使用,是一份不错的学习资料哦
STL中vector、list、deque和map的区别
使用VC++控制台应用程序编写,测试了:vector对象的排序,对象中的大小无序,有重复。
心希盼 c++ STL Vector 类源码 详细说明“心希盼 Vector.doc”
c++ STL source code, hash and vector etc
C++STL vector list map set dqueue 等应用举例及PPT讲解示例,代码演示
SGI vector源码
给大家介绍 stl vector用法,主要知识点在如何恰当的使用它们的成员函数,涉及到条件函数和函数指针在迭代算法中的使用,对stl vector用法感兴趣的朋友可以参考下本
详细讲解了STL中vector容器的用法.
vector list map pair stl 标准模板库 c++ 程序示例
STL VECTOR 、迭代器,DEV C++ 编译运行
实现统计一段文章的每个单词的个数 其中CountDemo使用STL中的Map来实现的 CountDemo2是用一般语言实现,没有用到STL实现的; MapCount是用STL中的Vector和Map共同实现的...此题目是学习STL中的Map和Vector必练的经典题目
非常实用的STL容器讲解学习,内容全,讲解详细 包括Vector、Vector、String、Deque、sort、set、map,绝对有用!!
仿写C++ STL标准库 vector 源码,可直接在cpp文件中调用实现
STL中map用法详解 STL中map用法详解 STL中map用法详解
很全的STL string类的函数方法,便于查询
STL 中的常用的Vector Map Set Sort用法