C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ map cbegin()

C++ map cbegin()函数

C++地图 cbegin()函数用于返回指向地图容器第一个元素的常量迭代器。

语法

const_iterator cbegin() const noexcept;  //since C++ 11
注意: const_iterator是指向常量内容的迭代器。

参数

返回值

它返回一个const_iterator

示例1

我们来看一个简单的cbegin()函数示例。
#include <iostream>
#include <map>
using namespace std;
int main ()
{
  map<char,string> mymap;
  mymap['b'] = "Java";
  mymap['a'] = "C++";
  mymap['c'] = "SQL";
  // show content:
  for (auto it = mymap.cbegin(); it != mymap.cend(); ++it)
    cout <<(*it).first << " => " << (*it).second << '\n';
  return 0;
}
输出:
a => C++
b => Java
c => SQL
在上面,cbegin()函数用于返回指向mymap映射中第一个元素的常量迭代器。

示例2

让我们看一个简单的示例,使用for-each循环遍历地图。
#include <iostream>
#include <map>
#include <string>
#include <iterator>
#include <algorithm>
 
using namespace std;
 
int main() {
 
      map<string, int> m;
  m["Room1"] = 100;
  m["Room2"] = 200;
  m["Room3"] = 300;
 
    // Iterate over a map using std::for_each and Lambda function
    
        for_each(m.cbegin(), m.cend(),
                [](pair<string, int> element){
                    
                    // Accessing KEY from element
                    string word = element.first;
                    // Accessing VALUE from element.
                    int count = element.second;
                    cout<<word<<" = "<<count<<endl;
        });
 
    return 0;
}
输出:
Room1 = 100
Room2 = 200
Room3 = 300 
在上面的示例中,我们使用STL算法std :: for-each遍历地图。它将迭代每个map元素并调用我们提供的回调。

示例3

让我们看一个简单的示例,使用while来迭代地图循环。
#include <iostream>
#include <map>
#include <string>
int main()
{
    using namespace std;
 
      map<int,string> mymap = {
                { 100, "Nikita"},
                { 200, "Deep"  },
                { 300, "Priya" },
                { 400, "Suman" },
                { 500, "Aman"  }};
 
    map<int, string>::const_iterator it; // declare an iterator
    it = mymap.cbegin(); // assign it to the start of the vector
    while (it != mymap.cend()) // while it hasn't reach the end
    {
cout << it->first << " = " << it->second << "\n"; 
// print the value of the element it points to
++it; // and iterate to the next element
    }
 
    cout << endl;
}
输出:
100: Nikita
200: Deep
300: Priya
400: Suman
500: Aman
在上面的示例中,cbegin()函数用于返回指向mymap映射中第一个元素的常量迭代器。

示例4

让我们看看另一个简单的示例。
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main ()
{
  map<int,int> mymap = {
                { 10, 10},
                { 20, 20 },
                { 30, 30 } };
          
                
  cout<<"Elements are:" <<endl;
 
    for (auto it = mymap.cbegin(); it != mymap.cend(); ++it)
    cout << it->first 
    << " + " 
    << it->second 
    << " = "
    <<it->first + it->second
    << '\n';
    auto ite = mymap.cbegin();
 
    cout << "The first element is: ";
    cout << "{" << ite->first << ", "
         << ite->second << "}\n";
  return 0;
  }
输出:
Elements are:
10 + 10 = 20
20 + 20 = 40
30 + 30 = 60
The first element is: {10, 10}
在上面的示例中,cbegin()函数用于返回指向mymap映射中第一个元素的迭代器。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4