C++利用std::forward_list查找插入数据方法示例分享

—-想了解C++利用std::forward_list查找插入数据方法示例分享的全部内容且更多的C语言教程关注<计算机技术网(www.ctvol.com)!!>

std::forward_list介绍

std::forward_list是在C++11中引入的单向链表或叫正向列表。forward_list具有插入、删除表项速度快、消耗内存空间少的特点,但只能向前遍历。与其它序列容器(array、vector、deque)相比,forward_list在容器内任意位置的成员的插入、提取(extracting)、移动、删除操作的速度更快,因此被广泛用于排序算法。forward_list是一个允许在序列中任何一处位置以常量耗时插入或删除元素的顺序容器(sequence Container)。forward_list可以看作是对C语言风格的单链表的封装,仅提供有限的接口,和C中它的实现相比,基本上不会有任何开销。当不需要双向迭代的时候,与std::list相比,该容器具有更高的空间利用率。

forward_list的主要缺点是不能在常量时间内随机访问任意成员,对成员的访问需要线性时间代价;以及存储链接信息需要消耗内存,特别是当包含大量的小规模成员时。forward_list处于效率考虑,有意不提供size()成员函数。获取forward_list所包含的成员个数需要用std::distance(_begin, _end)算法。forward_list中的每个元素保存了定位前一个元素及后一个元素的信息,不能进行直接随机访问操作。

C++利用std::forward_list查找插入数据方法示例分享将给大家介绍关于C++用std::forward_list查找插入数据的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

示例代码:

  //  // Forward_list.hpp  // 练习  //  // Created by hanzhiqiang on 2017/6/11.  // Copyright © 2017年 hanzhiqiang. All rights reserved.  //    #ifndef Forward_list_hpp  #define Forward_list_hpp    #include <stdio.h>  #include <iostream>  #include <forward_list>    using namespace std;    int main()  {    forward_list<string> mList;    mList.emplace_front("aaa");    mList.emplace_front("bbb");    mList.emplace_front("ccc");        for (auto it = mList.begin(); it != mList.end(); it++)    {      cout<<*it<<endl;    }      //  for (auto it = mList.before_begin(); it != mList.end(); it++)  //  {  //    cout<<*it<<endl;  //  }      //  auto itList = find(mList.begin(), mList.end(), "fff");  //  if (itList != mList.end())   //  {  //    mList.emplace_after(itList, "111");  //  }  //  else  //  {  //    mList.insert_after(mList.end(),"222");//c++ primer p 313 向末尾插入数据结果未知 error  //  }        auto prev = mList.before_begin();    auto curr = mList.begin();    bool isInsert = false;    while (curr != mList.end())    {      if (*curr == "fff")      {        curr = mList.insert_after(curr, "111");        isInsert = true;      }      prev = curr;      curr++;    }        if(!isInsert)    {      curr = mList.insert_after(prev, "222");//向末尾插入数据成功    }        for (auto it = mList.begin(); it != mList.end(); it++)    {      cout<<"插入元素后"<<*it<<endl;    }        cout<<"fuck"<<endl;    return 0;  }    #endif /* Forward_list_hpp */

总结

以上就是这篇文章的全部内容了,希望C++利用std::forward_list查找插入数据方法示例分享的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对<计算机技术网(www.ctvol.com)!!>的支持。

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/488441.html

(0)
上一篇 2020年11月12日
下一篇 2020年11月12日

精彩推荐