I encountered some weird behavior of insert function for std::vector using iterators.
What I'm trying to do is basically input the strings from text file in the alphabetical order, but when I use std::vector::insert function with strings I get an Segmentation fault (core dumped) message. std::vector::push_back works without any problems.
In other words, performing operation like this works:
std::vector<int> intVec;
intVec.push_back(1);
std::vector<int>::iterator it = intVec.begin();
intVec.insert(it, 5);
And operation like this causes the Segmentation fault:
std::vector<std::string> strVec;
std::vector<std::string>::iterator it = strVec.begin();
strVec.push_back("some string");
strVec.insert(it, "some other string");
Inserting using variables like std::sting var = "some string" does not work either, nor using c_str() function.
Does anyone possibly knows why that problem may occur? Thanks for any explanation.
P.S. I can solve sorting problem using different mechanisms, just want to figure out what's going on here.