c++ - string::insert produces a runtime error -
i have string b, want reverse it, append result string a. tried gives me runtime error
a.insert(a.end(), b.rbegin(), b.rend())
which is
terminate called after throwing instance of 'std::length_error' what(): basic_string::_m_create
what's problem line of code ?
update : here short program throws same exception :
#include <iostream> #include <string> using namespace std; int main (int argc, const char* argv[]) { string result="bbb"; string tail="aaa"; result.insert(result.end(), tail.rend(), tail.rbegin()); cout << result << endl; return 0; }
i using gcc 5.4.0 on ubuntu 16.0.4
the "insert" you're using doesn't work because:
inserts characters range [first, last) before element pointed pos. overload not participate in overload resolution if inputit not satisfy inputiterator. (since c++11)
the begin() of can't valid pos. case, can use append instead:
a.append(b.rbegin(), b.rend());
Comments
Post a Comment