Get an iterator to a specific position in a vector in C++ | Techie Delight

#include <iostream>

#include <vector>

#include <iterator>

using

namespace

std

;

 

int

main

(

)

{

    

vector

<

int

>

ints

=

{

3

,

6

,

1

,

5

,

8

}

;

    

int

index

;

 

    

// using normal iterators

    

index

=

2

;

    

std

::

vector

<

int

>

::

iterator

it

=

std

::

next

(

ints

.

begin

(

)

,

index

)

;

    

cout

<<

“Element at index “

<<

index

<<

” is “

<<

*

it

<<

endl

;

 

    

// using constant iterators

    

index

=

4

;

    

std

::

vector

<

int

>

::

const_iterator

c_itr

=

std

::

next

(

ints

.

cbegin

(

)

,

index

)

;

    

cout

<<

“Element at index “

<<

index

<<

” is “

<<

*

c_itr

;

 

    

return

0

;

}