C 范围V3:试图将链条链在一起转换
C++ range-v3: trying to chain together transforms
我是范围库的新手,所以我对此代码没有编译,我不知道为什么:
#include <iostream>
#include <algorithm>
#include <fstream>
#include <iterator>
#include <vector>
#include <range/v3/all.hpp>
#include <range/v3/view/all.hpp>
using namespace ranges::v3;
std::ifstream open_file(const std::string &filename) {
return std::ifstream{filename};
}
int count_lines(std::ifstream &in) {
return std::count(std::istreambuf_iterator<char>{in},
std::istreambuf_iterator<char>{}, 'n');
}
std::vector<int>
count_lines_in_files(const std::vector<std::string> &filenames) {
auto a1 = filenames | view::transform(open_file) | view::transform(count_lines);
return a1;
}
int main() {
const std::vector<std::string> files{"listing1_1.cpp",
"listing1_2.cpp",
"listing1_4.cpp",
"listing1_5.cpp"};
const auto result = count_lines_in_files(files);
std::cout << ranges::view::all(result) << 'n';
}
看来投诉是关于a1
的,编译器告诉我"错误:变量的类型不完整'void'。
有人可以看到我在做什么错,或者告诉我如何在可能的情况下正确连接它们?
预先感谢!
如Porsche9ii所指出的," std::ifstream
没有复制构造函数"。您可以在此处找到有关此主题的更多信息:
为什么iostreams无法复制?
C 11引入了std::basic_ifstream
的移动构造函数(6(,因此您可以写
auto open_file(const std::string &filename) {
return std::ifstream{filename};
}
auto count_lines(std::ifstream &&in) {
return std::count(std::istreambuf_iterator<char>{in},
std::istreambuf_iterator<char>{}, 'n');
}
可在此处测试。
std::ifstream
没有复制构造函数 - 函数返回 std::ifstream
不是一个好主意。一种可能的解决方案:打开和计数应在一个功能中进行。
相关文章:
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 为什么在使用转换构造函数赋值后调用C++类的析构函数?
- 将图形属性与 std::unique_ptr 捆绑在一起
- 你能在 c++ 中将不同的数字类型加在一起吗?
- 如何将两个字符串加在一起,就好像它们是变量一样?
- 虚拟成员函数的定义是否强制在同一转换单元中动态初始化静态数据成员?
- 当返回语句时,逗号运算符、大括号初始化列表和 std::unique_ptr 组合在一起
- 为什么push_back和emplace_back结合在一起时,会有不同的行为
- 在从 C++ 转换为 C# 的代码中,我应该使用什么而不是 memcpy?
- 在浮点精度成为一个问题之前,可以将多少个浮点值加在一起
- 为什么在template函数广播中把两个extensor表达式加在一起不正确
- 为什么在类型转换的 nullptr 上不发生隐式转换
- 实现具有浮点键的类似哈希表的数据结构,其中公差内的值被合并在一起
- 显式构造函数仍在执行转换
- 如何在Qt TableView中将列的宽度调整为数据并将最后一部分拉伸在一起
- 编译器如何将链表中的地址字符串在一起?
- 为什么在使用转换构造函数编译代码时需要 const 复制构造函数?
- 如何将两个 jlong 数据类型转换为 jstring,然后将两个字符串连接在一起以便从 JNI 将字符串返回给 jav
- C 范围V3:试图将链条链在一起转换
- 布尔值转换为字符串,并将boolalpha与一个方法组合在一起,而不是在打印时单独键入它