亚洲区国产区激情区无码区,国产成人mv视频在线观看,国产A毛片AAAAAA,亚洲精品国产首次亮相在线

C++ map crbegin() 函數(shù)使用方法及示例

C++ STL map(容器)

C ++ map crbegin()函數(shù)用于返回引用map容器中最后一個元素的常量反向迭代器。

常量map的反向迭代器將反向移動并遞增,直到到達map容器的開頭(第一個元素)并指向常量元素。

語法

const_reverse_iterator crbegin() const noexcept; //從 C++ 11 開始

參數(shù)

沒有

返回值

它返回一個常數(shù)反向迭代器,指向map的最后一個元素。

實例1

讓我們看一個簡單的crbegin()函數(shù)示例。

#include <iostream>
#include <map>

using namespace std;

int main ()
{
  map<char,int> mymap;

  mymap['b'] = 100;
  mymap['a'] = 200;
  mymap['c'] = 300;

  cout << "以相反的順序排列mymap:";
  for (auto rit = mymap.crbegin(); rit != mymap.crend(); ++rit)
    cout << " [" << rit->first << ':' << rit->second << ']';
  cout << '\n';

  return 0;
}

輸出:

以相反的順序排列mymap: [c:300] [b:100] [a:200]

在上面的示例中,使用crbegin()函數(shù)返回一個常數(shù)反向迭代器,該迭代器指向mymap容器中的最后一個元素。

因為map按鍵的排序順序存儲元素。因此,在map上進行迭代將導致上述順序,即鍵的排序順序。

實例2

讓我們看一個簡單的示例,使用while循環(huán)以相反的順序遍歷map。

#include <iostream>
#include <map>
#include <string>
#include <iterator>

using namespace std;
 
int main() {
 
	// 創(chuàng)建和初始化string和int的map
	map<string, int> mapEx = {
			{ "aaa", 10 },
			{ "ddd", 11 },
			{ "bbb", 12 },
			{ "ccc", 13 }
	};
 
	// 創(chuàng)建一個map迭代器并指向map的末尾
	 map<string, int>::const_reverse_iterator it = mapEx.crbegin();
 
	// 使用Iterator迭代map直到開始。
	while (it != mapEx.crend()) {
		//從其指向的元素訪問KEY。
		string word = it->first;
 
		//從它所指向的元素中訪問VALUE。
		int count = it->second;
 
		cout << word << " :: " << count << endl;
 
		//增加迭代器以指向下一個條目
		it++;
	}
	return 0;
}

輸出:

ddd :: 11
ccc :: 13
bbb :: 12
aaa :: 10

在上面的示例中,我們使用while循環(huán)以相反的順序?qū)ap進行const_iterate,并使用crbegin()函數(shù)初始化map的最后一個元素。

因為map因此按鍵的排序順序存儲元素,所以在map上進行迭代將導致上述順序,即鍵的排序順序。

實例3

讓我們看一個簡單的示例,以獲取反向map的第一個元素。

#include <iostream>
#include <string>
#include <map>


using namespace std;

int main ()
{
  map<int,int> m1 = {
                { 1, 10},
                { 2, 20 },
                { 3, 30 } };
          
    auto ite = m1.crbegin();
 
    cout << "反向map容器m1的第一個元素是:";
    cout << "{" << ite->first << ", "
         << ite->second << "}\n";

  return 0;
  }

輸出:

反向map容器m1的第一個元素是: {3, 30}

在上面的示例中,crbegin()函數(shù)返回反轉(zhuǎn)map容器m1的第一個元素,即{3,30}。

實例4

讓我們看一個簡單的示例,對最高分進行排序和計算。

#include <iostream>
#include <string>
#include <map>
using namespace std;

int main ()
{
  map<int,int> marks = {
                { 400, 10},
                { 312, 20 },
                { 480, 30 },
                { 300, 40 },
                { 425, 50 }};


   cout << "Marks" << " | " << "Roll Number" << '\n';
   cout<<"______________________\n";
   
  map<int,int>::const_reverse_iterator rit;
  for (rit=marks.crbegin(); rit!=marks.crend(); ++rit)
    cout << rit->first << "   |  " << rit->second << '\n';

    auto ite = marks.crbegin();
 
    cout << "\n最高分是: "<< ite->first <<" \n";
    cout << "Topper的卷數(shù)是: "<< ite->second << "\n";

  return 0;
  }

輸出:

Marks | Roll Number
______________________
480   | 30
425   | 50
400   | 10
312   | 20
300   | 40

最高分是: 480 
Topper的卷數(shù)是: 30

在上面的示例中,實現(xiàn)了map標記,其中將“卷號(Roll Number)”存儲為值,并將標記存儲為鍵。這使我們能夠利用map中的自動排序功能,并使我們能夠識別標記最高的元素的卷號。

C++ STL map(容器)