public member function
<vector>
Clear content
Removes all elements from thevector(which are destroyed), leaving the container with asizeof0.
移除vector内所有元素(并销毁他们),使容器大小为变为0(size而不是capacity)。
例子:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vi={10,20,30};
cout<<"size="<<vi.size()<<endl;
cout<<"capacity="<<vi.capacity()<<endl;
vi.clear();
cout<<"size="<<vi.size()<<endl;
cout<<"capacity="<<vi.capacity()<<endl;
cout<<"vi[1]="<<vi[1]<<endl;
}
运行结果:
发现数据还是没有被销毁阿。。。
A reallocation is not guaranteed to happen, and thevector capacityis not guaranteed to change
due to calling this function. A typical alternative that forces a reallocation is to useswap:
|
vector<T>().swap(x); // clear x reallocating
|
|
不保证会发生重分配,也不保证该函数会改变capacity,另一个备选的发生重分配的是swap。
Parameters
none
Return value
none
Example
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
|
// clearing vectors
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myvector;
myvector.push_back (100);
myvector.push_back (200);
myvector.push_back (300);
std::cout << "myvector contains:";
for (unsigned i=0; i<myvector.size(); i++)
std::cout << ' ' << myvector[i];
std::cout << '\n';
myvector.clear();
myvector.push_back (1101);
myvector.push_back (2202);
std::cout << "myvector contains:";
for (unsigned i=0; i<myvector.size(); i++)
std::cout << ' ' << myvector[i];
std::cout << '\n';
return 0;
}
|
|
Output:
myvector contains: 100 200 300
myvector contains: 1101 2202
|
Complexity
Linear insize(destructions).
This may be optimized toconstant complexityfortrivially-destructible types(such as scalar
or PODs), where elements need not be destroyed.
复杂度和其大小线性相关(析构)
最优复杂度可能是常量时间复杂度,因为可能其中的元素不需要析构。
Iterator validity
All iterators, pointers and references related to this container are invalidated.
所有的迭代器,指针以及引用都将失效。
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vi={10,20,30};
cout<<"size="<<vi.size()<<endl;
cout<<"capacity="<<vi.capacity()<<endl;
int &ri=vi[0];
auto it=vi.begin();
vi.clear();
cout<<"size="<<vi.size()<<endl;
cout<<"capacity="<<vi.capacity()<<endl;
cout<<"vi[1]="<<vi[1]<<endl;
cout<<"ri="<<ri<<endl;
cout<<"vi.begin()="<<*it<<endl;
}
坑我的吧,怎么都没有失效??
Data races
The container is modified.
All contained elements are modified.
容器将被访问。
所有容器元素都将被修改。
Exception safety
No-throw guarantee:this member function never throws exceptions.
该成员方法不会抛出异常。
//翻译的不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
2014-8-15
于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 source code, hash and vector etc
心希盼 c++ STL Vector 类源码 详细说明“心希盼 Vector.doc”
vector list map pair stl 标准模板库 c++ 程序示例
C++STL vector list map set dqueue 等应用举例及PPT讲解示例,代码演示
SGI vector源码
详细讲解了STL中vector容器的用法.
给大家介绍 stl vector用法,主要知识点在如何恰当的使用它们的成员函数,涉及到条件函数和函数指针在迭代算法中的使用,对stl vector用法感兴趣的朋友可以参考下本
STL VECTOR 、迭代器,DEV C++ 编译运行
非常实用的STL容器讲解学习,内容全,讲解详细 包括Vector、Vector、String、Deque、sort、set、map,绝对有用!!
实现统计一段文章的每个单词的个数 其中CountDemo使用STL中的Map来实现的 CountDemo2是用一般语言实现,没有用到STL实现的; MapCount是用STL中的Vector和Map共同实现的...此题目是学习STL中的Map和Vector必练的经典题目
仿写C++ STL标准库 vector 源码,可直接在cpp文件中调用实现
标准模板库中map、vector、以及sort等的用法讲解
STL 中的常用的Vector Map Set Sort用法
C++ 利用MAP和VECTOR实现多节点树,VC++ 利用STL中的MAP和VECTOR实现的一个多节点树。