Search code examples
c++stliteratorconstruction

C++: How to construct object with two iterators?


I have sample big integer class. It contains dynamic array of digits that comprise the big integer. I would like to construct objects of this class using 2 iterators (begin and end) in order I can pass digits from std::vector or std::list.

Some pseudocode illustrating my idea:

BigInteger(std::iterator begin, std::iterator end);
...

Usage:

std::vector<int> v;
// fill vector with digits
...
BigInteger b(v.begin(), v.end());

The question is: how to declare such constructor correctly? Also even is it possible?

Thanks!


Solution

  • Use a template constructor:

    template<class InputIterator>
    BigInteger( InputIterator begin, InputIterator end )
    

    This should be used like:

    std::vector<int> v; //Fill with values    
    BigInteger( v.begin(), v.end() );