logo

C++ STL의 반복자

전제조건 : 반복자 소개
반복자는 메모리 주소를 가리키는 데 사용됩니다. STL 컨테이너. 이는 주로 숫자, 문자 등의 시퀀스에 사용됩니다. 프로그램의 복잡성과 실행 시간을 줄여줍니다.

반복자의 작업 :-



1. 시작() :- 이 함수는 다음을 반환하는 데 사용됩니다. 시작 위치 컨테이너의.

2. 끝() :- 이 함수는 다음을 반환하는 데 사용됩니다. ~ 후에 끝 위치 컨테이너의.




// C++ code to demonstrate the working of> // iterator, begin() and end()> #include> #include // for iterators> #include // for vectors> using> namespace> std;> int> main()> {> >vector<>int>>= {1, 2, 3, 4, 5 };> > >// Declaring iterator to a vector> >vector<>int>>::반복자 ptr;> > >// Displaying vector elements using begin() and end()> >cout <<>'The vector elements are : '>;> >for> (ptr = ar.begin(); ptr cout << *ptr << ' '; return 0; }>

>

>

산출:

 The vector elements are : 1 2 3 4 5>

3. 사전() :- 이 기능은 다음과 같은 용도로 사용됩니다. 반복자 위치를 증가시킵니다. 인수에 언급된 지정된 숫자까지.




// C++ code to demonstrate the working of> // advance()> #include> #include // for iterators> #include // for vectors> using> namespace> std;> int> main()> {> >vector<>int>>= {1, 2, 3, 4, 5 };> > >// Declaring iterator to a vector> >vector<>int>>::반복자 ptr = ar.begin();> > >// Using advance() to increment iterator position> >// points to 4> >advance(ptr, 3);> > >// Displaying iterator position> >cout <<>'The position of iterator after advancing is : '>;> >cout << *ptr <<>' '>;> > >return> 0;> > }>

>

>

산출:

 The position of iterator after advancing is : 4>

4.다음() :- 이 기능 새로운 반복자를 반환합니다. 반복자가 다음을 가리킬 것입니다. 지위를 발전시키는 것 주장에서 언급했습니다.

5. 이전() :- 이 기능 새로운 반복자를 반환합니다. 반복자가 가리킬 것이라고 위치를 감소시킨 후 주장에서 언급했습니다.




// C++ code to demonstrate the working of> // next() and prev()> #include> #include // for iterators> #include // for vectors> using> namespace> std;> int> main()> {> >vector<>int>>= {1, 2, 3, 4, 5 };> > >// Declaring iterators to a vector> >vector<>int>>::반복자 ptr = ar.begin();> >vector<>int>>::반복자 ftr = ar.end();> > > >// Using next() to return new iterator> >// points to 4> >auto> it = next(ptr, 3);> > >// Using prev() to return new iterator> >// points to 3> >auto> it1 = prev(ftr, 3);> > >// Displaying iterator position> >cout <<>'The position of new iterator using next() is : '>;> >cout << *it <<>' '>;> >cout << endl;> > >// Displaying iterator position> >cout <<>'The position of new iterator using prev() is : '>;> >cout << *it1 <<>' '>;> >cout << endl;> > >return> 0;> }>

>

부스 알고리즘
>

산출:

 The position of new iterator using next() is : 4 The position of new iterator using prev() is : 3>


6. 삽입()
:- 이 기능은 다음과 같은 용도로 사용됩니다. 임의의 위치에 요소 삽입 컨테이너에. 그것은 받아들인다 인수 2개, 요소를 삽입해야 하는 위치에 대한 컨테이너 및 반복자 .




// C++ code to demonstrate the working of> // inserter()> #include> #include // for iterators> #include // for vectors> using> namespace> std;> int> main()> {> >vector<>int>>= {1, 2, 3, 4, 5 };> >vector<>int>>ar1 = {10, 20, 30};> > >// Declaring iterator to a vector> >vector<>int>>::반복자 ptr = ar.begin();> > >// Using advance to set position> >advance(ptr, 3);> > >// copying 1 vector elements in other using inserter()> >// inserts ar1 after 3rd position in ar> >copy(ar1.begin(), ar1.end(), inserter(ar,ptr));> > >// Displaying new vector elements> >cout <<>'The new vector after inserting elements is : '>;> >for> (>int> &x : ar)> >cout << x <<>' '>;> > >return> 0;> }>

>

>

산출:

 The new vector after inserting elements is : 1 2 3 10 20 30 4 5>

반복자의 유형:

  1. 입력 반복자
  2. 출력 반복자
  3. 정방향 반복자
  4. 양방향 반복자
  5. 무작위 액세스 반복자