简体   繁体   English

结合 istream_iterator 与 regex_token_iterator

[英]Combine istream_iterator with regex_token_iterator

Is it possible to combine an istream_iterator with an regex_token_iterator similar like this:是否可以像这样将istream_iteratorregex_token_iterator结合起来:

std::copy(
    std::sregex_token_iterator(std::istreambuf_iterator<char>{ifs},
    std::istreambuf_iterator<char>{}, r, 1), std::sregex_token_iterator{},
    std::ostream_iterator<std::string>(std::cout)
);

To give this a little bit of context.给这个一点背景。 Im new to programming and im trying to solve a problem where i want to delete everything in an ifstream, except digits.我是编程新手,我试图解决一个问题,我想删除 ifstream 中的所有内容,数字除外。 Im doing this for practice and learning.我这样做是为了练习和学习。

The input file is looking like this:输入文件看起来像这样:

aoisdnf 2 aopsjmdf 4 anpoidsnian 5 ainsdf 12 paalknshndf 43 aoksjhndfal 4 aslkdfoo 9 hjalkgshgdfk 4

The solution should look like this:解决方案应如下所示:

2 4 5 12 43 4 9 4

My first approach was this:我的第一个方法是这样的:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <ctype.h>

int main()
{
   std::ifstream ifs ("C:/Users/../whitespace_seperated_integers.txt", std::ifstream::in);
   std::string tmp;
   std::vector<int> vector;

   for (auto it = std::istreambuf_iterator<char>{ifs}; it != std::istreambuf_iterator<char>{}; ++it) {
      if (*it >= '0' && *it <= '9') tmp.append(1, *it);
      else if (!tmp.empty()){
         vector.push_back(std::stoi(tmp));
         tmp.clear();
      }
   }
   if (!tmp.empty()) vector.push_back(std::stoi(tmp));

   for(const auto i : vector){
      std::cout << i << " ";
   }

Which worked fine, but then i had the idea to solve this problem with regex, which lead to this solution:效果很好,但后来我有了用正则表达式解决这个问题的想法,这导致了这个解决方案:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <ctype.h>
#include <regex>

int main()
{
   std::ifstream ifs ("C:/Users/../whitespace_seperated_integers.txt", std::ifstream::in);
   std::string puf;
   std::vector<std::string> vector;
   std::string line;
   char wts = ' ';
   while(getline(ifs ,line, wts)){
      puf += line;
   }
   std::regex r(R"([^\d]*(\d+))");
   std::copy(std::sregex_token_iterator(puf.begin(), puf.end(), r, 1), std::sregex_token_iterator(), std::back_inserter(vector));

   std::vector<int> vec;
   std::smatch sm;
   while(std::regex_search(puf, sm, r))
   {
      vec.push_back(std::stoi(sm[1]));
      /* std::cout << sm[1] << '\n';*/
      puf = sm.suffix();
   }
   for(const auto i : vec){
      std::cout << i << " ";
   }
}

But im not really happy with this code, so i was trying to figure out how to improve it.但是我对这段代码不是很满意,所以我想弄清楚如何改进它。 I tried to combine the istream_iterator with the regex_token_iterator, but im not able to figure out how it works.我试图将 istream_iterator 与 regex_token_iterator 结合起来,但我无法弄清楚它是如何工作的。

If you really want to use the std::sregex_token_iterator , then you may want to chose a different approach.如果您真的想使用std::sregex_token_iterator ,那么您可能需要选择不同的方法。

For your given string, you want to extract the number.对于给定的字符串,您想要提取数字。 We can change the vieving angle and used a different algorithm.我们可以改变视角并使用不同的算法。 If we do see everything that is NOT a digit as a delimiter, we can use the std::sregex_token_iterator with the index parameter -1 for splitting up the string.如果我们确实看到所有不是数字的东西都作为分隔符,我们可以使用带有索引参数 -1 的std::sregex_token_iterator来拆分字符串。

So, the secret is the new delimiter.所以,秘密是新的定界符。 Then the result is a one-liner那么结果就是一行

#include <iostream>
#include <regex>
#include <vector>
#include <iterator>
#include <string>

int main()
{
    // The test string
    std::string test{"aoisdnf 2 aopsjmdf 4 anpoidsnian 5 ainsdf 12 paalknshndf 43 aoksjhndfal 4 aslkdfoo 9 hjalkgshgdfk 4"};

    // regex for anything but a digit
    const std::regex re {R"([\D]+)"};

    // Get all digits from the test string
    std::vector<std::string> token(std::sregex_token_iterator(test.begin(),test.end(),re, -1), {});

    // Output result
    std::copy(token.begin(), token.end(), std::ostream_iterator<std::string>(std::cout," "));

    return 0;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM