按状态排列向量中的元素
Arranging elements in a vector by their status
让我们简化一下class:
class A
{
bool val_;
public:
A() : val_(true) {}
bool isNew() const { return val_; }
void setDirty() { val_ = false; }
};
和此类对象的向量:
vector<A> coll;
coll.push_back(A());
coll.push_back(A());
coll.push_back(A());
coll.push_back(A());
coll[1].setDirty();
coll[3].setDirty();
我需要一些优雅的解决方案来重新排列(排序)向量中的元素,以便未修改的对象将在序列的开头分组。
您可以使用标准库中的分区算法:
bool MyPredicate(A& a) { return a.isNew();}
...
// bound is iterator pointing to the first element for which predicate returns false
vector<A>::iterator bound = partition(coll.begin(), coll.end(), MyPredicate);
或者,正如Christian Rau建议的不单独功能的解决方案:
std::partition(coll.begin(), coll.end(), std::mem_fun_ref(&A::isNew))
sort
:
#include <algorithm>
std::sort(coll.begin(), coll.end(),
[](const A & a, const A & b) -> bool { return a.isNew() < b.isNew(); } );
你必须重写类来声明isNew()
为const
。
对于较旧的编译器,使用函数代替lambda:
bool isNewCompare(const A & a, const A & b) { return a.isNew() < b.isNew(); }
std::sort(coll.begin(), coll.end(), isNewCompare);
编辑: @Vladimir有更好的答案,std::partition()
是更适合这个问题的算法。
std::sort
允许您提供自定义比较函数对象。您定义了一个类,该类覆盖了括号操作符,如果第一个参数应该在右参数之前,则返回true:
class COrderByDirty
{
bool operator(const A& lhs, const A& rhs) const
{
// Says lhs should come before rhs only if
// lhs is marked as dirty, and rhs is not
if (lhs.GetDirty() < rhs.Dirty())
{
return true;
}
}
}
然后简单地实例化它,用它来排序:
std::sort(coll.begin(), coll.end(), COrderByDirty());
如果你能使用c++ 11,你可以避免冗长的类创建而使用lambda,正如Kernek在他的回答中所做的。
您可以将<algorithm>
中的std::sort
与boost::bind
一起使用。它可以看起来像这样:
std::sort(coll.begin(), coll.end(), boost::bind(&A::isDirty, _1));
假设A
有bool A::isDirty() const
函数。
这样做是因为您隐式地使用了以下排序谓词:
bool cmp(const A &a, const A &b) {
return a.isDirty();
}
我们只是不关心两个都是脏的或者两个都不是脏的。
相关文章:
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- C++:如何循环通过向量中的整数元素
- 如何在C++向量中奇数元素前面加上值-1,我在使用insert函数时遇到了问题
- 不允许在向量中添加更多元素
- 基于范围的 for 循环:迭代使用一个元素扩展的向量
- 使用并行参数向量调用元素向量的成员函数
- C++ 查找字符串中存在的元素向量
- 包含 std::threads 的元素向量
- 避免从单一元素向量转换为基元类型
- 对自定义元素向量进行排序时出现意外(至少对我来说)行为
- 如何为对元素(向量和int)配对创建unique_ptr也是unique_ptr
- 元素向量乘法 C++(代码不起作用)
- 时间和空间复杂性在二叉树的每个级别创建元素向量(NON-BST)
- 遍历结构元素向量
- 犰狳C++中的元素向量或矩阵乘法
- 为什么'std::vector<int> b{2};'创建 1 元素向量,而不是 2 元素向量?
- 可移动元素向量的大小调整是否有效?
- 唯一元素向量的c++模板函数
- 获取索引和元素-向量问题
- 定义一个生成元素向量的函数时,正确的方法是什么?