Jump to content
  • Advertisement
Sign in to follow this  
StarikKalachnikov

stl vector +delete item

This topic is 4757 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I've got a little test app here and I'm having trouble understanding the following. I have a vector containing a class tdNumber, the adding of each class works perfect, but I want to delete an instance named 'num'. How can I do that? Using the find function doesn't work because I get the following error: error C2784: 'bool std::operator ==(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Ax> &' from 'std::allocator<_Ty>::value_type' Can someone tell me how to delete num from the vector?
#include <iostream>
#include <algorithm>
#include <vector>

class tdNumber
{
public:
  tdNumber(int aNum){ number = aNum; }
  virtual ~tdNumber(){ number = 0; }
  
  int number;
};

int main()
{
  using namespace std;
  
  vector<tdNumber> list;
  tdNumber num(2);
  
  list.push_back(tdNumber(1));
  list.push_back(num);
  list.push_back(tdNumber(3));
  list.push_back(tdNumber(4));
  list.push_back(tdNumber(5));
    
  vector<tdNumber>::iterator delItem = find(list.begin(), list.end(), num) ;
  
  
  vector<tdNumber>::iterator Loop;
  for (Loop = list.begin(); Loop != list.end(); Loop++)
    cout << "num: " << (*Loop).number << endl;

  
  return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
In order to use std::find(), the class type should have operator==() defined. Something like:

bool operator==(const tdNumber & lhs, const tdNumber & rhs) {
return lhs.number == rhs.number;
}


[Edited by - SiCrane on October 9, 2005 8:01:21 AM]

Share this post


Link to post
Share on other sites
Thanks alot for the quick answer.

But how can you compare classes if they have more member variables?
Or is it best if every class has a unique identifier?
How about comparing a Player class for example.
Can the comparison be more abstract so it doesn't have to be implemented by every class?

Thanks in advance.

Share this post


Link to post
Share on other sites
Only the class designer knows what criteria should be used to judge of two instances of a class should be considered equal to one another. If you have more variables you want to compare then you should and them together.

Share this post


Link to post
Share on other sites
You can define the operator == how you like if you overload it. Same goes for all the other operators (think std::cout << ). Containers can place restrictions on the behaviour though - std::set and std::map require a strict weak ordering to be defined, which as far as I remember means two things:

If a < b and b < c then a < c
If a < b then !(b < a)

Basically it has to be consistent.

Share this post


Link to post
Share on other sites
If you take the addresses of two objects of the same type and immediately compare them (i.e. do not store one address for some time and later compare it) then providing the type of the objects does not overload the address-of operator and does not have some extremely wierd definition of equality then if both addresses compare equal then both objects are the same object and thus must be equal to each other. The converse is not true. If the addresses do not compare equal you cannot deduce that the objects are not equal.

Enigma

Share this post


Link to post
Share on other sites
NaNs are a good example of a valid case where, though the address of two NaNs may be equal, the NaNs are not.

The following code demonstrates:


#include<limits>
#include<iostream>

int main()
{

float theNan = std::numeric_limits<float>::quiet_NaN();

if(theNan == theNan)
{
std::cout << "Object is equal to itself\n";
}
else
{
std::cout << "Object is not equal to itself\n";
}

if(&theNan == &theNan)
{
std::cout << "Object addresses are equal\n";
}
else
{
std::cout << "Object addresses are not equal\n";
}


}



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!