<myclass> 使用 STL 中的排序函数在一行中对 std::vector 进行排序
Sort std::vector<myclass> in one line using sort function from STL
问题是关于使用函数sort
从STL的算法类排序std::vector<myclass>
。
标准方式为:sort(v.begin(), v.end(), &myfunct)
其中myfunct
为:
bool myfunct( myclass first, myclass second ) {
if (first.value < second.value)
return true;
else return false;
}
上面的方法需要多行。我很好奇如何在一行中完成它。是否有可能定义函数,比较我的类对象内排序函数?可能是用了这个(a < b) ? a : b
。我记得c#中有类似的东西,但我忘了它是怎么叫的。
首先,您可以只返回first.value < second.value
,但这并不能消除该函数。在c++ 2011中,你可以使用lambda函数:
std::sort(begin, end, [](myclass const& f, myclass const& s){ return f.value < s.value; });
如果没有c++ 2011,我认为你需要一个函数对象,因为没有任何东西可以将你的类投影到你实际想要比较的值。
顺便说一句,除了最简单的对象之外,你肯定想把所有的对象都传递给比较函数。
您可以使用boost::lambda
和boost::lambda::bind
(带有boost lambda占位符)
std::sort(vec.begin(), vec.end(),
boost::lambda::bind(&A::a, boost::lambda::_1)
<
boost::lambda::bind(&A::a, boost::lambda::_2));
sort
将2个值传递给比较函数,因此您需要比较这2个值。代码的bind部分只是从struct A
(由_1
和_2
引用)中选择变量a
。
示例代码:
#include <iostream>
#include <algorithm>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/array.hpp>
struct A
{
A() : a(0), b(0) {}
int a;
int b;
};
std::ostream & operator<<(std::ostream & os, A & a)
{ return os << a.a << ":" << a.b; }
int main()
{
boost::array<A,5> vec;
std::fill(vec.begin(),vec.end(),A());
vec[0].a = 1;
vec[1].a = 3;
vec[2].a = 4;
vec[3].a = 0;
vec[4].a = 2;
std::for_each(vec.begin(),vec.end(), std::cout << boost::lambda::_1 << ' ');
std::cout << std::endl;
std::sort(vec.begin(), vec.end(),
boost::lambda::bind(&A::a, boost::lambda::_1)
<
boost::lambda::bind(&A::a, boost::lambda::_2));
std::for_each(vec.begin(),vec.end(), std::cout << boost::lambda::_1 << ' ');
std::cout << std::endl;
}
输出:1:0 3:0 4:0 0:0 2:0
0:0 1:0 2:0 3:0 4:0
为什么不将vector复制到set中:
std::copy(v.begin(),v.end(),std::inserter(s,s.end()));
现在集合中的元素按升序排序,现在使用set
对sort()的一行调用:sort(my_vector_of_class_object.begin(),my_vector_of_class_object.end(),compare);
"类对象的排序向量"的工作演示代码如下:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
class my_Class
{
public:
my_Class(int r,int n, int s):rollno(r),name(n),status(s) { }
int getRollno() const { return rollno;}
int getName() const { return name;}
int getStatus() const { return status;}
private:
int rollno;
int name;
int status;
};
bool compare(const my_Class& x, const my_Class& y) {
return x.getRollno() < y.getRollno();
}
int main()
{
vector<my_Class> my_vector_of_class_object;
vector<my_Class>::const_iterator iter;
my_Class s1(10,20,30);
my_Class s2(40,50,60);
my_Class s3(25,85,9);
my_Class s4(1,50,2);
my_Class s5(90,70,90);
my_Class s6(85,85,3);
my_Class s7(20,6,89);
my_Class s8(70,54,22);
my_Class s9(65,22,77);
my_vector_of_class_object.push_back(s1);
my_vector_of_class_object.push_back(s2);
my_vector_of_class_object.push_back(s3);
my_vector_of_class_object.push_back(s4);
my_vector_of_class_object.push_back(s5);
my_vector_of_class_object.push_back(s6);
my_vector_of_class_object.push_back(s7);
my_vector_of_class_object.push_back(s8);
my_vector_of_class_object.push_back(s9);
cout <<"Before vector sort n";
for(iter=my_vector_of_class_object.begin(); iter!=my_vector_of_class_object.end();++iter)
std::cout << (*iter).getRollno() << 't' << (*iter).getName() << 't' << (*iter).getStatus() << 'n';
cout <<" nn";
sort(my_vector_of_class_object.begin(),my_vector_of_class_object.end(),compare);
cout <<"After vector sort n";
for(iter=my_vector_of_class_object.begin(); iter!=my_vector_of_class_object.end();++iter)
std::cout << (*iter).getRollno() << 't' << (*iter).getName() << 't' << (*iter).getStatus() << 'n';
cout <<" nn";
return 0;
}
相关文章:
- 二叉排序树无法编译
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++选择排序算法中的逻辑错误
- 从C++dll访问C#中的一行主要参数
- 使用C++程序合并排序没有得到正确的输出
- 计算排序向量的向量中唯一值的计数
- 排序算法c++
- 使用2个键的cpp-stl::优先级队列排序不正确
- 将结构向量排序为子组
- 在c++中尝试对对象数组进行排序时,出现std:bad_alloc错误
- 如何对点云数据进行排序
- 在C++中,我如何接受不同于同一行的用户输入
- 按一行对2D数组进行排序
- 根据某一行对整个 2D 向量进行排序
- 基于一行对 3D 向量进行排序(C++,std::sort)
- 基于C++中每一行的总和对2D阵列进行排序
- <myclass> 使用 STL 中的排序函数在一行中对 std::vector 进行排序