English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

تعليميات C++ الأساسية

تحكم في العملية C ++

وظيفة مخصصة في C++

مجموعات بيانات C ++ & سلاسل

هياكل بيانات C ++

مفاهيم C ++ & الأشياء

المراجع في C++

الوراثة في C++

دليل C++ STL

دليل مرجعي C ++

استخدام دالة crend() في C ++ map وأمثلة

map (المساحة) في C++ STL

C ++ map crend()الوظيفة تستخدم لتحويلال迭代ر المستقربالترتيب المعكوسالعودة إلى نهاية map (العودة إلى دالة استنساخ عكسية، تشير إلى العنصر الافتراضي قبل أول عنصر). هذا يشبه العنصر قبل أول عنصر في وعاء غير معكوس.

الاحتياطية: هذا هو مكان استبدال. لا يوجد عنصر في هذا الموضع، والوصول إليه هو سلوك غير معرف.
ال迭代ر المستقر هو دالة تشير إلى محتوى ثابت.

القواعد

const_reverse_iterator crend() const noexcept; // بدءاً من C++ 11

المتغيرات

لا يوجد

النتيجة

يقوم بتقديم const_reverse_iterator إلى العنصر التالي للعنصر الأخير في المحتويات العكسية.

مثال 1

دعونا نرى مثالاً بسيطاً لدالة crend().

#include <iostream>
#include <map>
using namespace std;
int main ()
{
  map<char, int> mymap;
  
  mymap['x'] = 100;
  mymap['y'] = 200;
  mymap['z'] = 300;
  // عرض محتوى:
  map<char, int>::const_reverse_iterator rit;
  for (rit=mymap.crbegin(); rit!=mymap.crend(); ++rit)
    cout << rit->first << " = " << rit->second << '\n';
  return 0;
}

الخروج:

z = 300
y = 200
x = 100

في المثال السابق، تم استخدام دالة crend() لتقديم م迭代ر عكسي إلى العنصر التالي للعنصر الأخير في القالب العكسي.

Because the map stores elements in the order of the key's sorting sequence, iterating over the map will result in the above order, that is, the order of the keys.

مثال 2

دعونا نرى مثالاً بسيطاً يستخدم دائرة while لاستبدال ترتيب map.

#include <iostream>
#include <map>
#include <string>
#include <iterator>
using namespace std;
 
int main() {
 
	// يتم إنشاء وتحديد map من String و Ints
	map<string, int> mapEx = {
			{"aaa", 10},
			{"ddd", 11},
			{"bbb", 12},
			{"ccc", 13}
	};
 
	// يتم إنشاء م迭代ر وإشراكه في نهاية map
	map<string, int>::const_reverse_iterator it = mapEx.crbegin();
 
	// يستخدم Iterator للاستدلال على map حتى البداية.
	while (it != mapEx.crend()) {
		string word = it->first;
		int count = it->second;
		cout << word << " :: " << count << endl;
		it++;
	}
	return 0;
}

الخروج:

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

في المثال السابق، استخدمنا دائرة while لاستبدال ترتيب map باستخدام const_iterate.

Because the map stores elements in the order of the key's sorting sequence, iterating over the map will result in the above order, that is, the order of the keys.

Example 3

Let's look at a simple example.

#include <iostream>
#include <map>
using namespace std;
int main(void) {
   /* Initializer_list constructor */
   map<char, int> m = {
            {'a', 1},
            {'b', 2},
            {'c', 3},
            {'d', 4},
            {'e', 5},
            };
   cout << "The map contains the following elements in reverse order:" << endl;
   for (auto it = m.crbegin(); it != m.crend(); ++it)
      cout << it->first << " = " << it->second << endl;
   return 0;
}

الخروج:

The map contains the following elements in reverse order:
e = 5
d = 4
c = 3
b = 2
a = 1

In the above example, the elements of the map are returned in reverse order.

Example 4

Let's look at a simple example, sorting and calculating the highest score.

#include <iostream>
#include <string>
#include <map>
using namespace std;
int main ()
{
  map<int,int> emp = {
                { 1000, 10 },
                { 2500, 20 },
                { 4500, 30 },
                { 3000, 40 },
                { 5500, 50 };
   cout << "Salary 		| " << "ID" << '\n';
   cout << "______________________\n";
   
  map<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;
  }

الخروج:

الرواتب | الرقم التعريفي
______________________
5500 | 50
4500 | 30
3000 | 40
2500 | 20
1000 | 10
أعلى رواتب: 5500 
الرقم التعريفي is: 50

في المثال أعلاه، تم إنشاء مساحة map تحت اسم emp، حيث يتم تخزين الرقم التعريفي كقيمة، والرواتب كحالة. مما يتيح لنا استخدام وظيفة الترتيب التلقائي في map، وبإمكاننا تحديد الرقم التعريفي للعنصر الأعلى الرواتب.

map (المساحة) في C++ STL