STL accumulate() function
STL accumulate() function
如何计算满足lower_bound(42),upper_bound从这个代码??
accumulate(values.lower_bound(42), values.upper_bound(137), 0);
accumulate
不计数,它累加。当然,你可以积累1而不是元素,但这是向后的。直接的答案是std::distance
,它给出了两个迭代器之间(即指定的下限和上限之间)的元素数量:
auto result = std::distance(values.lower_bound(42), values.upper_bound(137));
我认为您正在寻找的解决方案是algorithm
标头中的std::count_if
。如果你有一个容器cont
,你可以这样使用它:
bool is_in_range(int x){
return 42 <= x && x >= 137;
}
std::count_if(cont.begin(), cont.end(), is_in_range);
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- 库函数需要一个 std::function<void(void)>,如何传入类函数?
- Confusion: decltype vs std::function
- 为什么 std::function 可以作为 std::not2 的参数?
- 'max'匹配'std::function<const int &(const int &, const int &)>'无过载
- 传递给std::function template的template参数究竟代表什么
- 将带有unique_ptr的可变 lambda 传递给 const&std::function
- 绑定派生类方法C++从实例范围之外的分隔 std::function 变量调用
- "no matching function for call to 'Vector::Vector'"错误
- 如何在向量中删除 std::function<void()>?
- 不断"Attempting to reference a deleted function"
- 将函数包装器转换为 std::function
- 类型擦除的std::function与虚拟函数调用的开销
- C++ std::function 对于类 exept 的所有实例都是空的(只有 Visual2019 编译器问题)
- 如果模板没有可变参数,则 Lambda 被推导出为 std::function
- 我在 ifstream input_file(文件名)行中收到错误"no matching function to call";
- 模板规范获取'Ambiguous call to overloaded function'
- 将 lambda 表达式传递给 std::function in C++
- STL accumulate() function