I have this loop
for(int i=0;i<vec1.size();++i)
{
if(vec1[i]==*p)
{
vec1[i]=*p;
cout<<"element updated"<<endl;
}
else
{
cout<<"push_back"<<endl;
vec1.push_back(*p);
}
}
I’m inserting objects in container class and I’ve overloaded the == to check two parameters inside the object and if they match I want to update the them and if they don’t match I want to put them in the vector, but I don’t seem to be able to properly populate my vector, when I do vec1.size() I get 0 even when I insert 3 objects.
You’re problem is that your
ifis inside your search loop. Yourifwill never be executed, because your loop body never runs, because your.size()will never be greater than 0.Try this:
Or, if you really want to code the loop by hand:
Of course, you might consider changing your container. Using a
std::mapwould shorten your code and reduce the time it takes to manipulate large data sets.