在C++ STL中使用forward_list::cbefore_begin()
为了忠实地解释在C++ STL中使用forward_list::cbefore_begin()的概念,我们先讨论一下C++编程语言中的List代码和输出。forward_list c before begin函数是STL(标准模板库)中的一个内置功能。它返回一个指向forward_list第一个元素前面位置的常量随机访问迭代器。
C++ STL中的List
C++代码
// CPP program to show the implementation of List
// Here we are writing down the C++ programming language code to
// demonstrate the concept of List in C++ programming language code
// in the Standard Template Library (STL)
#include
#include
#include
using namespace std;
// function for printing the elements in a list
// the below few lines of code help us with displaying the elements that
// we have inserted into it which is a part of the C++ Standard Template
// Library (STL)
void showlist(list g)
{
list::iterator it;
for (it = g.begin(); it != g.end(); ++it)
cout << '\t' << *it;
cout << '\n';
}
// the main driver code functionality starts from here
int main()
{
// then below code helps us with creating a list in C++ STL dynamically
list gqlist1, gqlist2;
// loop to push elements into the list which we have just created
for (int i = 0; i < 10; ++i) {
gqlist1.push_back(i * 2);
gqlist2.push_front(i * 3);
}
// cout statement helping us to print gqlist1 elements on console
cout << "\nThe List 1 has elements : ";
showlist(gqlist1);
// cout statement helping us to print gqlist2 elements on console
cout << "\nThe List 2 has elements : ";
showlist(gqlist2);
// cout statement helping us to print gqlist1 front elements on console
cout << "\n list1.front() : " << gqlist1.front();
// cout statement helping us to print gqlist1 back elements on console
cout << "\n list1.back() : " << gqlist1.back();
//cout statement helping us to print gqlist after poping elements on console
cout << "\n list1.pop_front() : ";
gqlist1.pop_front();
showlist(gqlist1);
//cout statement helping us to print list after poping elements on console
cout << "\n list2.pop_back() : ";
gqlist2.pop_back();
showlist(gqlist2);
//cout statement helping us to print gqlist after reverse elements on
// console
cout << "\n list1.reverse() : ";
gqlist1.reverse();
showlist(gqlist1);
//cout statement helping us to print gqlist after sort elements on console
cout << "\n list2.sort(): ";
gqlist2.sort();
showlist(gqlist2);
return 0;
// end of the C++ programming language code explaining
// forward_list::cbefore_begin() in C++ STL
}
输出:
The List 1 has elements : 0 2 4 6 8 10 12 14 16 18
List 2 has elements : 27 24 21 18 15 12 9 6 3 0
list1.front() : 0
list1.back() : 18
list1.pop_front() : 2 4 6 8 10 12 14 16 18
list2.pop_back() : 27 24 21 18 15 12 9 6 3
list1.reverse() : 18 16 14 12 10 8 6 4 2
list2.sort(): 3 6 9 12 15 18 21 24 27
forward_list::cbefore_begin()的语法
forwardlist_name.cbefore_begin()
C++代码
// Here we are writing down the c++ programming language code to
// demonstrate the concept of forward_list::cbefore_begin() in C++ STL
// where it illustrates the function cbefore_begin() in C++ programming code
#include
using namespace std;
// the main driver code functionality starts from here
int main()
{
// the below code snippet helps us with creating a list with few
// initial values like 20, 30, 40 and ends with 50
forward_list fl = { 20, 30, 40, 50 };
// here, we are trying to perform the function
// cbefore_begin helping us to use the stl library in the C++ language
auto it = fl.cbefore_begin();
// the below code snippet helps us with the insertion operation
// where the element will be added to the first place
fl.insert_after(it, 10);
cout << "the elements which have gotten inserted are as follows" << endl;
// the below for loop helps us with printing down the
// elements, which we have created in the list on our output screens
for (auto it = fl.begin(); it != fl.end(); ++it)
cout << *it << " ";
return 0;
// end of the C++ programming language code explaining
// forward_list::cbefore_begin() in C++ STL
}
输出:
the elements which have gotten inserted are as follows
10 20 30 40 50