STL2- 顺序容器
来源:互联网 发布:淘宝读书网页版 编辑:程序博客网 时间:2024/05/02 10:00
操作:
1. swap(c1,c2)或c1.swap(c2):交换c1和c2中元素,c1和c2类型必须相同,它比c2向c1拷贝元素快得多。
2. seq.assign(b,e)将seq中的元素替换为迭代器b和e表示范围内的元素。seq.assign(n,t)将seq中的元素替换为n个t。assign操作不适合关联容器和array。
3. c.push_back(t),c.emplace_back(args)
4. c.push_front(t),c.emplace_front(args)
5. c.insert(p,i),c.emplace(p,args),c.insert(p,n,t),c.insert(p,b,e),c.insert(p,il)
6. c.pop_back()
7. c.pop_front()
8. c.erase(p):删除迭代器p所指定的元素;c.erase(b,e);c.erase()删除所有元素。
9. li.resize(15);改变容器大小。
一.<vector>
1. vector表示对象的集合,vector是类模板。不存在包含引用的vector。
#inclue<vector>
using std::vector;
2. 如果循环体内包含有vector对象添加元素的语句,则不能使用范围for循环。范围for语句体内不应该改变其遍历序列的大小。
3. size()函数返回类型为:vector<int> ::size_type
4. 添加元素push_back();
void vectortest(){
cout<<"vectortest!!!!!!"<<endl;
vector<int> v {1,2,3,4,5};
for(int i=6; i<11 ; i++)
v.push_back(i);
for(auto&c:v)
cout<<c<<" "<<endl;
}
二.<array>
1.固定大小数组。支持快速随机访问,不能添加或删除元素。
2. 内置数组不能进行拷贝或对象赋值,但是array无此限制。
3.例子:
void arraytest(){
cout<<"arraytest!!!!"<<endl;
array<int,9> arr={1,2,3,4,5,6,7,8,9};
cout<<arr[5]<<endl;
}
三.<deque>
1.双端队列,在头尾删除或插入速度都很快。在中间位置删除或插入代价很大。
2.例子:
void dequetest(){
cout<<"dequetest!!!!"<<endl;
deque<int>de ={0,1,2,3,4,5,6,7};
de.push_back(8);
de.push_back(9);
de.pop_front();
for(auto&c:de)
cout<<c<<" ";
cout<<endl;
}
四.<list>
1.双向链表,在任何位置删除或插入都很快。不支持元素的随机访问,为了访问一个元素,需要遍历整个链表。与vector、deque、array相比,其内存开销很大。
2.例子:删除所有基数元素。
void listtest(){
cout<<"list test!!!!"<<endl;
list<int>li={3,4,5,6,7};
li.push_front(2);
li.push_front(1);
li.push_back(8);
li.push_back(9);
cout<<"list中元素为:"<<ends;
for(auto &c:li)
cout<<c<<" ";
cout<<endl;
auto it = li.begin();
while(it !=li.end())
if(*it %2)
it =li.erase(it);
else
++it;
cout<<"list删除基数元素为:"<<ends;
for(auto &c:li)
cout<<c<<" ";
cout<<endl;
}
五.<forward_list>
1.单向链表,在任何位置删除或插入速度很快。与vector、deque、array相比,其内存开销很大。
2.其没有size操作,因为保存或计算其大小就会比手写链表多出额外的开销。
void forward_listtest(){
cout<<"forward_listtest!!!!"<<endl;
forward_list<int>flist = {1,2,3,4,5,6,7,8,9};
auto prev =flist.before_begin();
auto curr =flist.begin();
while(curr !=flist.end()){
if(*curr %2)
curr = flist.erase_after(prev);
else{
prev =curr;
curr++;
}
}
cout<<"删除基数元素后为:"<<ends;
for(autoc:flist)
cout<<c<<" ";
cout<<endl;
}
- STL2- 顺序容器
- STL2——关联容器
- stl2
- STL2—关联容器map基础总结
- STL2—关联容器map基础总结
- STL2 各个容器的底层数据结构
- 顺序容器
- 顺序容器
- 顺序容器
- 顺序容器
- 顺序容器
- 顺序容器
- 顺序容器
- 顺序容器
- 顺序容器
- 顺序容器
- 顺序容器
- 顺序容器
- 求n内素数问题
- 网页中常用字体介绍
- Android 弹无虚发之第四弹:你应该掌握的Notification(Notification的通知栏常驻、Notification的各种样式、Notification点击无效)
- qws
- hdu1851 A Simple Game
- STL2- 顺序容器
- 解决:GridView提示无法将类型为“System.Web.UI.LiteralControl”的对象强制转换为类型...
- Linux下查看MySQL的安装路径
- 13-hibernate 实战 一对多对象关系模型映射
- STL3-容器适配器
- A+B problem II
- linux性能提升方法(整理)
- STL4- 关联容器
- 黑马程序员:Socket编程之(UDP vs TCP)