C++ 多映射 end() 函数用于返回多映射中最后一个条目旁边的迭代器。
注意:-这是一个占位符。此位置不存在元素,尝试访问是未定义的行为。
用法
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 的最后一个元素旁边。
复杂度
恒定。
迭代器有效性
没有变化。
数据竞争
访问容器的常量和非常量版本都不会修改容器。
异常安全
此成员函数从不抛出异常。
例子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 多映射中最后一个元素旁边的迭代器。
例子2
让我们看一个使用 for-each 循环遍历多重映射的简单示例:
#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 循环迭代 multimap 的简单示例:
#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 多映射中最后一个元素旁边的迭代器。
示例 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 多映射中最后一个元素旁边的迭代器。
相关用法
- C++ multimap empty()用法及代码示例
- C++ multimap emplace()用法及代码示例
- C++ multimap equal_range()用法及代码示例
- C++ multimap erase()用法及代码示例
- C++ multimap key_comp()用法及代码示例
- C++ multimap cend()用法及代码示例
- C++ multimap insert()用法及代码示例
- C++ multimap lower_bound()用法及代码示例
- C++ multimap rend()用法及代码示例
- C++ multimap maxsize()用法及代码示例
- C++ multimap rend用法及代码示例
- C++ multimap get_allocator()用法及代码示例
- C++ multimap upper_bound()用法及代码示例
- C++ multimap clear()用法及代码示例
- C++ multimap crend()用法及代码示例
- C++ multimap crbegin()用法及代码示例
- C++ multimap key_comp用法及代码示例
- C++ multimap rbegin用法及代码示例
- C++ multimap rbegin()用法及代码示例
- C++ multimap value_comp()用法及代码示例
注:本文由纯净天空筛选整理自 C++ multimap end() function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
