C++ multimap end() 函数

C++ multimap end() 函数

C++ multimap end() 函数用于返回一个迭代器,该迭代器位于 multimap 的最后一个元素的下一个位置。

注意:- 这是一个占位符。在此位置不存在任何元素,并且尝试访问它将导致未定义的行为。

语法

      iterator end();                            //until C++ 11
const_iterator end() const;                //until C++ 11
      iterator end() noexcept;              //since C++ 11
const_iterator end() const noexcept;  //since C++ 11

参数

返回值

它返回一个迭代器,该迭代器指向multimap的最后一个元素之后的位置。

复杂度

常数

迭代器有效性

没有改变

数据竞争

既不修改const版本也不修改非const版本访问容器

异常安全

该成员函数永远不会抛出异常

示例1

让我们看一下end()函数的简单示例:

#include <iostream>
#include <map>
using namespace std;
int main ()
{
  multimap<char,string> mymultimap;
  mymultimap = { 
               {'a',"Java"},
               {'b', "C++"},
               {'b', "Python"},
               {'a', "Android"}
               };
  // show content:
  for (multimap<char,string>::iterator it=mymultimap.begin(); it!=mymultimap.end(); ++it)
    cout << it->first << " => " << it->second << '\n';
  return 0;
}

输出:

a => Java
a => Android
b => C++
b => Python

在上面的示例中,end()函数用于返回一个迭代器,指向mymultimap multimap中最后一个元素的下一个位置。

示例2

让我们看一个简单的示例,使用for-each循环遍历multimap:

#include <iostream>
#include <map>
#include <string>
#include <iterator>
#include <algorithm>
using namespace std;
int main() {
      multimap<string, int> m;
  m= { 
     {"Room1", 100},
     {"Room2", 200},
     {"Room1", 300},
     {"Room1", 100} 
     };
    // Create a multimap iterator and point to beginning of multimap
    multimap<string, int>::iterator it = m.begin();
    // Iterate over a multimap using std::for_each and Lambda function  
        for_each(m.begin(), m.end(),
                [](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
Room1 = 300
Room1 = 100
Room2 = 200

在上面的示例中,我们使用STL算法std::for-each来遍历multimap。它将迭代多映射的每个元素,并调用我们提供的回调函数。

示例3

让我们看一个简单的示例,使用while循环来迭代多映射:

#include <iostream>
#include <map>
#include <string>
int main()
{
    using namespace std; 
      multimap<int,string> mymultimap = {
                { 100, "Nikita"},
                { 200, "Deep"  },
                { 200, "Priya" },
                { 400, "Suman" },
                { 300, "Aman"  }}; 
    multimap<int, string>::const_iterator it; // declare an iterator
    it = mymultimap.begin(); // assign it to the start of the vector
    while (it != mymultimap.end()) // 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;
    return 0;
}

输出:

100 = Nikita
200 = Deep
200 = Priya
300 = Aman
400 = Suman

在上面的示例中,end()函数用于返回一个迭代器,该迭代器指向mymultimap multimap中最后一个元素的下一个位置。

示例4

让我们看一个简单的示例:

#include <iostream>
#include <string>
#include <map>
using namespace std;
int main ()
{
  multimap<int,int> mymultimap = {
                { 10, 10},
                { 20, 20 },
                { 10, 30 },
                { 20, 10} 
                };                         
  cout<<"Elements are:" <<endl;
    for (multimap<int,int>::iterator it=mymultimap.begin(); it!=mymultimap.end(); ++it)
    cout << it->first 
    << " * " 
    << it->second 
    << " = "
    <<it->first * it->second
    << '\n';
  return 0;
  }

输出:

Elements are:
10 * 10 = 100
10 * 30 = 300
20 * 20 = 400
20 * 10 = 200

在上面的示例中,end() 函数被用来返回一个指向 mymultimap multimap 中最后一个元素之后的迭代器。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程