如何将所有值为 false 的成员排序到向量的末尾?
How to sort all members with a value of false to the end of a vector?
我想按值对布尔向量进行排序。这是列表:
std::vector<bool> bolis {true true false true false false true true};
是否有某种函数可以使这个列表等于:
{true,true,true,true,true,false,false,false}
我看到了 std::sort
函数,但它似乎唯一真正的用途是对整数或浮点数列表进行排序,而不是布尔值列表。
第一种方法:
这可以通过定义 自定义比较函数 并将其传递给 sort
函数来实现。
看看实现如下:
#include <iostream>
#include <vector>
#include <algorithm>
//define the function:
bool comparator(bool lhs, bool rhs) { return rhs < lhs; }
int main()
{
std::vector<bool> bolis {true, true, false, true, false, false, true, true};
// pass it to sort:
sort(bolis.begin(), bolis.end(), &comparator);
for(int i=0;i<bolis.size();i++){
std::cout<<(bolis[i] ? "true " : "false ");
}
std::cout<<std::endl;
}
输出:
true true true true true false false false
第二种方法O(N)时间复杂度:
计算向量中真值和假值的数量并相应地打印它们。
例如,如果有5个真值和2个假值,则打印5次为真,2次为假。
或者您也可以将值推送到另一个向量中或编辑原始向量。
感谢@john 提出的改进答案的所有建议!
对 std::vector<bool>
进行排序是一个非常具体的情况,可以通过非常具体的解决方案来完成:
计算 true
个元素的数量,然后根据此计数重写向量。
示例代码:
#include <vector>
#include <iostream>
int main()
{
std::vector<bool> bolis {true, true, false, true, false, false, true, true};
// count number of true
size_t nT = 0;
for (bool value : bolis) nT += value == true;
// write sorted vector
for (size_t i = 0, n = bolis.size(); i < n; ++i) bolis[i] = i < nT;
// show result
const char *sep = "";
for (bool value : bolis) {
std::cout << sep << std::boolalpha << value;
sep = ", ";
}
}
输出:
true, true, true, true, true, false, false, false
标准库中的 std::partition
函数专为完全按照您的意愿而设计。它接受一个范围和一个谓词,并对范围的元素重新排序,使它们根据这个谓词进行分区:
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
int main()
{
std::vector<bool> bolis {true, true, false, true, false, false, true, true};
//reorder so that true comes first
std::partition(bolis.begin(), bolis.end(), [](bool b){return b;});
std::cout << std::boolalpha;
std::copy(bolis.begin(), bolis.end(), std::ostream_iterator<bool>(std::cout, " " ));
return 0;
}
输出:
true true true true true false false false
这里是 demo。
我想按值对布尔向量进行排序。这是列表:
std::vector<bool> bolis {true true false true false false true true};
是否有某种函数可以使这个列表等于:
{true,true,true,true,true,false,false,false}
我看到了 std::sort
函数,但它似乎唯一真正的用途是对整数或浮点数列表进行排序,而不是布尔值列表。
第一种方法:
这可以通过定义 自定义比较函数 并将其传递给 sort
函数来实现。
看看实现如下:
#include <iostream>
#include <vector>
#include <algorithm>
//define the function:
bool comparator(bool lhs, bool rhs) { return rhs < lhs; }
int main()
{
std::vector<bool> bolis {true, true, false, true, false, false, true, true};
// pass it to sort:
sort(bolis.begin(), bolis.end(), &comparator);
for(int i=0;i<bolis.size();i++){
std::cout<<(bolis[i] ? "true " : "false ");
}
std::cout<<std::endl;
}
输出:
true true true true true false false false
第二种方法O(N)时间复杂度:
计算向量中真值和假值的数量并相应地打印它们。
例如,如果有5个真值和2个假值,则打印5次为真,2次为假。
或者您也可以将值推送到另一个向量中或编辑原始向量。
感谢@john 提出的改进答案的所有建议!
对 std::vector<bool>
进行排序是一个非常具体的情况,可以通过非常具体的解决方案来完成:
计算 true
个元素的数量,然后根据此计数重写向量。
示例代码:
#include <vector>
#include <iostream>
int main()
{
std::vector<bool> bolis {true, true, false, true, false, false, true, true};
// count number of true
size_t nT = 0;
for (bool value : bolis) nT += value == true;
// write sorted vector
for (size_t i = 0, n = bolis.size(); i < n; ++i) bolis[i] = i < nT;
// show result
const char *sep = "";
for (bool value : bolis) {
std::cout << sep << std::boolalpha << value;
sep = ", ";
}
}
输出:
true, true, true, true, true, false, false, false
标准库中的 std::partition
函数专为完全按照您的意愿而设计。它接受一个范围和一个谓词,并对范围的元素重新排序,使它们根据这个谓词进行分区:
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
int main()
{
std::vector<bool> bolis {true, true, false, true, false, false, true, true};
//reorder so that true comes first
std::partition(bolis.begin(), bolis.end(), [](bool b){return b;});
std::cout << std::boolalpha;
std::copy(bolis.begin(), bolis.end(), std::ostream_iterator<bool>(std::cout, " " ));
return 0;
}
输出:
true true true true true false false false
这里是 demo。