C++ multimap crend() 函数用于以相反的顺序返回一个常量迭代器到multimap的末尾(不是最后一个元素,而是过去的最后一个元素)。这类似于非逆向容器的第一个元素之前的元素。
注意:-这是一个占位符。此位置不存在元素,尝试访问是未定义的行为。
常量迭代器是指向常量内容的迭代器。
用法
const_reverse_iterator crend() const noexcept;       //since C++ 11参数
空
返回值
它将 const_reverse_iterator 返回到反向容器的最后一个元素之后的元素。
复杂度
恒定。
迭代器有效性
没有变化。
数据竞争
容器被访问。
异常安全
此函数从不抛出异常。
例子1
让我们看看 crend() 函数的简单示例:
#include <iostream>
#include <map>
using namespace std;
int main ()
{
  multimap<char,int> mymultimap;
  mymultimap= {
            {'a', 100},
            {'a', 200},
            {'c', 300},
            {'b', 400}
           };
  // show content:
  multimap<char,int>::const_reverse_iterator rit;
  for (rit=mymultimap.crbegin(); rit!=mymultimap.crend(); ++rit)
    cout << rit->first << " = " << rit->second << '\n';
  return 0;
}输出:
c = 300 b = 400 a = 200 a = 100
在上面的例子中, crend() 函数用于返回一个常量反向迭代器到反向容器的最后一个元素之后的元素。
由于 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 },
			{ "aaa", 12 },
			{ "ccc", 13 }
	};
	// Create a multimap iterator and point to the end of multimap
	multimap<string, int>::const_reverse_iterator it = multimapEx.crbegin();
	// Iterate over the multimap using Iterator till beginning.
	while (it != multimapEx.crend()) {
		// 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 aaa::12 aaa::10
在上面的例子中,我们在 multimap 上以相反的顺序使用 while 循环到 const_iterate。
由于 multimap 以键的排序顺序存储元素,因此迭代 multimap 将导致上述顺序,即键的排序顺序。
例子3
让我们看一个简单的例子:
#include <iostream>
#include <map>
using namespace std;
int main(void) {
   multimap<char, int> m = {
            {'a', 1},
            {'b', 2},
            {'a', 3},
            {'d', 4},
            {'d', 5},
            };
   cout << "Multimap contains following elements in reverse order:" << endl;
   for (auto it = m.crbegin(); it != m.crend(); ++it)
      cout << it->first << " = " << it->second << endl;
   return 0;
}输出:
Multimap contains following elements in reverse order: d = 5 d = 4 b = 2 a = 3 a = 1
在上面的例子中,multimap 的元素以相反的顺序返回。
示例 4
让我们看一个简单的例子来排序和计算最高分:
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main ()
{
  multimap<int,int> emp = {
                { 1000, 10},
                { 2500, 20 },
                { 3000, 30 },
                { 3000, 40 },
                { 5500, 50 }};
   cout << "Salary" << " | " << "ID" << '\n';
   cout<<"______________________\n";
  multimap<int,int>::const_reverse_iterator rit;
  for (rit=emp.crbegin(); rit!=emp.crend(); ++rit)
    cout << rit->first << "   |  " << rit->second << '\n';
    auto ite = emp.crbegin();
    cout << "\nHighest salary:"<< ite->first <<" \n";
    cout << "ID is:"<< ite->second << "\n";
  return 0;
  }输出:
Salary | ID ______________________ 5500 | 50 3000 | 40 3000 | 30 2500 | 20 1000 | 10 Highest salary:5500 ID is:50
在上面的示例中,实现了一个多映射 emp,其中 ID 存储为值,salary 存储为键。这使我们能够利用 multimaps 中的自动排序,并让我们能够识别具有最高薪水的元素的 ID。
相关用法
- C++ multimap crbegin()用法及代码示例
- C++ multimap cend()用法及代码示例
- C++ multimap clear()用法及代码示例
- C++ multimap cbegin()用法及代码示例
- C++ multimap key_comp()用法及代码示例
- C++ multimap empty()用法及代码示例
- 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 key_comp用法及代码示例
- C++ multimap emplace()用法及代码示例
- C++ multimap equal_range()用法及代码示例
- C++ multimap rbegin用法及代码示例
- C++ multimap rbegin()用法及代码示例
- C++ multimap value_comp()用法及代码示例
- C++ multimap erase()用法及代码示例
注:本文由纯净天空筛选整理自 C++ multimap crend() function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
