How to find an object in a vector of objects ?

Hi there,
The class packetId is implemented as follows. I have declared a vector of packetId and want to find an object of type packetId in that vector. As I cannot do it by std::find, do you have any other suggestion ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// class PacketId
class PacketId
{
public:
	// data members 
	uint64_t m_nodeId;
	int m_index;
	// constructors and a destructor
	PacketId ();
	PacketId (uint64_t nodeId, int index);
	~PacketId ();
	//member functions 
	int GetIndex () const;
	void SetIndex (int index);
	uint64_t GetNodeId () const;
	void SetNodeId (uint64_t nodeId);
	bool Equals (const PacketId& pktId) const;
};


Thanks in advance,
Ali
You can use std::find() if you define operator==() for your class:
1
2
3
4
5
6
7
8
9
10

	// Rather than do this:
	// bool Equals (const PacketId& pktId) const;

	// may as well define the relevant operator. That will enable
	// std::find() to work.
	bool operator==(const PacketId& p) const
	{
		return m_nodeId == p.m_nodeId && m_index == p.m_index;
	}
Thank you so much dear Galik :-)
Topic archived. No new replies allowed.