English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C ++ set cbegin()함수는 set 컨테이너의 첫 번째 요소를 가리키는 상수 이터레이터를 반환하는 데 사용됩니다.
const_iterator cbegin() const noexcept; //C++ 11시작
한상수성은 내용이 일정한 이터레이터를 가리킵니다.
없음
그는 집합의 첫 번째 요소를 가리키는 const_iterator를 반환합니다.
불변
변화가 없습니다.
컨테이너가 접근됩니다.
집합의 요소에 동시에 접근하는 것은 안전합니다.
이 멤버 함수는 예외를 일으키지 않습니다.
cbegin() 함수의 간단한 예제를 보겠습니다:
#include <iostream> #include <set> using namespace std; int main () { set<string> myset = {"Java", "C++","SQL"}; // 내용 표시: for (auto it = myset.cbegin(); it != myset.cend(); ++it) cout <<*it << '\n'; return 0; }
출력:
C++ Java SQL
在上面的示例中,cbegin()函数用于返回一个常量迭代器,该迭代器指向myset集中的第一个元素。
다른 간단한 예제를 보겠습니다:
#include <set> #include <iostream> int main( ) { using namespace std; set<int> s1; set<int>::iterator s1_Iter; set<int>::const_iterator s1_cIter; s1.insert( 1 ); s1.insert( 2 ); s1.insert( 3 ); s1_Iter << endl;1.begin( ); cout << "s1 *s1의 첫 번째 요소는 " << s1_Iter << endl;1.begin( ); s1.erase( s1_Iter = s // 이 두 줄은 이터레이터가 const인 데다가 잘못될 수 있습니다 // s1_cIter = s1.begin( ); // s1.erase( s1_cIter(); s1_cIter = s1.begin( ); cout << "s1의 첫 번째 요소는 지금 " << *s1 }
출력:
s1의 첫 번째 요소는 1 s1의 첫 번째 요소는 지금 2
while 루프를 사용하여 집합을 순회하는 간단한 예제를 보겠습니다:
#include <iostream> #include <set> #include <string> int main() { using namespace std; set<string> myset = {"로빈","돌리", "존","니키타"}; set<string>::const_iterator it; // 이터레이터를 선언합니다 it = myset.cbegin(); // 그를 벡터의 시작에 할당합니다 while (it != myset.cend()) { cout << *it << '\n'; // 그가 가리키는 요소의 값을 출력합니다 ++it; // 다음 요소로 이동합니다 } cout << endl; }
출력:
돌리 존 니키타 로빈
위의 예제에서 cbegin() 함수는 myset 집합의 첫 번째 요소를 가리키는 이터레이터를 반환합니다.
다른 간단한 예제를 보겠습니다:
#include <iostream> #include <string> #include <set> using namespace std; int main () { set<int> number = {400, 350, 465, 290, 410}; cout << "좌우 교차: " << '\n'; cout << "______________________\n"; set<int>::const_iterator cit; for (cit = number.cbegin(); cit != number.cend(); ++cit) cout << *cit << '\n'; auto low = number.cbegin(); auto high = number.rbegin(); cout << "\n가장 작은 수는: " << *low << endl; cout << "가장 큰 수는: " <<*high << endl; return 0; }
출력:
증가 순서: ______________________ 290 350 400 410 465 가장 작은 수는: 290 가장 큰 수는: 465
위의 예제에서 cbegin() 함수는 myset 집합의 첫 번째 요소를 가리키는 이터레이터를 반환합니다.