C++ - Using array/pointers in vector (or other container) constructor that expects type Iterator. How is that possible? -


below text c++ online course. says constructor of vector class

 template <class inputiterator>           vector ( inputiterator first, inputiterator last, const allocator& = allocator() ); 

can receive pointers first (inputiterator first) , second parameter (inputiterator last).

the next constructor uses iterators initialize itself. create vector copy of values first (inclusive) last (exclusive). in typical case, constructor creates new vector using elements existing collection. due fact iterators defined set of operations instead of type, possible use normal pointers. remark leads conclusion can use normal c++ array initialize collection. , in fact can.

#include <vector> #include <iostream>  using namespace std;  int main() {     int a1[]={1,2,3,4,5,6,7,8,9,10};     //first 1     vector<int> v1(a1, a1+10);     cout<<"size (v1):  "<<v1.size()<<endl;     for(unsigned = 0; < v1.size(); ++i)     {         cout<< v1[i]<<" ";     }     cout<<endl;     //second one;     vector<int> v2(a1+5,a1+10);     cout<<"size (v2):  "<<v2.size()<<endl;     for(unsigned = 0; < v2.size(); ++i)     {         cout<< v2[i]<<" ";     }     cout<<endl;     return 0; } 

i can used this, don't understand why possible. i'd understand technique.

my question in case (see code) how possible can put array address instead of iterator?

in above code element of type int * put parameter of type inputiterator. confuses me.

all story constructs expects iterator. expects able increment (using ++), deference (using *). interator either class overloads ++ , * operator, or basic pointer type naturally supports both operations.


Comments