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

C++ map end() 함수 사용 방법 및 예제

C++ STL map(컨테이너)

C ++ map end()함수는 map의 마지막 항목 옆에 위치한 이터레이터를 반환합니다.

문법

 iterator end(); // C에서++ 11 이전
const_iterator end() const; // C에서++ 11 이전
 iterator end() noexcept; //C에서++ 11 시작
const_iterator end() const noexcept;  //C에서++ 11 시작

파라미터

없음

반환 값

그것은 map의 마지막 요소 옆의 이터레이터를 반환합니다.

예제1

간단한 end() 함수 예제를 보여드리겠습니다.

#include <iostream>
#include <map>
using namespace std;
int main ()
{
  map<char,string> mymap;
  mymap['b'] = "Java";
  mymap['a'] = "C++";
  mymap['c'] = "SQL";
  for (map<char,string>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
    cout << it->first << " => " << it->second << '
';
  return 0;
}

출력:

a => C++
b => Java
c => SQL

위에서, end() 함수는 mymap 컨테이너의 마지막 요소 옆의 이터레이터를 반환합니다.

예제2

간단한 예제를 보여드리겠습니다. for 사용-each 루프로 map를 순회합니다.

#include <iostream>
#include <map>
#include <string>
#include <iterator>
#include <algorithm>
 
using namespace std;
 
int main() 
{
 map<string, int> m;
 m["룸1"] = 100;
 m["룸2"] = 200;
 m["룸3"] = 300;
	// 맵의 시작점을 가리키는 맵 이터레이터를 생성합니다
	map<string, int>::iterator it = m.begin();
	//각 요소에 대해 Lambda 함수와 함께 std::迭代 map을 사용합니다
	for_each(m.begin(), m.end(), [](pair<string, int> element){
		string word = element.first;
		int count = element.second;
		cout << word << " = " << count << endl;
	});
	return 0;
}

출력:

룸1 = 100
룸2 = 200
룸3 = 300

위의 예제에서는 STL 알고리즘 std::for를 사용했습니다.-each를 사용하여 map를 순회합니다. 이는 각 map 요소에서 이터너트하고 제공된 콜백을 호출합니다.

예제3

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.begin(); //그것을 벡터의 시작점에 할당합니다
    while (it != mymap.end())
    {
            cout << it->first << " = " << it->second << "\n"; 
            // 지정된 요소의 값을 출력합니다
            ++it; //다음 요소로 이동합니다
    }
 
    cout << endl;
}

출력:

100 = Nikita
200 = Deep
300 = Priya
400 = Suman
500 = Aman

위의 예제에서 end() 함수는 mymap 컨테이너의 마지막 요소 뒤의 이터레이터를 반환합니다.

예제4

간단한 예제를 보겠습니다.

#include <iostream>
#include <string>
#include <map>
using namespace std;
int main ()
{
  map<int, int> mymap = {
                { 10, 10},
                { 20, 20 },
                { 30, 30 };
    cout << "요소는:" << endl;
 
    for (map<int,int>::iterator it=mymap.begin(); it!=mymap.end();} ++it){
            cout << it->first 
            << " * " 
            << it->second 
            << " = "
            <<it->first * it->second
            << '\n';    
    }
  return 0;
  
  }

출력:

요소는:
10 * 10 = 100
20 * 20 = 400
30 * 30 = 900

위의 예제에서 end() 함수는 mymap 컨테이너의 마지막 요소 뒤의 이터레이터를 반환합니다.

C++ STL map(컨테이너)