sort the vector which contain class

I having some problem in sorting. I need to sort things alphabetically.
something similiar like these code...
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
28
29
30
31
32
33
34
class A
{
	protected:
	string name;
		
	public:
	A(string name):name(name){}
	void print()
	{
		cout<<name<<endl;
	}
	string getName()
	{
		return name;
	}
};

class B
{
	vector<A>a1;
	public:
	B(){}
	void setA(A aa)
	{
		a1.push_back(aa);
	}
	void print()
	{
		for(int i=0;i<a1.size();i++)
		{a1[i].print();}
	}
};



Thank you in advance. =)
1
2
3
static bool A_lex_lt(const A& a1,const A& a2) {return a1.getName()<a2.getName();}
[...]
sort(a1.begin(),a1.end(),A_lex_lt);

or

1
2
3
4
5
6
class A {
[...]
  bool operator<(const A& rhs) const {return name<rhs.name;}
}
[...]
sort(a1.begin(),a1.end());


Note that getName should return a constant reference to avoid copying in situations such as these.
can further explain? Thank you.
Athars code is trying to be clever. In his first sample he decalres a function that takes two pointers to pointers to your A class and tests to see if the first argument is greater then the second argument, although this doesn't return anything that you can use for this purpose I think he meant to show you a technique rather then provide functional code. Also with what you have written his code won't work if you just plug it right in.

EDIT: I see what's going on now, the A_lex_lt function is supposed to be your test to see if the letters NEED to be sorted, from there you need to use an if(...) to test the return value an if true then pass to your sort function. Athar seems to be ignoring the fact that you want to use a vector which unless this is part of the assignment is just as well.
Last edited on
miaOnDeLine wrote:
can further explain? Thank you.

What part are you having trouble with? sort can take a custom comparison object as the third argument. If you don't pass one, operator< is used by default. See here as well:
http://www.cplusplus.com/reference/algorithm/sort/

Computergeek01 wrote:
a function that takes two pointers to pointers

I'm not sure where you see any pointers.

Computergeek01 wrote:
first argument is greater then the second argument

Not quite.

Computergeek01 wrote:
although this doesn't return anything that you can use for this purpose I think he meant to show you a technique rather then provide functional code.

Why do you think so? This is actually fully functional code (when you make getName a const function, as it should be).
- First bullet, you are passing variables by address so this declares variables holding the address of what are sending the function, but you're not actually sending those variables although in the end I suppose it's the same. Man this part is hard to explain I guess I'm trying to point out why you are using the '&' symbol

- Blah! I always do that! My line should read
first argument is LESS THEN the second arguement.


- You don't actually give him a way to sort the string, the code can be hard to read for a beginner and they might expect that you've given them everything they need. A user with 10 posts may not be familiar with our policy of not handing over complete solutions in which case it can be frustrating copy and pasting what you have and not seeing results.

- I forgot that sort was a function in the algorithms library.
Last edited on
Topic archived. No new replies allowed.