国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++利用std::forward_list查找插入數據方法示例

2020-01-26 13:57:57
字體:
來源:轉載
供稿:網友

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查找插入數據的相關內容,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧。

示例代碼:

//// 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 */

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對武林網的支持。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 全椒县| 乌什县| 措美县| 荣成市| 淄博市| 册亨县| 南丹县| 中西区| 湾仔区| 遵化市| 牙克石市| 蕉岭县| 柯坪县| 吉木萨尔县| 离岛区| 石河子市| 萝北县| 皋兰县| 漠河县| 韶关市| 孝义市| 红原县| 泰顺县| 龙川县| 茂名市| 凤翔县| 凉城县| 葫芦岛市| 镇康县| 河西区| 连山| 民丰县| 安龙县| 抚松县| 万宁市| 乌审旗| 德州市| 祁阳县| 外汇| 朝阳县| 沁阳市|