函数 | 函数 | 作用 |
---|---|---|
push_back | emplace_back | 在容器尾部生成一个元素。和 push_front() 的区别是,该函数直接在容器头部构造元素,省去了复制移动元素的过程。 |
push_front | emplace_front | 在容器头部生成一个元素。和 push_front() 的区别是,该函数直接在容器头部构造元素,省去了复制移动元素的过程。 |
insert | emplace | 都是容器中插入新元素。插入类对象,insert需要调用类的构造函数和移动构造函数(或拷贝构造函数);而通过 emplace() 函数实现同样的功能,只需要调用构造函数即可。 |
见【STL四】序列容器——vector容器
因为empty对所有标准容器都是常数时间O(1),常被实现为内联函数(inline function)。而对于一些list实现,size需要遍历链表,耗费线性时间O(n)。
list<int> lst;
if (lst.size() == 0) { ... } // 不推荐使用
if (lst.empty()) { ... } // 推荐使用
#include >
#include
#include
using namespace std;
int main()
{
vector<int> v;
cout << "vector 含有0个对象时的empty" << endl;
auto start1 = chrono::high_resolution_clock::now();
v.empty();
auto end1 = chrono::high_resolution_clock::now();
cout << start1.time_since_epoch().count() << endl;
cout << end1.time_since_epoch().count() << endl<<endl;
/*auto l= std::chrono::duration_cast(end1 - start1);
cout << (double)l.count();*/
cout << "vector 含有0个对象时的size" << endl;
auto start2 = chrono::high_resolution_clock::now();
v.size();
auto end2 = chrono::high_resolution_clock::now();
cout << start2.time_since_epoch().count() << endl;
cout << end2.time_since_epoch().count() << endl<<endl;
/*auto l2 = std::chrono::duration_cast(end2 - start2);
cout << (double)l2.count();*/
cout << "vector 含有10000个对象时的size" << endl;
for (int i = 0; i < 10000; ++i)
v.push_back(i); //像普通数组一样使用数组容器
auto start3 = chrono::high_resolution_clock::now();
v.size();
auto end3 = chrono::high_resolution_clock::now();
cout << start3.time_since_epoch().count() << endl;
cout << end3.time_since_epoch().count() << endl;
/*auto l3 = std::chrono::duration_cast(end3 - start3);
cout << (double)l3.count();*/
return 0;
}
输出
vector 含有0个对象时的empty
109725992888000
109725992888100
vector 含有0个对象时的size
109725994739300
109725994739500
vector 含有10000个对象时的size
109725995782100
109725995782100
虽然每次输出的时间的相差略有不同,但是并没有证据证明他们耗费线性时间。
基于以上这些,个人并不支持网上的这个说法,个人认为他们的时间都是O(1);
//vector 容器。
#include
#include
using namespace std;
int main()
{
vector<int> v;
for (int i=0; i < 20; i++)
{
v.push_back(1);
cout <<"v.size = "<< v.size() << " ; v.capacity = " << v.capacity() << endl;
}
return 0;
}
输出
v.size = 1 ; v.capacity = 1
v.size = 2 ; v.capacity = 2
v.size = 3 ; v.capacity = 3
v.size = 4 ; v.capacity = 4
v.size = 5 ; v.capacity = 6
v.size = 6 ; v.capacity = 6
v.size = 7 ; v.capacity = 9
v.size = 8 ; v.capacity = 9
v.size = 9 ; v.capacity = 9
v.size = 10 ; v.capacity = 13
v.size = 11 ; v.capacity = 13
v.size = 12 ; v.capacity = 13
v.size = 13 ; v.capacity = 13
v.size = 14 ; v.capacity = 19
v.size = 15 ; v.capacity = 19
v.size = 16 ; v.capacity = 19
v.size = 17 ; v.capacity = 19
v.size = 18 ; v.capacity = 19
v.size = 19 ; v.capacity = 19
v.size = 20 ; v.capacity = 28
基于《三、C++ vector容量(capacity)和大小(size)的区别》我们可知,我们在知道大概容量的前提下,可以提前使用reserve来避免内存的浪费。
//vector 容器。
#include
#include
using namespace std;
int main()
{
vector<int> v;
v.reserve(21);
for (int i=0; i < 20; i++)
{
v.push_back(1);
cout <<"v.size = "<< v.size() << " ; v.capacity = " << v.capacity() << endl;
}
return 0;
}
输出
v.size = 1 ; v.capacity = 21
v.size = 2 ; v.capacity = 21
v.size = 3 ; v.capacity = 21
v.size = 4 ; v.capacity = 21
v.size = 5 ; v.capacity = 21
v.size = 6 ; v.capacity = 21
v.size = 7 ; v.capacity = 21
v.size = 8 ; v.capacity = 21
v.size = 9 ; v.capacity = 21
v.size = 10 ; v.capacity = 21
v.size = 11 ; v.capacity = 21
v.size = 12 ; v.capacity = 21
v.size = 13 ; v.capacity = 21
v.size = 14 ; v.capacity = 21
v.size = 15 ; v.capacity = 21
v.size = 16 ; v.capacity = 21
v.size = 17 ; v.capacity = 21
v.size = 18 ; v.capacity = 21
v.size = 19 ; v.capacity = 21
v.size = 20 ; v.capacity = 21
可以和pushBack搭配使用
可以和operator[]配合使用
//vector 容器。
#include
#include
using namespace std;
int main()
{
vector<int> v;
v.resize(21);
for (int i=0; i < 20; i++)
{
v.push_back(1);
cout <<"v.size = "<< v.size() << " ; v.capacity = " << v.capacity() << endl;
}
return 0;
}
输出
v.size = 22 ; v.capacity = 31
v.size = 23 ; v.capacity = 31
v.size = 24 ; v.capacity = 31
v.size = 25 ; v.capacity = 31
v.size = 26 ; v.capacity = 31
v.size = 27 ; v.capacity = 31
v.size = 28 ; v.capacity = 31
v.size = 29 ; v.capacity = 31
v.size = 30 ; v.capacity = 31
v.size = 31 ; v.capacity = 31
v.size = 32 ; v.capacity = 46
v.size = 33 ; v.capacity = 46
v.size = 34 ; v.capacity = 46
v.size = 35 ; v.capacity = 46
v.size = 36 ; v.capacity = 46
v.size = 37 ; v.capacity = 46
v.size = 38 ; v.capacity = 46
v.size = 39 ; v.capacity = 46
v.size = 40 ; v.capacity = 46
v.size = 41 ; v.capacity = 46
只有list容器才有成员函数remove,而array、vector和deque是没有remove成员函数的。
#include
#include
using namespace std;
int main()
{
list<int> l{ 1,2,2,3,3,4,5 };
auto begin = l.cbegin();
auto end = l.cend();
l.remove(3);
while( begin != end)
{
cout << *begin << endl;
begin++;
}
cout << "l.size = " << l.size() << endl;
return 0;
}
输出
1
3
3
4
5
l.size = 5
#include
#include
using namespace std;
int main()
{
list<int> l{ 1,2,2,3,3,4,5 };
auto first = l.begin();
first++;
auto last = l.end();
last--;
// l.erase(first,last);
l.erase(first);
for( auto begin = l.begin();begin != l.end();begin++)
{
cout << *begin << endl;
}
cout << "l.size = " << l.size() << endl;
return 0;
}
1
2
3
3
4
5
l.size = 6
#include
#include
using namespace std;
int main()
{
list<int> l{ 1,2,2,3,3,4,5 };
std::remove(l.begin(), l.end(), 2);//除了list,其他容器也可以使用,array,vector,deque)
for( auto begin = l.begin();begin != l.end();begin++)
{
cout << *begin << endl;
}
cout << "l.size = " << l.size() << endl;
return 0;
}
输出
1
3
3
4
5
4
5
l.size = 7
#include
#include
using namespace std;
int main()
{
vector<int> l{ 1,2,3,4,5 };
try
{
int a = l.at(5);
//int a = l[5];
}
catch (std::out_of_range const& e)
{
cout << e.what() << endl;
}
cout << "exit process!" << endl;
return 0;
}
输出
invalid vector subscript
exit process!
#include
#include
using namespace std;
int main()
{
vector<int> l{ 1,2,3,4,5 };
try
{
//int a = l.at(5);
int a = l[5];
}
catch (std::out_of_range const& e)
{
cout << e.what() << endl;
}
cout << "exit process!" << endl;
return 0;
}
#include
#include
using namespace std;
class demoObj {};
int main()
{
vector<demoObj*> v;
for (int i = 0; i < 5; i++)
{
v.emplace_back(new demoObj);
}
for (auto ite = v.begin(); ite != v.end(); ite++)
{
delete *ite;
cout << "delete new demoObj" << endl;
}
return 0;
}
#include
#include
using namespace std;
class demoObj {};
int main()
{
vector<int> v{1,2,2,3,3,3,4,5};
for (auto ite = v.begin(); ite != v.end(); )
{
if (2 == *ite)
{
cout << "erase v = " << *ite << endl;
ite=v.erase(ite);
}
else
{
ite++;
}
}
return 0;
}
#include
#include
using namespace std;
class demoObj {};
int main()
{
vector<int> v{1,2,2,3,3,3,4,5};
v.clear();
cout << "v.capacity = " << v.capacity() << endl;
return 0;
}
输出
v.capacity = 8
#include
#include
using namespace std;
class demoObj {};
int main()
{
vector<int> v{1,2,2,3,3,3,4,5};
// v.resize(0); //都初始化为0,但是capacity不变。
// v.reserve(1); //只对占用内存影响,对数据无影响,但是并不可以释放内存
v.clear();
cout << "v.capacity = " << v.capacity() << endl;
v.swap(vector<int>()); // or调用shrink_to_fit
cout << "v.capacity = " << v.capacity() << endl;
return 0;
}
尽管clear()会主动调用vector中元素的析构函数,但是并不会释放掉元素所占用的内存。所以vector所占的内存并不会随着元素的释放而释放。
如果你想在vector生命周期结束之前及时释放掉vector的内存,
方案一:swap()用一个匿名的vector对象来和已有的vector对象v来swap。
方案二:调用成员函数shrink_to_fit
参考:
1、C++ STL 容器库 中文文档
2、STL教程:C++ STL快速入门
3、https://www.apiref.com/cpp-zh/cpp/header.html
4、https://en.cppreference.com/w/cpp/container
5、STL 使用要点总结20条——王凡