C++ map cend() 函数

C++ map cend() 函数

C++ map cend() 函数用于返回一个常量迭代器,该迭代器位于map中最后一个条目之后。

注意:这是一个占位符。在此位置上不存在任何元素,尝试访问是未定义的行为。

语法

const_iterator cend() const noexcept;  //since C++ 11

注意:const_iterator是指向常量内容的迭代器。

参数

返回值

它返回一个指向地图中最后一个元素之后的常量迭代器。

示例1

让我们看一个关于cend()函数的简单示例。

#include <iostream>
#include <map>

using namespace std;

int main ()
{
  map<char,int> mymap;

  mymap['b'] = 100;
  mymap['a'] = 200;
  mymap['c'] = 300;

  // print content:
  cout << "mymap contains:";
  for (auto it = mymap.cbegin(); it != mymap.cend(); ++it)
    cout << " [" << (*it).first << ':' << (*it).second << ']';
  cout << '\n';

  return 0;
}

输出:

mymap contains: [a:200] [b:100] [c:300]

在上面的示例中,cend()函数用于返回一个指向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;


    // Create a map iterator and point to beginning of map
    map<string, int>::iterator it = m.begin();

    // 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。它将在每个map元素上进行迭代,并调用我们提供的回调函数。

示例3

让我们看一个简单的示例,使用while循环来遍历map。

#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

在上面的示例中,cend()函数用于返回一个常量迭代器,该迭代器指向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.cend();

    cout << "end element (point next to the last): ";
    cout << "{" << ite->first << ", "
         << ite->second << "}\n";

  return 0;
  }

输出:

Elements are:
10 + 10 = 20
20 + 20 = 40
30 + 30 = 60
end element (point next to the last): {3, 0}

在上面的示例中,cend()函数用于返回一个常量迭代器,该迭代器指向mymap映射中最后一个元素的下一个位置。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程