Lets say I have following code
std::vector<char> myvector = {'u', 'd', 'l', 'r', 'u'};
std::vector<char>::iterator it;
std::cout << "vector size = " << myvector.size() << std::endl;
std::cout << "myvector.begin() = " << *(myvector.begin()+1) << std::endl;
it = find (myvector.begin() + 1, myvector.end(), *myvector.begin());
if (it != myvector.end())
{
myvector.erase(myvector.begin());
myvector.erase(it);
std::cout << "Found and deleted " << '\n';
std::cout << "vector size = " << myvector.size() << std::endl;
}
else
std::cout << "Element not found in myvector\n";
By using myvector.begin() + 1 in this line I do get a segmentation fault, and I have no idea why.
it = find (myvector.begin() + 1, myvector.end(), *myvector.begin());
Normally I should be able to increment the iterator by arithmetic. I even tried other solutions like std::advance or std::next but with no success.
Any suggestions?
@And yeah, what I want to achieve is check if the element has it duplicate and remove them both
Aucun commentaire:
Enregistrer un commentaire