/*Erased the address of element in first iteration but in next iteration list is trying to erase the same address of element. For example :- I tried to erase duplicate element from list So Initially my list looked like 1,2,3,4,2,3,5,6 So in first iteration i deleted 2 if the address of element is not same. In cout it's printing 1,3,4,5,6 but for next iteration i m getting segfault. When i checked in debug i found in next iteration again it is trying to delte 2 which was already deletedd. As per my understanding list should get updated after erase then why in next iteration it's getting element 2 ?. */
int main()
{
vector<pair<int,int>> v1 = {make_pair(1,2),make_pair(3,4)};
vector<pair<int,int>> v2 = {make_pair(2,3),make_pair(5,6)};
list<int>v3;
list<int>::iterator it;
list<int>::iterator itip;
vector<pair<int,int>> v4;
int count = 0;
for (auto &i :v1)
{
v3.push_back(i.first);
v3.push_back(i.second);
}
for (auto &i :v2)
{
v3.push_back(i.first);
v3.push_back(i.second);
}
for (itip=v3.begin(); itip != v3.end(); itip++ )
{
for (it = v3.begin() ; it != v3.end(); it++ )
{
if (*itip == *it)
{
if (itip != it )
{
v3.erase(itip);
v3.erase(it);
cout<<"List After erased"<<endl;
for (auto &i : v3)
{
cout<<i<<"\t";
}
cout<<"List After erased"<<endl;
break;
}
}
}
}
cout<<endl;
for (auto &i : v3)
{
cout<<i<<"\t";
}
return 0;
}