erase:擦掉; 抹去; 擦掉; 清除
emplace:放列,安置,安放
list.cpp
主要关于容器的初始化操作
#include <list> #include <forward_list> #include <deque> #include <string> #include <iostream> #include <array> using namespace std; //vector-可变大小数组,支持快速随机访问,在尾部之外的位置插入或删除元素可能很慢 //list-双向链表,只支持双向顺序访问,在list中任何位置进行插入删除速度都很快 //forward_list-单向链表,只支持单向顺序访问,在链表中任何位置进行插入删除速度都很快 //deque-双端队列,支持快速随机访问,在头尾位置插入删除速度很快 //array-固定大小数组,支持快速随机访问,不能添加或删除元素 //string-与vector相似的容器,但专门用于保存字符,随机访问快,在尾部插入删除速度快 int main(){ //使用给定的元素初始化 //列表初始化 list<string> authors ={"hello","rihl","sdsdsd"}; for(auto w:authors){ cout<<w<<endl; } //拷贝初始化 list<string> authors2(authors); for(auto w:authors2){ cout<<w<<endl; } list<string> slist(10,"hi"); for(auto w:slist){ cout<<w<<endl; } //只支持单向遍历 forward_list<int> fl(10); for(auto w:fl){ cout<<w<<endl; } list<string> demo = {"hi","sd","we","gf"}; auto it1 = demo.begin(); auto it2 = demo.end(); while(it1!=it2){ cout<<*it1<<endl; ++it1; } //反向迭代器 list<string>::reverse_iterator it3 = demo.rbegin(); list<string>::reverse_iterator it4 = demo.rend(); //倒序遍历-反向遍历 while(it3!=it4){ cout<<*it3<<endl; ++it3; } //array的定义,除了制定类型外,还要指定容器大小 //列表初始化 //不够十个,剩下的元素执行默认初始化 array<int,10> hello_array = {12,12,4,5,54,65,78}; array<string,10> hello_string_array = {"as","sd","sds","sdfsd"}; auto array_it = hello_array.begin(); while(array_it!=hello_array.end()){ cout<<*array_it<<endl; ++array_it; } //双端队列 deque<string> h_deque(10); for(auto w:h_deque){ cout<<w<<endl; } deque<string> deque2={"s","ds","sdsdsd","qweqe"}; for(auto w:deque2){ cout<<w<<endl; } deque<string> deque3(19,"ninimimi"); for(auto w:deque3){ cout<<w<<endl; } //deque的拷贝初始化 deque<string> deque4 = deque3; for(auto w:deque4){ cout<<w<<endl; } }
list2.cpp
赋值,assign和swap操作
#include <list> #include <forward_list> #include <deque> #include <string> #include <iostream> #include <array> #include <vector> using namespace std; /* 赋值 assign swap */ int main(){ //赋值 array<int,10> a1 = {0,1,2,3,4,5,6,7,8,9}; array<int,10> a2 = {0}; //所有元素的值均为零 a1=a2; //替换a1中的元素 a2={0,90,122,23,232,3}; //使用花括号列表赋予数组,书上说不能将一个花括号列表赋予数组。。。 for(auto a:a1){ cout<<a<<endl; } cout<<"============="<<endl; for(auto a:a2){ cout<<a<<endl; } //赋值运算符要求左边和右边的运算对象具有相同的类型。他将右边运算对象中的所有元素 //拷贝到左边运算对象中去。 //使用assign(顺序容器) //顺序容器(处array外)还定义了一个名为assign的成员 list<string> names; vector<const char*> old_style; //用字符指针指向一个字符串 //把字符串的第一个字符的地址赋予指针 const char *s1 = "hello,world"; const char *s2 = "nishi"; const char *s3 = "sdsdsd"; old_style.push_back(s1); old_style.push_back(s2); old_style.push_back(s3); //将names中的元素替换为迭代器指定的范围中的元素的拷贝 names.assign(old_style.begin(),old_style.end()); for(auto a : names){ cout<<a<<endl; } //使用swap //swap操作交换两个相同类型容器中的内容 list<string> v1={"sd","sdsd"}; list<string> v2={"sa","wd"}; swap(v1,v2); for(auto a:v2){ cout<<a<<endl; } }
list3.cpp
向顺序容器添加元素
#include <deque> #include <string> #include <iostream> #include <array> #include <vector> #include <list> using namespace std; //向顺序容器添加元素 //array类型的容器不支持这些操作 int main(){ //使用push_back操作 list<string> l; l.push_back("sdsds"); //使用push_front l.push_front("push_front"); for(auto a:l){ cout<<a<<endl; } deque<int> d; d.push_back(2); d.push_front(1); for(auto a:d){ cout<<a<<endl; } //vector不支持push_front操作 //在容器的特定位置添加元素 //使用insert操作 //每个insert函数接受一个迭代器作为第一个参数,迭代器指出了在容器中什么位置放置元素 //insert函数将元素插入到迭代器所指定的位置之前 list<int> ll = {1,3,4}; auto it = ll.begin(); ll.insert(++it,2); for(auto a:ll){ cout<<a<<endl; } //使用insert插入范围元素 vector<int> ivec = {1,2,4}; //将10个元素插入到ivec的末尾 ivec.insert(ivec.end(),10,8); for(auto i:ivec){ cout<<i<<endl; } //insert函数的返回值 list<string> lst; auto lst_it = lst.begin(); string word; while(cin>>word){ lst_it = lst.insert(lst_it,word); } //新标准引入的三个成员 //emplace_front,emplace,emplace_back这些操作分别对应push_front,insert,push_back //当调用push或insert成员函数时,我们将元素类型的对象传递给她们,这些对象被拷贝到容器中 //当我们调用一个emplace成员函数时,则是将参数传递给元素类型的构造函数 //emplace函数的参数根据元素类型而变化,参数必须与元素类型的构造函数相匹配 }
list4.cpp
访问顺序容器中的元素
#include <deque> #include <string> #include <iostream> #include <array> #include <vector> #include <list> using namespace std; //访问顺序容器中的元素 //包括array在内的每个顺序容器都有一个front成员函数 //而除forward_list之外的所有容器都有一个back成员函数 //这两个操作分别返回首元素和尾元素的引用 int main(){ list<string> l = {"sadfsad","sadfsadf","asdfsad"}; if(!l.empty()){ //返回首元素和尾元素的引用 auto s1 = l.front(); auto s2 = l.back(); cout<<s1<<" "<<s2<<endl; //如果容器是一个const对象,则返回const的引用 //如果容器不是const,则返回值是普通引用 } //下标操作和安全的随机访问 //提供快速随机访问的容器(string,vector,deque和array)也都提供下标运算符 vector<int> ivec = {1,2,45,13}; cout<<ivec.at(4)<<endl; //抛出异常,下标越界 //terminate called after throwing an instance of 'std::out_of_range' //what(): vector::_M_range_check }
list5.cpp
删除元素
#include <deque> #include <string> #include <iostream> #include <array> #include <vector> #include <list> using namespace std; //删除元素 int main(){ //在删除元素之前,必须确保他们是存在的 //pop_front和pop_back成员函数 //分别删除首元素和尾元素 //vector和string不支持push_front,同样也不支持pop_front list<string> hello = {"sdsdsd","sdfsdfdsfdsf","hhlo"}; while(!hello.empty()){ hello.pop_front(); } if(hello.empty()){ cout<<"容器是空的"<<endl; } //从容器内部删除一个元素 //成员函数erase从容器中指定为删除元素,我们可以删除由一个迭代器指定的单个元素,也可以删除某个范围内元素 //erase成员函数都返回指向删除的(最后一个)元素之后位置的迭代器 list<int> il = {0,1,2,3,4,5,6,7,8,9}; auto it = il.begin(); while(it!=il.end()){ if(*it % 2){ it = il.erase(it); }else { ++it; } } for(auto a:il){ cout<<a<<endl; } }
=====END=====