Rcpp-sregex_token_iterator对载体的捕获结果

Rcpp - Capture result of sregex_token_iterator to vector

本文关键字:结果 token iterator Rcpp-sregex      更新时间:2023-10-16

我是一名R用户,正在学习c++以利用Rcpp。最近,我在Rcpp中使用string.h编写了一个R的strsplit的替代方案,但它不是基于正则表达式的(afaik)。我一直在读关于Boost的文章,发现了sregex_token_iterator。

下面的网站有一个例子:

std::string input("This is his face");
sregex re = sregex::compile(" "); // find white space
// iterate over all non-white space in the input. Note the -1 below:
sregex_token_iterator begin( input.begin(), input.end(), re, -1 ), end;
// write all the words to std::cout
std::ostream_iterator< std::string > out_iter( std::cout, "n" );
std::copy( begin, end, out_iter );

我的rcpp函数运行得很好:

#include <Rcpp.h>
#include <boost/xpressive/xpressive.hpp>
using namespace Rcpp;
// [[Rcpp::export]]
StringVector testMe(std::string input,std::string uregex) {
  boost::xpressive::sregex re = boost::xpressive::sregex::compile(uregex); // find a date
  // iterate over the days, months and years in the input
  boost::xpressive::sregex_token_iterator begin( input.begin(), input.end(), re ,-1), end;
  // write all the words to std::cout
  std::ostream_iterator< std::string > out_iter( std::cout, "n" );
  std::copy( begin, end, out_iter );
  return("Done");
}
/*** R
testMe("This is a funny sentence"," ")
*/

但它所做的只是打印出代币。我对C++很陌生,但我理解用StringVector res(10);rcpp中创建一个向量的想法(创建一个名为res的长度为10的向量),然后我可以对res[1] = "blah"进行索引。

我的问题是——如何获取boost::xpressive::sregex_token_iterator begin( input.begin(), input.end(), re ,-1), end;的输出并将其存储在向量中,以便返回它?

http://www.boost.org/doc/libs/1_54_0/doc/html/xpressive/user_s_guide.html#boost_xpressive.user_s_guide.string_splitting_and_tokenization


最终工作Rcpp解决方案

包括这一点是因为我的需求是Rcpp特定的,我不得不对提供的解决方案进行一些小的更改。

#include <Rcpp.h>
#include <boost/xpressive/xpressive.hpp>
typedef std::vector<std::string> StringVector; 
using boost::xpressive::sregex; 
using boost::xpressive::sregex_token_iterator;
using Rcpp::List;
void tokenWorker(/*in*/      const std::string& input, 
                 /*in*/      const sregex re,
                 /*inout*/   StringVector& v) 
{
  sregex_token_iterator begin( input.begin(), input.end(), re ,-1), end;
  // write all the words to v
  std::copy(begin, end, std::back_inserter(v));
}
//[[Rcpp::export]]
List tokenize(StringVector t, std::string tok = " "){
  List final_res(t.size());
  sregex re = sregex::compile(tok); 
  for(int z=0;z<t.size();z++){
    std::string x = "";
    for(int y=0;y<t[z].size();y++){
      x += t[z][y];
    }
    StringVector v;
    tokenWorker(x, re, v);
    final_res[z] = v;
  }
  return(final_res);
}
/*** R
tokenize("Please tokenize this sentence")
*/

我的问题是-如何获取boost::expression::sregex_token_iterator begin(input.begin(),input.end(),re,-1),end;并将其存储在向量中,这样我就可以返回它

你已经走到一半了。

缺少的链接只是std::back_inserter

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <boost/xpressive/xpressive.hpp>
typedef std::vector<std::string> StringVector; 
using boost::xpressive::sregex; 
using boost::xpressive::sregex_token_iterator; 

void testMe(/*in*/      const std::string& input, 
            /*in*/      const std::string& uregex,
            /*inout*/   StringVector& v) 
{
    sregex re = sregex::compile(uregex); 
    sregex_token_iterator begin( input.begin(), input.end(), re ,-1), end;
    // write all the words to v
    std::copy(begin, end, std::back_inserter(v));
}
int main() 
{
    std::string input("This is his face");
    std::string blank(" ");
    StringVector v;
     // find white space
    testMe(input, blank, v);
    std::copy(v.begin(), v.end(), 
              std::ostream_iterator<std::string>(std::cout, "|"));
    std::cout << std::endl;
    return 0;
}

输出:

This|is|his|face|

我使用了遗留的C++,因为您使用了来自boost的正则表达式库,而不是std <regex>;也许当你现在学习C++时,你最好从一开始就考虑C++14;C++14甚至会缩短这个小片段,使其更具表现力。

这是C++11版本。

除了使用标准化的<regex>的好处之外,使用<regex>的版本在运行Debian x8_64-Jessie的QuadCore Box上编译的速度大约是使用gcc-4.9和clang-3.5(-g-O0-std=c++11)的boost::expression版本的两倍。

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
//////////////////////////////////////////////////////////////////////////////
// A minimal adaption layer atop boost::xpressive and c++11 std's <regex>   //
//--------------------------------------------------------------------------//
// remove the comment sign from the #define if your compiler suite's        //
// <regex> implementation is not complete                                   //
//#define USE_REGEX_FALLBACK_33509467 1                                     //
//////////////////////////////////////////////////////////////////////////////
#if defined(USE_REGEX_FALLBACK_33509467)
#include <boost/xpressive/xpressive.hpp>
using regex = boost::xpressive::sregex; 
using sregex_iterator = boost::xpressive::sregex_token_iterator; 
auto compile = [] (const std::string& s) { 
    return boost::xpressive::sregex::compile(s);
}; 
auto make_sregex_iterator = [] (const std::string& s, const regex& re) {
    return sregex_iterator(s.begin(), s.end(), re ,-1);
};    
#else // #if !defined(USE_REGEX_FALLBACK_33509467)
#include <regex>
using regex = std::regex; 
using sregex_iterator = std::sregex_token_iterator; 
auto compile = [] (const std::string& s) { 
    return regex(s); 
}; 
auto make_sregex_iterator = [] (const std::string& s, const regex& re) {
    return std::sregex_token_iterator(s.begin(), s.end(), re, -1);
};    
#endif // #if defined(USE_REGEX_FALLBACK_33509467)
//////////////////////////////////////////////////////////////////////////////

typedef std::vector<std::string> StringVector; 

StringVector testMe(/*in*/const std::string& input, 
                    /*in*/const std::string& uregex)
{
    regex re = compile(uregex); 
    sregex_iterator begin = make_sregex_iterator(input, re), 
                    end;
    return StringVector(begin, end); // doesn't steal the strings
                                     // but try (and succeed) to move the vector
}
int main() {
    std::string input("This is his face");
    std::string blank(" ");
     // tokenize by white space
    StringVector v = testMe(input, blank);
    std::copy(v.begin(), v.end(), 
              std::ostream_iterator<std::string>(std::cout, "|"));
    std::cout << std::endl;
    return EXIT_SUCCESS;
}