我已经创建了一个适合我特定需求的代码-以逗号分割字符串(从文件中读取)剥离任何空格。此外,我想将这些子字符串转换为double
并将它们存储在std::向量
中。我在c 20中使用范围
库,并像下面这样实现:
#include <iostream>
#include <fstream>
#include <ranges>
#include <algorithm>
#include <vector>
auto join_character_in_each_subranges = [](auto &&rng) { return std::string_view(&*rng.begin(), std::ranges::distance(rng)); };
auto trimming = std::ranges::views::filter([](auto character){ return !std::isspace(character);});
typedef std::vector<double> LineList;
typedef std::vector<LineList> List;
int main () {
std::ifstream myfile;
std::string myline;
List list;
myfile.open("data.txt");
while (std::getline(myfile, myline))
{
LineList line_list;
for (auto words : myline
| std::ranges::views::split(',')
| std::ranges::views::transform(join_character_in_each_subranges))
{
auto words_trimming = words | trimming;
std::string clean_numbers;
std::ranges::for_each(words_trimming, [&](auto character){ clean_numbers += character;});
line_list.push_back(atof(clean_numbers.c_str()));
}
list.push_back(line_list);
}
}
首先,迭代myline
句子并将视图拆分为分隔符上的子范围
myline | std::ranges::views::split(',')
附加子范围内的每个字符,并查看到std::字符串
与转换
函数
STd::变换将给定的函数应用于一个范围并将结果存储在另一个范围中。
std::ranges::views::transform(join_character_in_each_subranges)
此外,从视图范围中删除任何前缀和后缀
auto words_trimming = words | trimming;
并将视图范围转换为std::字符串
std::ranges::for_each(words_trimming, [&](auto character){ clean_number += character;});
最后,将每个clean_number
转换为double
和push_back
到列表中。
line_list.push_back(atof(clean_words.c_str()));
但是当我将此代码更改为
for (auto words : myline
| std::ranges::views::split(',')
| std::ranges::views::transform(join_character_in_each_subranges)
| trimming)
我有很多错误。
read.cpp:26:17: error: no match for 'operator|' (operand types are 'std::ranges::transform_view<std::ranges::split_view<std::ranges::ref_view<std::__cxx11::basic_string<char> >, std::ranges::single_view<char> >, <lambda(auto:16&&)> >' and 'std::ranges::views::__adaptor::_RangeAdaptorClosure<std::ranges::views::__adaptor::_RangeAdaptor<_Callable>::operator()<{<lambda(auto:17&&)>}>::<lambda(_Range&&)> >')
23 | for (auto words : myline
| ~~~~~~
24 | | std::ranges::views::split(',')
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 | | std::ranges::views::transform(join_character_in_each_subranges)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| |
| std::ranges::transform_view<std::ranges::split_view<std::ranges::ref_view<std::__cxx11::basic_string<char> >, std::ranges::single_view<char> >, <lambda(auto:16&&)> >
26 | | trimming)
| ^ ~~~~~~~~
| |
| std::ranges::views::__adaptor::_RangeAdaptorClosure<std::ranges::views::__adaptor::_RangeAdaptor<_Callable>::operator()<{<lambda(auto:17&&)>}>::<lambda(_Range&&)> >
In file included from read.cpp:3:
/usr/local/Cellar/gcc@10/10.3.0/include/c++/10.3.0/ranges:1183:4: note: candidate: 'template<class _Tp> constexpr auto std::ranges::views::__adaptor::operator|(const std::ranges::views::__adaptor::_RangeAdaptorClosure<_Callable>&, const std::ranges::views::__adaptor::_RangeAdaptorClosure<std::ranges::views::__adaptor::_RangeAdaptor<_Callable>::operator()<{<lambda(auto:17&&)>}>::<lambda(_Range&&)> >&)'
1183 | operator|(const _RangeAdaptorClosure<_Tp>& __x,
| ^~~~~~~~
/usr/local/Cellar/gcc@10/10.3.0/include/c++/10.3.0/ranges:1183:4: note: template argument deduction/substitution failed:
read.cpp:26:19: note: 'std::ranges::transform_view<std::ranges::split_view<std::ranges::ref_view<std::__cxx11::basic_string<char> >, std::ranges::single_view<char> >, <lambda(auto:16&&)> >' is not derived from 'const std::ranges::views::__adaptor::_RangeAdaptorClosure<_Callable>'
26 | | trimming)
| ^~~~~~~~
In file included from read.cpp:3:
/usr/local/Cellar/gcc@10/10.3.0/include/c++/10.3.0/ranges:1178:4: note: candidate: 'constexpr auto std::ranges::views::__adaptor::operator|(_Range&&, const std::ranges::views::__adaptor::_RangeAdaptorClosure<_Callable>&) [with _Range = std::ranges::transform_view<std::ranges::split_view<std::ranges::ref_view<std::__cxx11::basic_string<char> >, std::ranges::single_view<char> >, <lambda(auto:16&&)> >; _Callable = std::ranges::views::__adaptor::_RangeAdaptor<_Callable>::operator()<{<lambda(auto:17&&)>}>::<lambda(_Range&&)>]'
1178 | operator|(_Range&& __r, const _RangeAdaptorClosure& __o)
| ^~~~~~~~
/usr/local/Cellar/gcc@10/10.3.0/include/c++/10.3.0/ranges:1178:4: note: constraints not satisfied
/usr/local/Cellar/gcc@10/10.3.0/include/c++/10.3.0/ranges: In instantiation of 'constexpr auto std::ranges::views::__adaptor::operator|(_Range&&, const std::ranges::views::__adaptor::_RangeAdaptorClosure<_Callable>&) [with _Range = std::ranges::transform_view<std::ranges::split_view<std::ranges::ref_view<std::__cxx11::basic_string<char> >, std::ranges::single_view<char> >, <lambda(auto:16&&)> >; _Callable = std::ranges::views::__adaptor::_RangeAdaptor<_Callable>::operator()<{<lambda(auto:17&&)>}>::<lambda(_Range&&)>]':
read.cpp:26:19: required from here
/usr/local/Cellar/gcc@10/10.3.0/include/c++/10.3.0/ranges:1178:4: required by the constraints of 'template<class _Callable> template<class _Range> requires (viewable_range<_Range>) && requires{(declval<_Callable>)()((declval<_Range>)());} constexpr auto std::ranges::views::__adaptor::operator|(_Range&&, const std::ranges::views::__adaptor::_RangeAdaptorClosure<_Callable>&)'
/usr/local/Cellar/gcc@10/10.3.0/include/c++/10.3.0/ranges:1176:13: in requirements [with _Range = std::ranges::transform_view<std::ranges::split_view<std::ranges::ref_view<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::ranges::single_view<char> >, ._anon_104>; _Callable = std::ranges::views::__adaptor::_RangeAdaptor<std::ranges::views::._anon_83>::operator()::._anon_106]
/usr/local/Cellar/gcc@10/10.3.0/include/c++/10.3.0/ranges:1176:44: note: the required expression 'declval<_Callable>()(declval<_Range>())' is invalid
1176 | requires requires { declval<_Callable>()(declval<_Range>()); }
| ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
cc1plus: note: set '-fconcepts-diagnostics-depth=' to at least 2 for more detail
In file included from /usr/local/Cellar/gcc@10/10.3.0/include/c++/10.3.0/ios:42,
from /usr/local/Cellar/gcc@10/10.3.0/include/c++/10.3.0/ostream:38,
from /usr/local/Cellar/gcc@10/10.3.0/include/c++/10.3.0/iostream:39,
from read.cpp:1:
/usr/local/Cellar/gcc@10/10.3.0/include/c++/10.3.0/bits/ios_base.h:87:3: note: candidate: 'constexpr std::_Ios_Fmtflags std::operator|(std::_Ios_Fmtflags, std::_Ios_Fmtflags)'
87 | operator|(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
| ^~~~~~~~
/usr/local/Cellar/gcc@10/10.3.0/include/c++/10.3.0/bits/ios_base.h:87:27: note: no known conversion for argument 1 from 'std::ranges::transform_view<std::ranges::split_view<std::ranges::ref_view<std::__cxx11::basic_string<char> >, std::ranges::single_view<char> >, <lambda(auto:16&&)> >' to 'std::_Ios_Fmtflags'
87 | operator|(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
| ~~~~~~~~~~~~~~^~~
/usr/local/Cellar/gcc@10/10.3.0/include/c++/10.3.0/bits/ios_base.h:129:3: note: candidate: 'constexpr std::_Ios_Openmode std::operator|(std::_Ios_Openmode, std::_Ios_Openmode)'
129 | operator|(_Ios_Openmode __a, _Ios_Openmode __b)
| ^~~~~~~~
/usr/local/Cellar/gcc@10/10.3.0/include/c++/10.3.0/bits/ios_base.h:129:27: note: no known conversion for argument 1 from 'std::ranges::transform_view<std::ranges::split_view<std::ranges::ref_view<std::__cxx11::basic_string<char> >, std::ranges::single_view<char> >, <lambda(auto:16&&)> >' to 'std::_Ios_Openmode'
129 | operator|(_Ios_Openmode __a, _Ios_Openmode __b)
| ~~~~~~~~~~~~~~^~~
/usr/local/Cellar/gcc@10/10.3.0/include/c++/10.3.0/bits/ios_base.h:169:3: note: candidate: 'constexpr std::_Ios_Iostate std::operator|(std::_Ios_Iostate, std::_Ios_Iostate)'
169 | operator|(_Ios_Iostate __a, _Ios_Iostate __b)
| ^~~~~~~~
/usr/local/Cellar/gcc@10/10.3.0/include/c++/10.3.0/bits/ios_base.h:169:26: note: no known conversion for argument 1 from 'std::ranges::transform_view<std::ranges::split_view<std::ranges::ref_view<std::__cxx11::basic_string<char> >, std::ranges::single_view<char> >, <lambda(auto:16&&)> >' to 'std::_Ios_Iostate'
169 | operator|(_Ios_Iostate __a, _Ios_Iostate __b)
| ~~~~~~~~~~~~~^~~
如何解决这个问题?
谢啦
根据弗兰克的评论,改变修剪如下:
auto trimming = std::ranges::views::transform(std::ranges::views::filter([](char character){return !std::isspace(character); }));