C++ looks up the example insert data method using std::forward_list
- 2020-05-27 06:44:48
- OfStack
std: : forward_list is introduced
std::forward_list is a unidirectional linked list or forward list introduced in C++11. forward_list is fast to insert and delete table entries and consumes less memory space, but it can only traverse forward. Compared with other sequence containers (array, vector, deque), forward_list can insert, extract (extracting), move and delete members at any location in the container faster, so it is widely used in sorting algorithms. forward_list is a sequential container (sequence Container) that allows constant time to insert or delete elements at any point in the sequence (sequence Container). forward_list can be seen as a encapsulation of the C language style single linked list, providing only a limited interface and essentially no overhead compared to its implementation in C. When bidirectional iteration is not required, the container has higher space utilization than std::list.
The main disadvantage of forward_list is that it cannot randomly access any member in constant time. And storing link information consumes memory, especially when it contains a large number of small members. forward_list is deliberately not provided for the size() member function for efficiency reasons. The std::distance(_begin, _end) algorithm is needed to get the number of members contained by forward_list. Each element in forward_list holds information about locating the first and last element, and no direct random access is possible.
This article will introduce you about C++ using std::forward_list search insert data related content, share out for your reference to learn, the following words do not say, to 1 look at the detailed introduction.
Sample code:
//
// Forward_list.hpp
// practice
//
// Created by hanzhiqiang on 2017/6/11.
// Copyright © 2017 years 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 The result of inserting data into the end is unknown 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");// Insert data to the end successfully
}
for (auto it = mList.begin(); it != mList.end(); it++)
{
cout<<" After inserting the element "<<*it<<endl;
}
cout<<"fuck"<<endl;
return 0;
}
#endif /* Forward_list_hpp */
conclusion