c++ 在标记化过程中删除所有空格

abithluo  于 2023-06-25  发布在  其他
关注(0)|答案(3)|浏览(160)

如何删除以下代码中标记之间的所有空格?

  1. #include <iostream>
  2. #include <sstream>
  3. using namespace std;
  4. int main()
  5. {
  6. string s[3] = {"a 1", "b 2", "c 3"};
  7. for (int i = 0; i < 3; i++) {
  8. stringstream ss(s[i]);
  9. string tok;
  10. while(getline(ss, tok, ' ')) // <-- need to fix
  11. {
  12. cout << tok << "."; // <-- need to fix
  13. }
  14. cout << endl;
  15. }
  16. return 0;
  17. }

预期输出为

  1. a.1
  2. b.2
  3. c.3

但是代码会生成

  1. a.1.
  2. b....2.
  3. c..3.
20jt8wwn

20jt8wwn1#

您可以使用来自流的格式化输入,而不是std::getline。默认情况下,格式化输入跳过空格:

  1. #include <iostream>
  2. #include <sstream>
  3. int main() {
  4. std::string s[3] = {"a 1", "b 2", "c 3"};
  5. for (const auto& str : s) {
  6. std::istringstream ss(str);
  7. if (std::string tok; ss >> tok) {
  8. // at least one token read, print it
  9. std::cout << tok;
  10. // then read the rest and prepend them with '.' instead:
  11. while (ss >> tok) {
  12. std::cout << '.' << tok;
  13. }
  14. }
  15. std::cout << '\n';
  16. }
  17. }

输出:

  1. a.1
  2. b.2
  3. c.3

Demo

展开查看全部
oalqel3c

oalqel3c2#

您可以使用std::regex_replace来实现这一点,除非有特殊的原因需要使用stringstream。

  1. #include <iostream>
  2. #include <iterator>
  3. #include <regex>
  4. #include <string>
  5. int main()
  6. {
  7. std::string s[3] = {"a 1", "b 2", "c 3"};
  8. for (std::string& str : s) {
  9. str = std::regex_replace(str, std::regex{"\\s+"}, ".");
  10. std::cout << str << std::endl;
  11. }
  12. return 0;
  13. }

Output

  1. a.1
  2. b.2
  3. c.3
展开查看全部
um6iljoc

um6iljoc3#

你可以用我之前写的split函数来实现。

  1. #include <vector>
  2. #include <string_view>
  3. #include <iostream>
  4. // it is always useful to build up a library of functions
  5. // regex as said is awfully slow. And splitting (or tokenizing)
  6. // strings is needed often.
  7. // split uses string_views on the original string
  8. // so keep the original string in memory
  9. //
  10. auto split(std::string_view string, std::string_view delimiters)
  11. {
  12. std::vector<std::string_view> substrings;
  13. if ((string.size() == 0ul))
  14. {
  15. return substrings;
  16. }
  17. if (delimiters.size() == 0ul)
  18. {
  19. substrings.emplace_back(string);
  20. return substrings;
  21. }
  22. auto start_pos = string.find_first_not_of(delimiters);
  23. auto end_pos = start_pos;
  24. auto max_length = string.length();
  25. while (start_pos < max_length)
  26. {
  27. end_pos = std::min(max_length, string.find_first_of(delimiters, start_pos));
  28. if (end_pos != start_pos)
  29. {
  30. substrings.emplace_back(&string[start_pos], end_pos - start_pos);
  31. start_pos = string.find_first_not_of(delimiters, end_pos);
  32. }
  33. }
  34. return substrings;
  35. }
  36. // using namespace std; <== do not do this.
  37. int main()
  38. {
  39. std::vector<std::string> strings{ "a 1", "b 2", "c 3" };
  40. for (const auto& string : strings)
  41. {
  42. // this will split to sets of tokens {"a", "1"}, {"b","2"} and {"c", "3"}
  43. auto tokens = split(string," ");
  44. std::cout << "tokens : ";
  45. // output the (cleaned up) tokens
  46. for (const auto& token : tokens)
  47. {
  48. std::cout << token << " ";
  49. }
  50. std::cout << "\n";
  51. }
  52. return 0;
  53. }
展开查看全部

相关问题