连接 R 和 C++。在 R 中使用无序映射

发布于 2025-01-14 07:56:53 字数 1338 浏览 0 评论 0 原文

我最近开始尝试 C++ 与 R 的接口,并且我正在努力解决以下 C++ 函数。具体来说,我尝试将 sourceCpp 与以下 .cpp 文件一起使用

#include <iostream>
#include <unordered_map>
#include <vector>
#include <string>
#include <algorithm>

#include <Rcpp.h>
using namespace Rcpp;


// [[Rcpp::export]]


std::unordered_map<std::string,std::string> example_function(std::unordered_map<std::string,std::vector<std::string>> map_1, std::unordered_map<std::string,std::vector<std::string>> map_2)
{
  std::unordered_map<std::string, std::string> example_map;
 
  //Do some things to example_map and return it

  return example_map;
}

我收到的错误位于 Exporter.h 的某些行上,它指出

no matching function for call to 'std::unordered_map<std::__cxx11::basic_string<char>, std::vector<std::__cxx11::basic_string<char> > >::unordered_map(SEXPREC*&)

invalid conversion from 'SEXP' {aka 'SEXPREC*'} to 'std::unordered_map<std::__cxx11::basic_string<char>, std::vector<std::__cxx11::basic_string<char> > >::size_type' {aka 'long unsigned int'} [-fpermissive]

我对问题的理解是 C++ 无法“翻译”unordered_map > 到 R。我知道我需要为此编写函数,一个将 C++ 转换为 R,一个将 R 转换为 C++,但我不知道从哪里开始。我将非常感谢你的帮助。

提前致谢。

I have recently started experimenting with interfacing C++ with R and I am struggling with the following C++ function. Specifically I am trying to use sourceCpp with the following .cpp file

#include <iostream>
#include <unordered_map>
#include <vector>
#include <string>
#include <algorithm>

#include <Rcpp.h>
using namespace Rcpp;


// [[Rcpp::export]]


std::unordered_map<std::string,std::string> example_function(std::unordered_map<std::string,std::vector<std::string>> map_1, std::unordered_map<std::string,std::vector<std::string>> map_2)
{
  std::unordered_map<std::string, std::string> example_map;
 
  //Do some things to example_map and return it

  return example_map;
}

The error I am getting is on some line of Exporter.h and it states

no matching function for call to 'std::unordered_map<std::__cxx11::basic_string<char>, std::vector<std::__cxx11::basic_string<char> > >::unordered_map(SEXPREC*&)

invalid conversion from 'SEXP' {aka 'SEXPREC*'} to 'std::unordered_map<std::__cxx11::basic_string<char>, std::vector<std::__cxx11::basic_string<char> > >::size_type' {aka 'long unsigned int'} [-fpermissive]

My understanding of the issue is that C++ has no way to "translate" unordered_map<std::string, std::vector <std::string> > into R. I understand that I need to write to functions for this, one that turns C++ into R, and one that turns R into C++ but I do not know where to start. I would be grateful for your help.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文