在opencv中用c++如何实现 把一组ector
时间: 2024-03-02 22:49:00 浏览: 95
将一组vector按照某个关键字排序可以使用sort()函数。假设我们有一个存储Person对象的vector,每个Person对象包含一个名字(name)和年龄(age)属性,我们可以按照年龄(age)属性来排序:
```
#include <opencv2/opencv.hpp>
#include <algorithm>
using namespace cv;
using namespace std;
class Person {
public:
string name;
int age;
Person(string n, int a) {
name = n;
age = a;
}
};
bool compareByAge(const Person &a, const Person &b) {
return a.age < b.age;
}
int main() {
vector<Person> people;
people.push_back(Person("Alice", 25));
people.push_back(Person("Bob", 30));
people.push_back(Person("Charlie", 20));
sort(people.begin(), people.end(), compareByAge);
for (int i = 0; i < people.size(); i++) {
cout << people[i].name << ", " << people[i].age << endl;
}
return 0;
}
```
输出结果为:
```
Charlie, 20
Alice, 25
Bob, 30
```
这样就可以按照年龄属性将一组Person对象排序了。
阅读全文