- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试编写一个简单的模板函数来打印某个容器的每个元素,而不使用 for 循环。到目前为止,我已经
#include <iostream>
#include <vector>
#include <algorithm>
template <typename T> void print_with_space(T x){
std::cout << x << ' ';
}
template <typename T> void print_all(T beg, T end){
std::for_each(beg, end, print_with_space<int>);
std::cout << '\n';
}
int main(){
int a[] = {1, 2, 3};
std::vector<int> v(a, a+3);
print_all(v.begin(), v.end());
return 0;
}
代码编译并运行,但只是因为我输入了print_with_space<int>
在 print_all
的执行中.我只想拥有 print_with_space
那里的原因很明显,但是代码无法编译。我该怎么做?
最佳答案
您可以使用:
std::for_each(beg, end, [](const typename T::value_type& value) {
print_with_space(value);
});
T
类型为 std::vector<>::iterator
,这是一个 RandomAccessIterator
.每个RandomAcessIterator
有一个基础类型,由 value_type
公开.
所以,如果你通过 std::vector<int>::iterator
, std::vector<int>::iterator::value_type
将是 int
.
现在您有了类型,您可以创建一个 lambda,它会在每次迭代时执行。
在 C++14 中,您甚至可以:
//'auto' automatically deduces the type for you
std::for_each(beg, end, [](const auto& value) {
print_with_space(value);
});
关于c++ - 将模板函数传递给 std::for_each,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38935694/
这行得通,for_each 传递 vector std::vector v(10, 1); std::vector > vv(10, v); auto vvit = vv.begin(); std::
主要思想是在GitHub存储库中为一个环境(生产和开发)创建多个env_var。。使用这些变量,我可以遍历每个变量。Key键是环境名称。但是,我需要认识到如何迭代嵌套的值。我需要使用什么来代替loca
我正在尝试使用 Terraform 0.12+ 及其新的 for_each 在 Azure 中构建多个 vnet,但遇到了一些麻烦。我希望新功能能够让我创建一个接收复杂变量的通用网络模块,但我可能已经
在升级到更新的编译器并解决编译器错误时,我意识到 boost::fusion::for_each 要求传入的函数对象具有运算符 const。 示例来自 Boost : struct increment
我正在阅读 C++ Concurrency in Action 安东尼·威廉姆斯。在关于设计并发代码的章节中有并行版本的std::for_each。算法。这是本书中略微修改的代码: join_thre
我试图理解为什么在单线程上运行的 std::for_each 比 __gnu_parallel::for_each 快 ~3 倍在下面的示例中: Time =0.478101 milliseconds
我想在模块 block 中使用 count,但由于不支持它,我尝试使用 for 循环编写 for_each 但它给出了 “for_each”参数值不合适 错误。 我无法将计数传递给内部模块,因为它会弄
运行时terraform plan或 terraform apply提供给 for_each 的列表发生错误说 Error: Invalid for_each argument on main.t
我的问题是:如何将类成员函数传递给 for_each 我试图开始工作的代码:(在类外定义函数时有效) 失败的部分被注释掉了——使用for_each函数作为类成员函数的部分 关于如何让它工作有什么建议吗
我不太使用 STL,但我想开始学习它,所以我使用 STL 的 for_each 函数编写了一个非常简单的程序。这是整个程序(减去头文件): class Object { public: int
我现在正在学习 OpenCL,并且正在尝试编写一个“hello world”示例;我创建平台、设备和环境。 现在我想从设备获取设备信息,以便我创建信息并将其保存在 vector 中,然后打印它们。为此
我想弄清楚 for_each() 是如何使用的。首先,我改造了这个循环 for(int i = 0; i < myvector.size(); ++i){ myvector[i].a.b. =
我正在尝试使用 for_each 遍历字符串的每个字母, std::unordered_map dico; std::for_each(str.begin(),str.end(),[&](std::s
我想处理 2 个以某种方式相关的容器,并且我想处理它们之间的元素。 简单的例子:一个容器包含某种总和,另一个等长的容器包含必须从该总和中减去的数字。 std::vector s = {20,56,7,
我遇到了以下代码的问题: #include #include #include #include #include using namespace std; struct Person {
我试图让 myFunction 给我数组中值的总和,但我知道我不能使用返回值,当我用代码运行我的程序时,我得到的只是一个打印输出的值,没有总和,这是为什么? void myFunction (int
我正在尝试使用 STL 算法 (C++11) 重写我的一些代码,但我陷入了“无原始循环”规则。 我的一个函数中有这段代码: for (size_t i = 0; i size(); i++) {
有类和事件结构: struct Foo { Foo(): name("nx"), val(9) {} string name; int val; }; class Base {
我尝试将 for_each 与 boost::trim 一起使用。首先我用错了代码 std::for_each(v.begin(),v.end(),&boost::trim)); // error
我正在尝试函数式编程的一些功能,并尝试为 vector 中的每个对象调用一个成员函数。到目前为止,这是我的代码。 emplace.cc #include #include #include #i
我是一名优秀的程序员,十分优秀!