English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C ++ map cend()함수는 맵의 마지막 요소 뒤에 있는 상수 이터레이터를 반환하는 함수입니다.
const_iterator cend() const noexcept; //C++ 11 시작
없음
그것은 맵의 마지막 요소 뒤에 있는 상수 이터레이터를 반환합니다.
간단한 cend() 함수 예제를 보겠습니다.
#include <iostream> #include <map> using namespace std; int main () { map<char, int> mymap; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; //출력 내용: cout << "mymap contains:"; for (auto it = mymap.cbegin(); it != mymap.cend(); ++it) cout << "[" << (*it).first << ':' << (*it).second << ']'; cout << '\n'; return 0; }
출력:
출력 내용: [a:200] [b:100] [c:300]
위의 예제에서 cend() 함수는 mymap 컨테이너의 마지막 요소 뒤에 있는 이터레이터를 반환합니다.
이 간단한 예제를 통해 for-맵을 each 루프로 순회합니다.
#include <iostream> #include <map> #include <string> #include <iterator> #include <algorithm> using namespace std; int main() { map<string, int> m; m["Room1"] = 100; m["Room2"] = 200; m["Room3"] = 300; // 맵 이터레이터를 생성하고 맵의 시작에 설정합니다 map<string, int>::iterator it = m.begin(); // std::forEach를 사용하여 map를 이터레이션하고 Lambda 함수를 사용합니다. for_each(m.cbegin(), m.cend(), [&](pair<string, int> element){ // KEY에 접근합니다 string word = element.first; //VALUE에 접근합니다. int count = element.second; cout << word << " " = " " << count << endl; }); return 0; }
출력:
Room1 = 100 Room2 = 200 Room3 = 300
위의 예제에서는 STL 알고리즘 std::for를 사용했습니다.-each를 사용하여 map를 순회합니다. 이는 map의 각 요소에 대해 이터레이션하고 제공된 콜백을 호출합니다.
while循环을 사용하여 map를 이터레이션하는 간단한 예제를 보겠습니다.
#include <iostream> #include <map> #include <string> int main() { using namespace std; map<int, string> mymap = { { 100, "Nikita"}}, { 200, "Deep" }}; { 300, "Priya" }, { 400, "Suman" }, { 500, "Aman" }}; map<int, string>::const_iterator it; // 이터레이터를 선언합니다 it = mymap.cbegin(); //빈 배열의 앞에 할당합니다 while (it != mymap.cend()) { cout << it->first << " " = " " << it->second << "\n"; // 지정한 요소의 값을 출력합니다 ++it; // 다음 요소로 이동합니다 } cout << endl; }
출력:
100 = Nikita 200 = Deep 300 = Priya 400 = Suman 500 = Aman
위의 예제에서 cend() 함수는 mymap 컨테이너의 마지막 요소 뒤의 상수 이터레이터를 반환합니다.
간단한 예제를 보겠습니다.
#include <iostream> #include <string> #include <map> using namespace std; int main () { map<int, int> mymap = { { 10, 10}, { 20, 20 }, { 30, 30 } }; cout << "요소는:" << endl; for (auto it = mymap.cbegin(); it != mymap.cend(); ++it) cout << it->first << " " + " << it->second << " " = " " <<it->first + it->second << '\n'; auto ite = mymap.cend(); cout << "끝 요소(마지막을 가리키는): "; cout << "{" << ite->first << ", " << ite->second << "}\n"; return 0; }
출력:
요소는: 10 + 10 = 20 20 + 20 = 40 30 + 30 = 60 끝 요소(마지막을 가리키는): {3, 0}
위의 예제에서 cend() 함수는 mymap 컨테이너의 마지막 요소 뒤의 상수 이터레이터를 반환합니다.