find in <algorithm>

Feb 17, 2012 at 6:01am
Hello. I am quite confused with the "find" function in <algorithm>.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <list>
#include <algorithm>
#include <string>

using namespace std;

struct here
{
	int name;
	string add;
};

int main()
{
	list<here> a;
	here an, bn;
	an.name = 123;
	an.add = "aldhfal";
	bn.name = 222;
	bn.add = "alkdhflahf";
	a.push_back(an);
	a.push_back(bn);
	list<here>::iterator k, s;
	k = find(a.begin(), a.end(), an);
        s = a.front();
}

Neither the last nor the second last statement appear to be normal. What is the matter?

Thanks in advance!
Feb 17, 2012 at 6:53am
When you say "not normal", do you mean that they don't compile?
The last statement is wrong because front() method returns a here&. To get an iterator, use begin(). I don't know what's wrong with line 25. What are the errors?
Feb 17, 2012 at 7:29am
In order to use 'here' in find() it needs an 'bool operator==(const here &h) const' in 'here' otherwise find() does not know how to compare instances of 'here'
Feb 25, 2012 at 8:19pm
Thanks coder777 and hamsterman! Combining your suggestions, I can now run the code!
Topic archived. No new replies allowed.