English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C ++ set cend()함수는 컬렉션의 마지막 요소 옆에 위치한 상수 이터레이터를 반환합니다.
문법 //const_iterator cend() const noexcept;++ 11부터 C
하나상수성는 내용이 일정한 이터레이터를 가리킵니다。
없음
그는 집합의 마지막 요소를 가리키는 상수 이터레이터를 반환합니다。
불변합니다。
변경이 없습니다。
컨테이너가 접근됩니다。
집합의 요소에 동시에 접근하는 것은 안전합니다。
이 멤버 함수는 예외를 투척하지 않습니다。
cend() 함수의 간단한 예제를 보겠습니다:
#include <iostream> #include <set> int main () { std::set<int> myset = {60,20,40,50,10,30}; std::cout << "myset에 포함됩니다:"; for (auto it=myset.cbegin(); it != myset.cend(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; }
출력:
myset에 포함됩니다: 10 20 30 40 50 60
위의 예제에서 cend() 함수는 myset 집합의 마지막 요소 뒤의 이터레이터를 반환합니다.
집합에서 요소를 찾는 간단한 예제를 보겠습니다:
#include <iostream> #include <string> #include <set> using namespace std; int main () { int val; set<int> c = {10,20,30,40,50}; cout<<"찾으려는 값을 입력합니다: "; cin>>val; auto result = c.find(val); //set 요소의 끝까지 검색합니다 if (result != c.cend()) { cout << "요소를 찾았습니다: "<< *result; cout << endl; } cout << "요소를 찾을 수 없습니다。" << endl; } return 0; }
출력:
찾으려는 값을 입력합니다: 10 요소를 찾습니다: 10
while 루프를 사용하여 집합을 순회하는 간단한 예제를 보겠습니다:
#include <iostream> #include <set> #include <string> int main() { using namespace std; set<string> myset = {"Orange", "Banana", "Apple"}; set<string>::const_iterator it; // 이터레이터를 선언합니다 it = myset.cbegin(); // 그를 집합의 시작에 할당합니다 while (it != myset.cend()) { cout << *it <<endl; // 그가 가리키는 요소의 값을 출력합니다 ++it; // 다음 요소로 반복합니다 } cout << endl; }
출력:
Apple Banana Orange
위의 예제에서 cend() 함수는 myset 집합의 마지막 요소 뒤의 이터레이터를 반환합니다.
간단한 예제를 보겠습니다:
#include <iostream> #include <set> #include <algorithm> using namespace std; int main() { set<int> c = {3, 1, 2}); for_each(c.cbegin(), c.cend(), [](const int& x) { cout << x << endl; }); return 0; }
출력:
1 2 3
위의 예제에서 cend() 함수는 myset 집합의 마지막 요소 뒤의 이터레이터를 반환합니다.