C++ 多映射 rbegin() 函数用于返回引用多映射容器最后一个元素的反向迭代器。
multimap 的反向迭代器以相反的方向移动并递增它,直到它到达 multimap 容器的开头(第一个元素)。
用法
reverse_iterator rbegin();                            //until C++ 11
const_reverse_iterator rbegin() const;                //until C++ 11
      reverse_iterator rbegin() nothrow;	      //since C++ 11
const_reverse_iterator rbegin() const nothrow;    //since C++ 11参数
空
返回值
它返回一个指向多重映射最后一个元素的反向迭代器。
复杂度
恒定。
迭代器有效性
没有变化。
数据竞争
访问容器的常量和非常量版本都不会修改容器。
异常安全
此函数从不抛出异常。
例子1
让我们看看 rbegin() 函数的简单示例:
#include <iostream>
#include <map>
using namespace std;
int main ()
{
  multimap<char,int> mymultimap;
  mymultimap = {
               {'a', 100},
               {'b', 200},
               {'a', 300},
               {'c', 300}
               };
  // show content:
  multimap<char,int>::reverse_iterator rit;
  for (rit=mymultimap.rbegin(); rit!=mymultimap.rend(); ++rit)
    cout << rit->first << " = " << rit->second << '\n';
  return 0;
}输出:
c = 300 b = 200 a = 300 a = 100
在上面的例子中, rbegin() 函数用于返回指向 mymultimap 多映射中最后一个元素的反向迭代器。
由于 multimap 以键的排序顺序存储元素,因此迭代 multimap 将导致上述顺序,即键的排序顺序。
例子2
让我们看一个使用 while 循环以相反顺序迭代 multimap 的简单示例:
#include <iostream>
#include <map>
#include <string>
#include <iterator>
using namespace std;
int main() {
	// Creating & Initializing a multimap of String & Ints
	multimap<string, int> multimapEx = {
			{ "aaa", 10 },
			{ "ddd", 11 },
			{ "ccc", 12 },
			{ "ccc", 13 }
	};
	// Create a multimap iterator and point to the end of multimap
	multimap<string, int>::reverse_iterator it = multimapEx.rbegin();
	// Iterate over the multimap using Iterator till beginning.
	while (it != multimapEx.rend()) {
		// Accessing KEY from element pointed by it.
		string word = it->first;
		// Accessing VALUE from element pointed by it.
		int count = it->second;
		cout << word << "::" << count << endl;
		// Increment the Iterator to point to next entry
		it++;
	}
	return 0;
}输出:
ddd::11 ccc::13 ccc::12 aaa::10
在上面的例子中,我们使用 while 循环以相反的顺序迭代多重映射,并使用 rbegin() 函数初始化多重映射的最后一个元素。
由于 multimap 以键的排序顺序存储元素,因此迭代 multimap 将导致上述顺序,即键的排序顺序。
例子3
让我们看一个简单的例子来获取反向多重映射的第一个元素:
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main ()
{
  multimap<int,int> m1 = {
                { 1, 10},
                { 2, 20 },
                { 3, 30 }, 
                { 3, 40 },
                { 4, 50}
                };         
    auto ite = m1.rbegin();
    cout << "The first element of the reversed multimap m1 is:";
    cout << "{" << ite->first << ", "
         << ite->second << "}\n";
  return 0;
  }输出:
The first element of the reversed multimap m1 is:{4, 50}
在上面的例子中,rbegin() 函数返回反向多映射 m1 的第一个元素,即 {4,50}。
示例 4
让我们看一个简单的例子来排序和计算最高分:
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main ()
{
  multimap<int,int> marks = {
                { 425, 10},
                { 300, 20 },
                { 480, 30 },
                { 300, 40 },
                { 425, 50 }};
   cout << "Marks" << " | " << "Roll Number" << '\n';
   cout<<"______________________\n";
  multimap<int,int>::reverse_iterator rit;
  for (rit=marks.rbegin(); rit!=marks.rend(); ++rit)
    cout << rit->first << "   |  " << rit->second << '\n';
    auto ite = marks.rbegin();
    cout << "\nHighest Marks is:"<< ite->first <<" \n";
    cout << "Roll Number of Topper is:"<< ite->second << "\n";
  return 0;
  }输出:
Marks | Roll Number ______________________ 480 | 30 425 | 50 425 | 10 300 | 40 300 | 20 Highest Marks is:480 Roll Number of Topper is:30
在上面的示例中,实现了多映射标记,其中将卷号存储为值并将标记为键。这使我们能够利用多映射中的自动排序,并让我们识别具有最高标记的元素的滚动数。
相关用法
- C++ multimap rbegin用法及代码示例
- C++ multimap rend()用法及代码示例
- C++ multimap rend用法及代码示例
- C++ multimap key_comp()用法及代码示例
- C++ multimap empty()用法及代码示例
- C++ multimap cend()用法及代码示例
- C++ multimap insert()用法及代码示例
- C++ multimap lower_bound()用法及代码示例
- C++ multimap maxsize()用法及代码示例
- C++ multimap get_allocator()用法及代码示例
- C++ multimap upper_bound()用法及代码示例
- C++ multimap clear()用法及代码示例
- C++ multimap crend()用法及代码示例
- C++ multimap crbegin()用法及代码示例
- C++ multimap key_comp用法及代码示例
- C++ multimap emplace()用法及代码示例
- C++ multimap equal_range()用法及代码示例
- C++ multimap value_comp()用法及代码示例
- C++ multimap erase()用法及代码示例
- C++ multimap size()用法及代码示例
注:本文由纯净天空筛选整理自 C++ multimap rbegin() function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
