删除矢量元素使用矢量<bool>中的条件
Remove vector element use the condition in vector<bool>
我有两个大小相同的向量a和b。
vector<int> a{ 4, 3, 1, 3, 1};
vector<bool> b{false,false,true,false,true};
如果b
中的相同元素(相同索引)为真,我想删除a
中的元素。
应用函数后:a=4,3,3
注意:我想使用std
算法或函数,而不是简单的for循环。
std::vector<int> v {1,2,3,4,5,6};
std::vector<bool> b {true, false, true, false, true, false};
v.erase(std::remove_if(v.begin(), v.end(),
[&b, &v](int const &i) { return b.at(&i - v.data()); }), v.end());
现场演示
void filter(std::vector<int>& v, const std::vector<bool>& b)
{
assert(v.size() == b.size());
auto it = b.begin();
v.erase(std::remove_if(v.begin(), v.end(), [&](int) { return *it++; }), v.end());
}
演示
我试图通过不使用lambdas,只使用std
函数来达到极限。我有两个解决方案,但它们都需要外部内存:
第一个解决方案
#include <algorithm>
#include <iostream>
#include <iterator>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
int main(int argc, char* argv[]) {
vector<int> a{4, 3, 1, 3, 1};
vector<bool> b{true, false, true, false, true};
vector<int> c;
vector<pair<int,bool>> tmp;
// Join
transform(begin(a), end(a),
begin(b),
back_inserter(tmp),
make_pair<int const&,bool const&>);
// Filter
auto last = partition(begin(tmp), end(tmp),
(bool const&(*)(std::pair<int,bool> const&))
std::get<1,int,bool>);
// Copy back
transform(begin(tmp), last,
back_inserter(c),
(int const&(*)(pair<int,bool> const&))
get<0,int,bool>);
// Print (you could do a.swap(c) if you just want to modify 'a'
copy(begin(c), end(c), ostream_iterator<int>(cout, ", "));
cout << endl;
}
第二个解决方案
它使用valarray代替:
// 2nd solution using valarray
valarray<int> va(&a[0], a.size());
valarray<bool> vb(b.size());
copy(begin(b), end(b), begin(vb));
valarray<int> vc( va[vb] );
copy(begin(vc), end(vc), ostream_iterator<int>(cout, ", "));
cout << endl;
没有STL算法,但有std::remove_if
:的修改版本
template<class ForwardIt1, class ForwardIt2, class UnaryPredicate>
ForwardIt1 remove_if_tagged(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2,
UnaryPredicate p)
{
ForwardIt1 result = first1;
for (; first1 != last1; ++first1, ++first2) {
if (!p(*first1, *first2)) {
*result++ = *first1;
}
}
return result;
}
std::vector<int> a{ 4, 3, 1, 3, 1};
std::vector<bool> b{false,false,true,false,true};
a.erase(
remove_if_tagged(
a.begin(), a.end(),
b.begin(),
[](int, bool tag) { return tag; }
),
a.end()
);
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 在没有太多条件句的情况下,我如何避免被零除
- 基于多个条件处理地图中的所有元素
- 条件constexpr函数
- 无论条件是否为true,if总是在c++中执行
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 基于模板值的条件变量
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- EASTL矢量<向量<int>>连续的
- 将按位if条件转换为普通if条件
- 条件断点在不应该触发时触发
- 为什么简单的算术减法在"if"条件下不起作用?
- 如何在for循环中包含两个索引值的测试条件
- 如果条件为TRUE(最佳方式?),则在do while循环中后置增量
- 我提出什么条件才能再加5%的折扣
- 循环中的条件:为什么每次都调用strlen(),而vector.size()只调用一次
- 即使没有满足他们的条件,我也无法通过一些 do-while 循环
- 如何编写一个使用n倍三元条件语句的C++布尔函数
- 没有超时的C++条件变量
- 正在LLVM中检测整数比较条件