为什么 std::generate 将与 lambda 生成器一起使用,而 std::fill 则不能
Why std::generate will work with lambda generator and std::fill will not?
我偶然发现了这个网站:http://www2.research.att.com/~bs/C++0xFAQ.html#lambda 他们解释lambda函数的地方。我尝试使用提供的示例,即:
vector<int> indices( notImportantNumber );
int count = 0;
fill(indices.begin(), indices.end(), [&](){ return ++count; });
和类似
generate(indices.begin(), indices.end(), [&](){ return ++count; });
虽然,当我尝试使用带有填充的示例时,我不断收到此错误:
错误1 错误 C2440:"=":无法从"常量"转换 'anonymous-namespace'::' to 'long' c:\program files\Microsoft Visual Studio 10.0\VC\include\xutility 2692
有人知道为什么会这样吗?在 std::fill()
的声明中,没有函子作为最后一个参数。
这是该网站上的错误。 std::fill
需要一个值来填充,而不是一个可调用的值。
因为std::fill
不希望函子作为最后一个参数 - 请参阅参考
将给定值分配给范围 [第一个、最后一个] 中的元素。
它只需要一个元素,该元素将分配给指定范围内的每个项目。
使用 std::remove_if 和更多 STL 函数的示例:
#include <algorithm>
#include <iostream>
#include <vector>
#include <set>
int main()
{
// using STL you can write code
// that is more expressive and has less (visible) for loops
// using a set is more maintainable, but just checking a few values
// with ==5 && ==7 is faster.
std::set<int> numbers_to_remove{ 5,7 };
// initialize a list of odd numbers
std::vector<int> values(5);
// last argument to generate is a lambda function
// https://en.cppreference.com/w/cpp/language/lambda
int odd_number{ 1 };
std::generate(values.begin(), values.end(), [&] { return odd_number += 2; });
// remove the items from the vector
// remove_if will only move the items to delete to the end.
// so one more call to erase is needed to cleanup the vector.
auto erase_it = std::remove_if(values.begin(),values.end(), [&](const int value)
{
return numbers_to_remove.find(value) != numbers_to_remove.end();
});
// or if you don't want to use a set
// auto erase_it = std::remove_if(values.begin(), values.end(), [](const int value) { return (value==5) || (value==7)}); // using explicit compares.
values.erase(erase_it,values.end());
// loop over all the values using a range based for loop.
// this is safer then using indices, you can't go out of range.
// const because you don't want to change the values in the loop
// https://en.cppreference.com/w/cpp/language/range-for
for (const int value : values)
{
std::cout << value << " ";
}
return 0;
}
相关文章:
- std::unique_ptr 在 GCC 中工作,但不能在 Visual Studio 中编译
- 为什么我不能在 constexpr lambda 函数中使用 std::tuple
- 为什么我不能将引用作为 std::async 的函数参数传递
- 为什么 GCC 不能假设 std::vector::size 在这个循环中不会改变?
- 为什么初始化 std::vector 时不能使用模板整数?
- "std::shared_from_this"不能由其派生类继承吗?
- 不能将C++的"std::filesystem"库与介子构建一起使用
- std::variant<>::get() 不能使用 Apple LLVM 10.0 编译
- std::chrono::d uration 可以按秒初始化,但不能按毫秒初始化?
- 为什么使用 std::vector 的代码不能编译,但使用 std::unique_ptr 如果没有 noexcept
- 为什么不能通过在错误输入后设置 std::cin.clear() 来使用 std::cin?
- 为什么"std::is_function_v"不能按预期工作?
- 为什么不能用两层列表初始值设定项初始化 2D std::array?
- 矢量迭代器不能与 std::shared_ptr<> 取消引用
- std::remove() 按预期处理文字,但不能与取消引用的迭代器一起工作
- 可变参数函数模板不能很好地使用 std::function 作为参数
- Boost.Asio:不能使用 std::bind() 来指定回调
- 为什么 std::set 中的项目不能被"弹出"?
- 我不能将 'fill' std::vector 构造函数与 unique_ptrs 一起使用
- 为什么std::{container}::template不能推导其参数类型