Q:

Explain the differences between the constructor that takes a container to copy and the constructor that takes two iterators

0

Explain the differences between the constructor that takes a container to copy and the constructor that takes two iterators.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

The constructor that takes a container to copy requires the container type and the element type of the two containers are both match exactly.

The constructor that takes two iterators requires the element type of the source container must be compatible with the element type of the destiny container For example,

#include <vector>
    #include <list>

    struct A {
    };

    struct B {
        B(A a) {}  // type A can covert to type B
    };

    int main() {
        std::vector<A> va;
        std::vector<B> vb;

        //std::list<A> la(vb.begin(), vb.end());  // Error, convert B to A
        std::list<B> lb(va.begin(), va.end());  // OK, convert A to B

        return 0;
    }

The requirement of the former constructor is stricter than the latter.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now