C++ Forward_list::cbefore_begin() 函数



C++ 的std::forward_list::cbefore_begin()函数用于获取指向forward_list容器第一个元素之前元素的常量迭代器。

这个forward_list的元素充当占位符,尝试访问它会导致未定义行为。返回的迭代器可以与emplace_after()、erase_after()、insert_after()和splice_after()函数一起使用,以在返回的迭代器位置插入、删除和拼接元素。

cbefore_begin()函数类似于C++ std::forward_list中的before_begin()函数。

语法

以下是C++ std::forward_list::cbefore_begin()函数的语法:

const_iterator cbefore_begin();

参数

  • 它不接受任何参数。

返回值

此函数返回指向第一个元素之前元素的常量迭代器。

示例1

如果forward_list是int类型,则cbefore_begin()函数返回指向第一个元素之前元素的常量迭代器。

在下面的程序中,我们使用C++ std::forward_list::cbefore_begin()函数来获取此forward_list {1, 2, 3, 4, 5}第一个元素之前元素的常量迭代器。

#include<iostream>
#include<forward_list>
using namespace std;
int main() {
   //create a forward_list
   forward_list<int> num_list = {1, 2, 3, 4, 5};
   cout<<"The num_list contents are: "<<endl;
   for(int n : num_list){
      cout<<n<<endl;
   }
   //using the cbefore_begin() function
   auto it = num_list.cbefore_begin();
   cout<<"The constant iterator to the element before the first element is: "<<*it;
}

输出

让我们编译并运行上述程序,这将产生以下结果:

The num_list contents are: 
1
2
3
4
5
The constant iterator to the element before the first element is: 0

示例2

如果forward_list是char类型,则此函数返回指向第一个char元素之前元素的常量迭代器。

以下是C++ std::forward_list::cbefore_begin()函数的另一个示例。在这里,我们创建一个名为char_list的forward_list(char类型),内容为{'B', 'C', 'E', 'F'}。然后,我们使用cbefore_begin()函数获取指向此容器第一个元素之前元素的常量迭代器,并使用emplace_after()函数在返回的迭代器位置插入一个新元素'A'。

#include<iostream>
#include<forward_list>
using namespace std;
int main() {
   //create a forward_list
   forward_list<char> char_list = {'B', 'C', 'D', 'E'};
   cout<<"The char_list contents are: "<<endl;
   for(char c : char_list){
      cout<<c<<endl;
   }
   //using the cbefore_begin() function
   auto it = char_list.cbefore_begin();
   cout<<"The constant iterator is: "<<*it<<endl;
   //using emplace_after() function
   char_list.emplace_after(it, 'A');
   cout<<"The char_list contents after inserting new element: "<<endl;
   for(char c: char_list){
      cout<<c<<endl;
   }
}

输出

这将生成以下输出:

The char_list contents are: 
B
C
D
E
The constant iterator is: �
The char_list contents after inserting new element: 
A
B
C
D
E

示例3

除了int类型和char类型的forward_list之外,我们还可以检索string类型forward_list元素的常量迭代器。

在这个例子中,我们创建一个名为fruits的forward_list(string类型),内容为{"Apple", "Banana", "Orange", "Grapes", "Guava"}。然后,使用cbefore_begin()函数,我们尝试获取指向此容器第一个元素之前元素的常量迭代器,并使用insert_after()函数,我们尝试在返回的迭代器位置插入一个新元素"Pear"。

#include<iostream>
#include<forward_list>
using namespace std;
int main() {
   //create a forward_list
   forward_list<string> fruits = {"Apple", "Banana", "Orange", "Grapes", "Guava"};
   cout<<"The fruits forward_list contents are: "<<endl;
   for(string n : fruits){
      cout<<n<<endl;
   }
   //using the cbefore_begin() function
   auto it = fruits.cbefore_begin();
   cout<<"The constant iterator is: "<<*it<<endl;
   //using the insert_after()function
   fruits.insert_after(it, "Pear");
   cout<<"The fruits forward_list contents after inserting new element: "<<endl;
   for(string s : fruits){
      cout<<s<<endl;
   }
}

输出

以下是上述程序的输出:

The fruits forward_list contents are: 
Apple
Banana
Orange
Grapes
Guava
The constant iterator is: 
The fruits forward_list contents after inserting new element: 
Pear
Apple
Banana
Orange
Grapes
Guava
广告