C++ STL中的multiset多集cbegin()和cend()函数

2021年4月27日16:59:38 发表评论 954 次浏览

multiset::cbegin()是C++ STL中的内置函数, 它返回一个常量迭代器, 该迭代器指向容器中的第一个元素。迭代器不能用于修改set容器中的元素。可以增加或减少迭代器以遍历集合。

语法如下:

constant_iterator multiset_name.cegin()

参数:该函数不接受任何参数。

返回值:该函数返回一个常量迭代器, 该迭代器指向容器中的第一个元素。

下面的程序说明了multiset::cbegin()方法。

//CPP program to demonstrate the
//multiset::cbegin() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
  
     int arr[] = { 14, 10, 15, 11, 10 };
  
     //initializes the set from an array
     multiset<int> s(arr, arr + 5);
  
     //Prints the first element
     cout <<"The first elements is: " <<*(s.cbegin()) <<endl;
  
     //prints all elements in set
     for ( auto it = s.cbegin(); it != s.cend(); it++)
         cout <<*it <<" " ;
  
     return 0;
}

输出如下:

The first elements is: 10
10 10 11 14 15

multiset::cend()

是C++ STL中的内置函数, 该函数返回一个常量迭代器, 该迭代器指向容器中最后一个元素之后的位置。迭代器不能用于修改set容器中的元素。可以增加或减少迭代器以在集合中进行相应遍历。

语法如下:

constant_iterator multiset_name.cend()

参数:该函数不接受任何参数。

返回值:该函数返回一个常量迭代器, 该迭代器指向容器中容器中最后一个元素之后的位置。

下面的程序说明了multiset::cend()方法。

//CPP program to demonstrate the
//multiset::cend() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
  
     int arr[] = { 14, 10, 15, 11, 10, 15, 17, 17 };
  
     //initializes the set from an array
     multiset<int> s(arr, arr + 8);
  
     //prints all elements in set
     for ( auto it = s.cbegin(); it != s.cend(); it++)
         cout <<*it <<" " ;
  
     return 0;
}

输出如下:

10 10 11 14 15 15 17 17

被认为是行业中最受欢迎的技能之一, 我们拥有自己的编码基础C++ STL通过激烈的问题解决过程来训练和掌握这些概念。


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: