stl vector +delete item

Started by
7 comments, last by Mxz 18 years, 6 months ago
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;
}

Next time I give my advice, I'll buy some bubblegum so I won't your kick ass!
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]
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.
Next time I give my advice, I'll buy some bubblegum so I won't your kick ass!
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.
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.
If I compare the addresses of two objects and they are the same, is that enough to guarantee the two are equal?

Thanks
Next time I give my advice, I'll buy some bubblegum so I won't your kick ass!
No.
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
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";  }}

This topic is closed to new replies.

Advertisement