Sorting a float in a vector of objects

I've a vector of objects that I can get my float value from by accessing it through a method.It's not a pointer of vector of objects though.

Meaning it's something like vector[i].getFloatValue().

However,how do I sort the float values by descending order since I access it through a method?

If you have a vector of objects which do have a getFloatValue() member function, then ypor example is correct.

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
35
36
37
#include <iostream>
#include <algorithm>
#include <vector>

class foo
{
    float f;
public:
	foo(float f_) : f(f_) {}
	float getFloatValue() const {return f;}
};

struct foo_comparer
{
	bool operator()(const foo& lhs, const foo& rhs)
	{
		return lhs.getFloatValue() > rhs.getFloatValue();
	}
};

int main()
{
	std::vector<foo> var {1.f, -3.14f, .12f};
	std::cout << var[1].getFloatValue() << '\n';
	//Sorting:
	//Ascending using lambdas:
	std::sort(var.begin(), var.end(), [](const foo& lhs, const foo& rhs)
						{return lhs.getFloatValue() < rhs.getFloatValue();});
	for(auto s: var)
		std::cout << s.getFloatValue() << ' ';
	std::cout << '\n';
	//Descending using function object:
	std::sort(var.begin(), var.end(), foo_comparer());
	for(auto s: var)
		std::cout << s.getFloatValue() << ' ';
	std::cout << '\n';
}
-3.14
-3.14 0.12 1
1 0.12 -3.14
But, I access my float values in another file through a method like Vector[i].getFloatValues(). This method's in another class file.

So if i'm in main and i want to sort by descending, would this example still work?

Because the method's declared in another class header/cpp file.
The thing is,I access this method such as Vector[i].getFloatValues() in main.

The getFloatValues method's declared in another class,so I'm not sure if this function'll work if I'm to use the above code only and try t out.

This method's in another class file.
If class header is included in main file you can use member function just fine.

I am using vectors .begin() .end() members even though it is declared in completely separate file.
I didn't understand this part though.

public:
foo(float f_) : f(f_) {}
float getFloatValue() const {return f;}
};

struct foo_comparer
{
bool operator()(const foo& lhs, const foo& rhs)
{
return lhs.getFloatValue() > rhs.getFloatValue();
}
};

If i already have a method that gets and returns the values, then i'll just have to add the struct in my class file, and do the following below in main to sort?

//Descending using function object:
std::sort(var.begin(), var.end(), foo_comparer());
for(auto s: var)
std::cout << s.getFloatValue() << ' ';
std::cout << '\n';
If i already have a method that gets and returns the values, then i'll just have to add the struct in my class file, and do the following below in main to sort?
Basicly yes. I do not have access to your class, so I had to create a quick mock-up of class with getFloatValue member.

You can do sorting in four ways:
1) Using lambda (Shown in previous posts)
2) Using function object (Shown in previous post)
3) Using function:
1
2
3
4
5
6
bool compare_less(const foo& lhs, const foo& rhs)
{
    return lhs.getFloatValue() < rhs.getFloatValue();
}
//...
std::sort(var.begin(), var.end(), compare_less); //Note the absence of () 

4) Defining operator< for your class:
1
2
3
4
5
6
bool operator<(const foo& lhs, const foo& rhs)
{
    return lhs.getFloatValue() < rhs.getFloatValue();
}
//
std::sort(var.begin(), var.end());//No additional parameters 
Registered users can post here. Sign in or register to post.