- 映射::cbegin() 是C++ STL中的一个内置函数,它返回一个常数迭代器,引用映射容器中的第一个元素。由于map container以有序的方式包含元素,因此cbegin()将根据容器的排序标准指向最先出现的元素。
语法:
map_name.cbegin()
参数: 该函数不接受任何参数。
返回值: 该函数返回一个常量迭代器,该迭代器引用映射容器中的第一个元素。
// C++ program to illustrate
// the map::cbegin() function
#include <bits/stdc++.h>
using
namespace
std;
int
main()
{
// initialize container
map<
int
,
int
> mp;
// insert elements in random order
mp.insert({ 2, 30 });
mp.insert({ 1, 40 });
mp.insert({ 3, 60 });
mp.insert({ 4, 20 });
mp.insert({ 5, 50 });
auto
ite = mp.cbegin();
cout <<
"The first element is: "
;
cout <<
"{"
<< ite->first <<
", "
<< ite->second <<
"}"
;
// prints the elements
cout <<
"The map is : "
;
cout <<
"KEY ELEMENT"
;
for
(
auto
itr = mp.cbegin(); itr != mp.cend(); ++itr) {
cout << itr->first
<<
' '
<< itr->second <<
''
;
}
return
0;
}
输出:The first element is: {1, 40} The map is : KEY ELEMENT 1 40 2 30 3 60 4 20 5 50
- 地图::cend() 是C++ STL中的一个内置函数,它返回一个常数迭代器,指向MultIAP中最后一个元素的理论元素。由于map container以有序的方式包含元素,因此根据容器的排序标准,cend()将指向最后一个元素后面的元素。
语法:
map_name.cend()
参数: 该函数不接受任何参数。
返回值: 该函数返回一个常量迭代器,该迭代器指向映射中最后一个元素后面的理论元素。
// C++ program to illustrate
// the map::cend() function
#include <bits/stdc++.h>
using
namespace
std;
int
main()
{
// initialize container
map<
int
,
int
> mp;
// insert elements in random order
mp.insert({ 2, 30 });
mp.insert({ 1, 40 });
mp.insert({ 3, 60 });
mp.insert({ 4, 20 });
mp.insert({ 5, 50 });
// print the elements
cout <<
"The map is : "
;
cout <<
"KEY ELEMENT"
;
for
(
auto
itr = mp.cbegin(); itr != mp.cend(); ++itr) {
cout << itr->first
<<
' '
<< itr->second <<
''
;
}
return
0;
}
输出:The map is : KEY ELEMENT 1 40 2 30 3 60 4 20 5 50
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END