Я работаю с библиотекой диапазонов и конвейерами для чтения файлов CSV. Мой код работает нормально, за исключением случаев, когда я добавляю ranges::to в конце конвейера. При добавлении «to» последняя строка данных повторяется и искажается.
Правильный вывод:
"Values to Analyze, Current", 1, 7, 2025, 5, 218, 11, 23, 119
"Values to Analyse, Current", 1, 8, 2025, 21, 2055, 1, 7, 131
"Values to Analyse, Current", 1, 9, 2025, 29, 210, 3, 4, 105
"Values to Analyse, Current", 1, 10, 2025, 5, 214, 4, 7, 124
Искаженный вывод:
"Values to Analyse, Current", 1, 10 ,2025 ,5 ,214 ,4, 7,1 24
"Values to Analyse, Current", 1, 10 ,2025 ,5, 214,4 ,7 ,1 24
"Values to Analyse, Current", 1, 10 ,2025 ,5, 214, 4, 7, 124
"Values to Analyse, Current", 1, 10, 2025, 5, 214, 4, 7, 124
Вот код. Оно длинное, но я не смог придумать более короткий пример. Ссылка на него также есть в Compiler Explorer,
#include
#include
#include
#include
using namespace std::literals;
namespace rng = std::ranges;
namespace vws = std::views;
using vws::chunk_by, vws::transform;
#include
using fmt::println, fmt::print;
inline auto to_vec{rng::to()};
//----------------------------------------------------------------------------------------------
struct Line { // @formatter:off
std::string mLine;
friend void operator>>(std::istream& is, Line& l) { std::getline(is, l.mLine); }
}; // @formatter:on
//--------------------------------------------------------------------------------------------------
void print_data(auto&& data) {
for (auto const& line : data) {
for (auto const& cnk : line) {
for (auto const& elem : cnk) {
print("{}", elem);
}
print("\t");
}
println("");
}
}
//--------------------------------------------------------------------------------------------------
auto parse_csv = transform([](auto&& line) { //
static bool is_quoting{false};
return line.mLine //
| chunk_by([](char const lhs, char const) {
if (lhs == '"') {
is_quoting = !is_quoting;
}
return (lhs != ',' or is_quoting);
}) //
| to_vec;
});
//--------------------------------------------------------------------------------------------------
constexpr std::string_view test_data{ // @formatter:off
R"("Values to Analyze, Current",1,7,2025,5,218,11,23,119
"Values to Analyse, Current",1,8,2025,21,2055,1,7,131
"Values to Analyse, Current",1,9,2025,29,210,3,4,105
"Values to Analyse, Current",1,10,2025,5,214,4,7,124)"
};// @formatter:on
//--------------------------------------------------------------------------------------------------
auto main() -> int {
std::ispanstream csv_chars(test_data);
auto data{vws::istream(csv_chars)};
auto csv_data{data | parse_csv};
#if 0 // works okay
print_data(csv_data);
#elif 1 // why does the vector garble output?
print_data(csv_data | to_vec);
#else // transform deleted - why does it work above?
println("{}", fmt::join(csv_data, " "));
#endif
return 0;
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... ostdvector
Почему мои данные искажаются с помощью ranges::to? ⇐ C++
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение