forward_list学习之insert_after,emplace_after, push_fron, emplace_front,resize操作

本篇学习forward_list添加元素操作

insert_after:在某个元素后插入新元素

emplace_after:在元素后原位构造元素

push_front:插入元素到容器起始

emplace_front:在容器头部就地构造元素

resize:改变容器中可存储元素的个数

代码实现

#include <forward_list>
#include <iostream>
#include <time.h>

using namespace std;

class Person2
{

public:
    Person2(string name):
        m_strName(name)
    {

    }

    string m_strName;
};

void addElement()
{
    forward_list<Person2> 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<Person2> 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<int> list = {75,76,77,78};

    forward_list<int> 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<int> 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<int> 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填充,指定默认值则采用默认值填充。

参考:

https://zh.cppreference.com/w/cpp/container/forward_list

http://www.cplusplus.com/reference/forward_list/forward_list/

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值