gpt4 book ai didi

c++ - Rcpp - 将 sregex_token_iterator 的结果捕获到 vector

转载 作者:太空狗 更新时间:2023-10-29 20:37:51 24 4
gpt4 key购买 nike

我是一名 R 用户,正在学习 C++ 以在 Rcpp 中发挥作用。最近,我使用 string.h 在 Rcpp 中编写了 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"," ")
*/

但它所做的只是打印出 token 。我是 C++ 的新手,但我理解在 rcpp 中使用 StringVector res(10); 创建一个 vector 的想法(创建一个名为 res 的长度为 10 的 vector ),我然后可以索引 res[1] = "blah"

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

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")
*/

最佳答案

My question is - how do I take the output of boost::xpressive::sregex_token_iterator begin( input.begin(), input.end(), re ,-1), end; and store it in a vector so I can return it?

你已经完成了一半。

缺少的链接只是 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 的正则表达式库而不是标准 <regex> ;当你现在学习 C++ 时,也许你最好从一开始就考虑 C++14; C++14 甚至会缩短这个小片段并使其更具表现力。

关于c++ - Rcpp - 将 sregex_token_iterator 的结果捕获到 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33509467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com