C++ forward_list::before_begin() 函数



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

此 forward_list 的元素充当占位符。此函数返回的迭代器可与 emplace_after()、erase_after()、insert_after() 和 splice_after() 函数一起使用,以在返回的迭代器位置插入、删除和替换元素。before_begin() 函数类似于 C++ std::forward_list 中的 cbefore_begin() 函数。

语法

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

iterator before_begin();

参数

  • 它不接受任何参数。

返回值

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

示例 1

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

在下面的程序中,我们使用 C++ std::forward_list::before_begin() 函数来检索指向此 forward_list {10, 20, 30, 40, 50} 的第一个元素之前元素的迭代器。

#include<iostream>
#include<forward_list>
using namespace std;
int main() {
   //create a forward_list
   forward_list<int> num_list = {10, 20, 30, 40, 50};
   cout<<"The num_list contents are: "<<endl;
   for(int n : num_list){
      cout<<n<<endl;
   }
   //using the before_begin() function
   auto it = num_list.before_begin();
   cout<<"An iterator which points to the position before the first element is: "<<*it;
}

输出

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

The num_list contents are:
10
20
30
40
50
An iterator which points to the position before the first element is: 0

示例 2

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

以下是 C++ std::forward_list::before_begin() 函数的另一个示例。在这里,我们创建一个名为 char_list 的 forward_list(char 类型),其内容为 {'b', 'c', 'd', 'e'}。然后,我们使用 before_begin() 函数来检索指向此 forward_list 的第一个元素之前元素的迭代器,并使用 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 before_begin() function
   auto it = char_list.before_begin();
   cout<<"An 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
An iterator is: 0
The char_list contents after inserting new element: 
a
b
c
d
e

示例 3

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

在这个例子中,我们创建了一个名为 names 的 forward_list(string 类型),其内容为 {"Raju", "Aman", "Rahul", "Ganesh"}。然后,使用 before_begin() 函数,我们尝试检索指向第一个元素之前元素的迭代器,并使用 insert_after() 函数在返回的迭代器位置插入一个新元素 "Ramesh"。

#include<iostream>
#include<forward_list>
using namespace std;
int main() {
   //create a forward_list
   forward_list<string> names = {"Raju", "Aman", "Rahul", "Ganesh"};
   cout<<"The names forward_list contents are: "<<endl;
   for(string n : names){
      cout<<n<<endl;
   }
   //using the before_begin() function
   auto it = names.before_begin();
   cout<<"An iterator is: "<<*it<<endl;
   //using the insert_after()function
   names.insert_after(it, "Ramesh");
   cout<<"The names forward_list contents after inserting new element: "<<endl;
   for(string s : names){
      cout<<s<<endl;
   }
}

输出

以下是上述程序的输出:

The names forward_list contents are: 
Raju
Aman
Rahul
Ganesh
An iterator is: 
The names forward_list contents after inserting new element: 
Ramesh
Raju
Aman
Rahul
Ganesh
广告
© . All rights reserved.