本文共 3524 字,大约阅读时间需要 11 分钟。
本篇学习forward_list添加元素操作
insert_after:在某个元素后插入新元素
emplace_after:在元素后原位构造元素
push_front:插入元素到容器起始
emplace_front:在容器头部就地构造元素
resize:改变容器中可存储元素的个数
代码实现
#include#include #include using namespace std;class Person2{public: Person2(string name): m_strName(name) { } string m_strName;};void addElement(){ forward_list list1; string strName = "《出塞(王昌龄)》秦时明月汉时关,万里长征人未还。但使龙城飞将在,不教胡马度阴山。"; Person2 person(strName); //int = [-2147483648, 2147483647] const int count = 214748;//2147483648 //在链表头插入元素 double startTime1 = clock();//1计算开始 for(int i = 0; i < count; i++)//214748 { list1.push_front(strName); } double endTime1 = clock();//1时间结束 list1.clear(); cout << "for 1 run time is: " << (double)(endTime1 - startTime1) / CLOCKS_PER_SEC << "s" << endl; cout << endl; forward_list list2; double startTime2 = clock();//2计算开始 for(int i = 0; i < count; i++)//214748 { list2.emplace_front(strName); } double endTime2 = clock();//2时间结束 list2.clear(); cout << "for 2 run time is: " << (double)(endTime2 - startTime2) / CLOCKS_PER_SEC << "s" << endl; cout << endl; //在pos位置之后插入元素 forward_list list = {75,76,77,78}; forward_list list3; list3.assign({23, 26, 28}); cout << "list3的值为:" << endl; for(auto &val: list3) { cout << val << "\t"; } cout << endl; //向list3第一个元素之后插入60这个元素 list3.insert_after(list3.begin(), 60); cout << "list3的值为:" << endl; for(auto &val: list3) { cout << val << "\t"; } cout << endl; //向list3第一个元素之后插入3个98 list3.insert_after(list3.begin(), 3, 98); cout << "list3的值为:" << endl; for(auto &val: list3) { cout << val << "\t"; } cout << endl; //向list3第一个元素之后插入list的第二个元素到最后一个元素之间的值 list3.insert_after(list3.begin(), ++list.begin(), list.end()); cout << "list3的值为:" << endl; for(auto &val: list3) { cout << val << "\t"; } cout << endl; list3.clear(); cout << endl; forward_list list4; list4.assign({35, 26, 18}); cout << "list4的值为:" << endl; for(auto &val: list4) { cout << val << "\t"; } cout << endl; //向list4第2个元素后插入73这个元素 list4.emplace_after(++list4.begin(), 73); cout << "list4的值为:" << endl; for(auto &val: list4) { cout << val << "\t"; } cout << endl;// //向list4第2个元素之后插入两个62// list4.emplace_after(list4.begin(), 2, 62);//不支持// //向list4第一个元素之后插入list的第二个元素到最后一个元素之间的值// list4.emplace_after(++list4.begin(), ++list.begin(), list.end());//不支持 list4.clear(); cout << endl; //改变容器存储的个数 forward_list list5; cout << "list5.empty = " << list5.empty() << " list5.max_size = " << list5.max_size() << endl; cout << "list5的值为:" << endl; list5 = {31, 32, 33, 34}; for(auto &val: list5) cout << val << "\t"; cout << endl; cout << "list5的值为:" << endl; list5.resize(3); for(auto &val: list5) cout << val << "\t"; cout << endl; list5.resize(5); cout << "list5的值为:" << endl; for(auto &val: list5) cout << val << "\t"; cout << endl; cout << "list5的值为:" << endl; list5.resize(7, 67); for(auto &val: list5) cout << val << "\t"; cout << endl; cout << endl;}int main(){ addElement(); cout << endl; cout << "Hello World!" << endl; return 0;}
运行结果:
从以上结果可以看出,添加元素,emplace_frontr比insert_front效果要高很多,原理是emplace_after在元素后原位构造元素,省掉拷贝的过程,emplace_after与insert_after也是一样的原理。但是emplace_after不支持使用第二方的list范围操作和插入多个同样值,代码可以看出校果。
resize重新设置forward_list容器的大小,如果设置的值n小于当前容器的容量,则只取容器前n个元素,如果n大于当前容器的容量,则将其扩充为容量n,如果没有指定填充值,采用0填充,指定默认值则采用默认值填充。
参考:
转载地址:http://zbiub.baihongyu.com/