Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rcpp-sregex_令牌迭代器到向量的捕获结果 我是一个R用户,我正在学习C++在Rcpp的应用。最近,我使用string.h在Rcpp中编写了一个替代R的strsplit,但它不是基于正则表达式的(afaik)。我一直在阅读有关Boost的文章,发现了sregex_token_迭代器_C++_R_Boost_Rcpp - Fatal编程技术网

Rcpp-sregex_令牌迭代器到向量的捕获结果 我是一个R用户,我正在学习C++在Rcpp的应用。最近,我使用string.h在Rcpp中编写了一个替代R的strsplit,但它不是基于正则表达式的(afaik)。我一直在阅读有关Boost的文章,发现了sregex_token_迭代器

Rcpp-sregex_令牌迭代器到向量的捕获结果 我是一个R用户,我正在学习C++在Rcpp的应用。最近,我使用string.h在Rcpp中编写了一个替代R的strsplit,但它不是基于正则表达式的(afaik)。我一直在阅读有关Boost的文章,发现了sregex_token_迭代器,c++,r,boost,rcpp,C++,R,Boost,Rcpp,以下网站有一个例子: 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; //

以下网站有一个例子:

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 );
但它所做的只是打印出代币。我对C++非常陌生,但是我理解了在代码< RCPP < /C>中用VCube > StringVector res(10)制作矢量的想法;code>(创建一个长度为10的名为res的向量),然后我可以对其进行索引
res[1]=“blah”

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


最终工作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")
*/
#包括
#包括
typedef std::vector StringVector;
使用boost::xpressive::sregex;
使用boost::xpressive::sregex\u令牌\u迭代器;
使用Rcpp::List;
void tokenWorker(/*in*/const std::string和input,
/*在*/const sregex re中,
/*输入输出*/StringVector&v)
{
sregex_令牌_迭代器begin(input.begin(),input.end(),re,-1),end;
//把所有的单词都写在v上
std::copy(开始、结束、std::back_inserter(v));
}
//[[Rcpp::导出]]
列表标记化(StringVector t,std::string tok=“”){
列出最终尺寸(t.尺寸());
sregx re=sregx::compile(tok);
对于(int z=0;z
我的问题是-我如何获取
boost::xpressive::sregex\u token\u迭代器begin(input.begin(),
input.end(),re,-1),end;并将其存储在向量中,以便返回
是吗

你已经走到一半了

缺少的环节只是


< C++ > C++ C++,14使用C++的“正则C++”,因为你使用了来自Boost的正则表达式库,而不是STD <代码> <代码>;也许你最好在学习C++的时候从一开始就考虑C++ 14;C++ 14会缩短这个小片段,使它更具表现力。

< P>,这里是C++ 11版本。 除了使用标准化的
的好处外,使用
的版本在运行Debian x86\u 64 Jessie的四核机上编译速度大约是使用gcc-4.9和clang-3.5(-g-O0-std=c++11)的boost::Xpression版本的两倍

#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;
}
#包括
#包括
#包括
#包括
//////////////////////////////////////////////////////////////////////////////
//boost::xpressive和c++11 std上的最小自适应层//
//--------------------------------------------------------------------------//
//从#定义编译器套件是否//
//执行工作尚未完成//
//#定义使用\u REGEX\u回退\u 33509467 1//
//////////////////////////////////////////////////////////////////////////////
#如果已定义(使用\u REGEX\u FALLBACK\u 33509467)
#包括
使用regex=boost::xpressive::sregex;
使用sregx_迭代器=boost::xpressive::sregx_令牌迭代器;
自动编译=[](常量std::string&s){
返回boost::xpressive::sregex::compile(s);
}; 
自动生成\u sregex\u迭代器=[](常量std::string&s,常量regex&re){
返回sregx_迭代器(s.begin(),s.end(),re,-1);
};    
#else/#if!已定义(使用_REGEX_FALLBACK_33509467)
#包括
使用regex=std::regex;
使用sregx_迭代器=std::sregx_令牌迭代器;
自动编译=[](常量std::string&s){
返回正则表达式;
}; 
自动生成\u sregex\u迭代器=[](常量std::string&s,常量regex&re){
返回std::sregex_token_迭代器(s.begin(),s.end(),re,-1);
};    
#endif/#如果定义(使用_REGEX_FALLBACK_33509467)
//////////////////////////////////////////////////////////////////////////////
typedef std::vector StringVector;
StringVector testMe(/*in*/const std::string和input,
/*in*/const std::string&uregex)
{
regex re=compile(uregex);
sregex_迭代器begin=make_sregex_迭代器(输入,re),
结束;
return StringVector(begin,end);//不窃取字符串
//但是尝试(并成功)移动向量
}
int main(){
字符串输入(“这是他的脸”);
std::字符串空白(“”);
//用空格标记
StringVector v=testMe(输入,空白);
复制(v.begin(),v.end(),
std::ostream_迭代器(std::cout,“|”);

STD:感谢C++-<代码> Rcpp < /C++ >支持C++ 11,但我不认为它支持C++ 14。所以我正在看<代码> STD::BuffixIdvuts<代码>,如何实现它。与我正在做的区别是,我需要返回<代码> StringVector <代码>,因为这实际上是要编译为一个在R中使用的函数。获取无法识别的错误消息:“没有可行的重载“=”它指向一个
\u树
文件。@标记,但你确定它支持C++11吗?如果是的话,我很乐意更新它;这不是一笔交易,而是一个很大的改进。@Mark:我并没有忘记它-当然,你可以返回一个
StringVector
,而不是将其作为/*inout*/param传递。但是关于它将执行什么,我们还不太清楚盟友以返回值发生,如果和为什么会或不会是(可能昂贵的)在遗留C++中的副本。搜索“返回值优化”(RVO)是的,我确信它支持C++ 11,虽然我几乎不理解它的意思。我正在做的是用C++编译一个函数,但是通过R中的一个库,可以从R中调用。这是为了获得对复用的或昂贵的函数的性能增强。我需要输入一个字符串,解析它并返回一个字符串向量。e你给了我工具来做这件事(我提到的错误在某些情况下会消失,所以我想我已经成功了)
#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|
#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;
}