最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++利用std::forward_list查找插入數(shù)據(jù)方法示例

 更新時(shí)間:2017年08月13日 16:22:26   作者:nanmuyao  
這篇文章主要給大家介紹了關(guān)于C++利用std::forward_list查找插入數(shù)據(jù)的相關(guān)資料,文中先對(duì)std::forward_list進(jìn)行了詳細(xì)的介紹,而后通過示例代碼給大家介紹了查找的方法,需要的朋友可以參考借鑒,下面話不多說了,來一起看看吧。

std::forward_list介紹

std::forward_list是在C++11中引入的單向鏈表或叫正向列表。forward_list具有插入、刪除表項(xiàng)速度快、消耗內(nèi)存空間少的特點(diǎn),但只能向前遍歷。與其它序列容器(array、vector、deque)相比,forward_list在容器內(nèi)任意位置的成員的插入、提取(extracting)、移動(dòng)、刪除操作的速度更快,因此被廣泛用于排序算法。forward_list是一個(gè)允許在序列中任何一處位置以常量耗時(shí)插入或刪除元素的順序容器(sequence Container)。forward_list可以看作是對(duì)C語言風(fēng)格的單鏈表的封裝,僅提供有限的接口,和C中它的實(shí)現(xiàn)相比,基本上不會(huì)有任何開銷。當(dāng)不需要雙向迭代的時(shí)候,與std::list相比,該容器具有更高的空間利用率。

forward_list的主要缺點(diǎn)是不能在常量時(shí)間內(nèi)隨機(jī)訪問任意成員,對(duì)成員的訪問需要線性時(shí)間代價(jià);以及存儲(chǔ)鏈接信息需要消耗內(nèi)存,特別是當(dāng)包含大量的小規(guī)模成員時(shí)。forward_list處于效率考慮,有意不提供size()成員函數(shù)。獲取forward_list所包含的成員個(gè)數(shù)需要用std::distance(_begin, _end)算法。forward_list中的每個(gè)元素保存了定位前一個(gè)元素及后一個(gè)元素的信息,不能進(jìn)行直接隨機(jī)訪問操作。

本文將給大家介紹關(guān)于C++用std::forward_list查找插入數(shù)據(jù)的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹吧。

示例代碼:

//
// Forward_list.hpp
// 練習(xí)
//
// 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 向末尾插入數(shù)據(jù)結(jié)果未知 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");//向末尾插入數(shù)據(jù)成功
  }
  
  for (auto it = mList.begin(); it != mList.end(); it++)
  {
    cout<<"插入元素后"<<*it<<endl;
  }
  
  cout<<"fuck"<<endl;
  return 0;
}

#endif /* Forward_list_hpp */

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論

平顶山市| 温泉县| 当雄县| 柳州市| 辛集市| 龙口市| 清水河县| 抚松县| 梅河口市| 易门县| 盐边县| 兴城市| 苏尼特右旗| 延长县| 龙南县| 托克托县| 台山市| 定陶县| 台州市| 黄石市| 洛扎县| 当涂县| 红安县| 井冈山市| 门源| 海兴县| 平邑县| 临洮县| 德庆县| 泾源县| 翼城县| 金湖县| 无锡市| 安陆市| 灌阳县| 抚远县| 上虞市| 万全县| 饶阳县| 平顶山市| 福安市|