C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ multiset crend()

C++ multiset crend()

C++ multiset crend()函数用于将常量迭代器返回到 multiset 的末尾(而不是最后一个)元素,但过去的最后一个元素)的顺序相反。这类似于非反转容器的第一个元素之前的元素。
注意: 注意:-这是一个占位符。此位置没有元素,尝试访问是未定义的行为。
常量迭代器是指向常量内容的迭代器。

语法

const_reverse_iterator crend() const noexcept;       //since C++ 11

参数

返回值

crend()函数将const_reverse_iterator返回到以下元素反向容器的最后一个元素。

复杂度

常量。

迭代器有效性

没有更改。

数据竞争

容器已被访问。
同时访问 multiset 容器的元素是安全的。

异常安全

此函数从不抛出异常。

示例1

让我们看一下简单crend()函数的示例:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
  multiset<int> mymultiset = {40,20,30,10,20};
  cout << "mymultiset in reverse order:";
  for (auto rit=mymultiset.crbegin(); rit != mymultiset.crend(); ++rit)
    cout << ' ' << *rit;
  cout << '\n';
  return 0;
}
输出:
mymultiset in reverse order: 40 30 20 20 10
在上面的示例中,crend()函数用于将恒定的反向迭代器返回到反转容器的最后一个元素之后的元素。
因为 multiset 按键的排序顺序存储元素因此,对一个 multiset 进行迭代将导致上述顺序,即键的排序顺序。

示例2

让我们看一个简单的示例,对它进行反向迭代使用while循环订购:
#include <iostream>
#include <set>
#include <string>
#include <iterator>
using namespace std;
 
int main() {
 
    // Creating & Initializing a multiset of String & Ints
    multiset<string> multisetEx = {"ccc", "bbb", "aaa", "bbb"};
 
    // Create a multiset iterator and point to the end of multiset
    multiset<string>::const_reverse_iterator it = multisetEx.crbegin();
 
    // Iterate over the multiset using Iterator till beginning.
    while (it != multisetEx.crend()) {
        // Accessing KEY from element pointed by it.
        string word = *it;
 
        cout << word << endl;
 
        // Increment the Iterator to point to next entry
        it++;
    }
    return 0;
}
输出:
ccc
bbb
bbb
aaa
在上面的示例中,我们使用while循环以相反的顺序对 multiset 进行const_iterate。
由于 multiset 按键的排序顺序存储元素,因此对 multiset 进行迭代将导致顺序,即键的排序顺序。

示例3

让我们看一个简单的示例:
#include <iostream>
#include <set>
#include <algorithm>
using namespace std;
int main()
{
  multiset<int> c = {3, 1, 2, 2};
  for_each(c.crbegin(), c.crend(), [](const int& x) {
    cout << x << endl;
  });
}
输出:
3
2
2
1
在上面的示例中, multiset 元素以相反的顺序返回。

示例4

让我们看一个简单的示例来对最高分进行排序和计算:
#include <iostream>
#include <string>
#include <set>
using namespace std;
int main ()
{
  multiset<int> emp = {1000,2500,4500,1200,2500};
   cout << "Salary"<< '\n';
   cout<<"______________________\n";
   
  multiset<int>::const_reverse_iterator rit;
  for (rit=emp.crbegin(); rit!=emp.crend(); ++rit)
    cout << *rit<< '\n';
    auto ite = emp.crbegin();
 
    cout << "\nHighest salary: "<< *ite <<" \n";
  return 0;
  }
输出:
Salary
______________________
4500
2500
2500
1200
1000
Highest salary: 4500
在上面的示例中,实现了一个 multiset emp,其中薪水存储为键。这使我们能够利用薪资的自动排序功能,并确定最高薪资。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4