Using iterator to initialize string, get "transposed pointer range" exception

Viewed 107

Maybe a noob question but why these two lines:

    vector<char> v{"h","i"};
    string s1(v.cbegin(), v.cend());

won't compile? It says "debug assertion failed, exception:transposed pointer range".

1 Answers

Debug assetions happen in run-time, not compile time.
In any case, you should change:

vector<char> v{"h","i"};

to:

vector<char> v{ 'h','i' };

char literals should be enclosed with ', not ".
This way your code should compile and run properly.

See also @Eljay's comment above for more info how the compiler actually interpreted your current code.

Side note: better to avoid using namespace std - see here Why is "using namespace std;" considered bad practice?.

Related