简体   繁体   English

有没有一种干净的方法来使用范围拆分逗号和空格分隔的单词?

[英]Is there a clean way to split comma-and-space--separated words using ranges?

I'm actually adapting this one from a deleted question from yesterday .我实际上是根据昨天删除的问题改编这个问题。

Input: a string containing comma separated words with space after the commas.输入:一个字符串,包含逗号分隔的单词,逗号后有空格。

Output: the words. Output:字。

By "clean" I mean something that doesn't rely on storing temporaries into variables which wouldn't have a reason to exist, based on the intent of the code. “干净”是指不依赖于根据代码的意图将临时变量存储到没有理由存在的变量中的东西。

My attempt我的尝试

#include <iostream>
#include <range/v3/view/split.hpp>
#include <range/v3/view/split_when.hpp>
#include <range/v3/view/transform.hpp>
#include <range/v3/view/trim.hpp>
#include <string>
#include <vector>

auto constexpr is = [](char c){
    return [c](char c_){ return c_ == c; };
};
using namespace ranges::views;

int main()
{
  std::string ss{"cccccciao"};
  // correctly prints [i,a,o]
  std::cout << (ss | trim(is('c'))) << std::endl;

  std::string s = "blue, green, red";
  std::cout << split_when(s, is(',')) << std::endl;

  std::vector<std::string> vs;
  vs.push_back(ss);
  vs.push_back(ss);
  // correctly prints [[i,a,o],[i,a,o]]
  std::cout << (vs | transform(trim(is('c')))) << std::endl;

  // doesn't work
  //auto result0 = split_when(s, is(',')) | transform(trim(is(' ')));
  //auto result1 = transform(split_when(s, is(',')), trim(is(' ')));

  // doesn't work either
  auto sss = split_when(s, [](char c){ return c == ','; });
  //auto result2 = transform(sss, trim([](char c){ return c == ' '; }));
  //auto result3 = sss | transform(trim([](char c){ return c == ' '; }));
}

Based on the principle of least astonishment I expected that something like this oneliner should work,基于 最小惊讶的原则,我预计像这样的 oneliner 应该可以工作,

auto result0 = s | split_when(is(',')) | transform(trim(is(' ')));

but it doesn't.但事实并非如此。

split can split on a pattern that is longer than one element. split可以在比一个元素长的模式上进行拆分。 The one caveat is that ", " , considered as a range, is a pattern of three elements (including the terminating null character) - and in any event the adapter decays it to a pointer, which makes it not-a-range.需要注意的是", " ,被视为一个范围,是三个元素的模式(包括终止 null 字符) - 在任何情况下,适配器都会将其衰减为一个指针,这使得它不是一个范围。 So:所以:

auto result = input | rv::split(", "sv);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c++ 中的 stringstream 有助于从字符串中提取逗号分隔的整数,但不能使用向量从空格分隔的整数中提取,为什么? - stringstream in c++ helps to extract comma separated integers from string but not space separated integers using vectors,why? 用逗号和字母分隔的字符串中的拆分值 - Split values in string separated by both comma and a letter C ++读取inFile,以逗号和空格分隔 - C++ Read inFile separated by a comma and a space 使用语义操作解析以逗号分隔的范围和数字列表 - Parsing comma-separated list of ranges and numbers with semantic actions 在 C++ 中解析逗号分隔的整数/整数范围 - Parse comma-separated ints/int-ranges in C++ 从一系列字符串中提取最后两个单词,以空格分隔 - extracting last 2 words from a sequence of strings, space-separated c ++:使用声明中的逗号分隔列表 - c++ : comma separated list in using declaration 如何在C ++中将空格分隔的字符串拆分为多个字符串? - How to split a space separated string into multiple strings in C++? 使用 range-v3 读取带有逗号分隔数据的行 - Using range-v3 to read lines with comma-separated data 从使用sstream读取的单词中删除最后一个空格的最佳方法? - Best way to remove the last blank space from words read using sstream?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM