unordered_map 在 rcpp 中的使用 [英] unordered_map use in rcpp

查看:52
本文介绍了unordered_map 在 rcpp 中的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个尝试在 Rcpp 中使用标准模板库 (STL) 学习数据结构和算法实现的新手.

I am a newbie trying to learn data structure and algorithm implementation using Standard Template Library (STL) in Rcpp.

我正在尝试使用 Rcpp 中的 unordered_map 实现一个非常基本的哈希表,从 Hadley 的 高级 R

I am trying to implement a very basic hash table using unordered_map in Rcpp, taking a hint from Hadley's Advanced R

这是我想在 Rcpp 中实现的 C++ 代码(取自 element14 博客)

Here is the C++ code I want to implement in Rcpp (taken from element14 blog)

#include <unordered_map>
#include <string>
#include <iostream>
using namespace std;

int main()
{
unordered_map<string, string> hashtable;
hashtable.emplace("www.element14.com", "184.51.49.225");

cout << "IP Address: " << hashtable["www.element14.com"] << endl;

return 0;

}

我的相同代码的 Rcpp 版本是(请忽略 int main,我现在将其命名为 int hash)

My Rcpp version for the same code is (please ignore the int main, I will name it int hash for now)

// [[Rcpp::plugins(cpp11)]]
#include <Rcpp.h>
#include <unordered_map>
#include <string>


using namespace Rcpp;

//[[Rcpp::export]]
int hash_test{
std::unordered_map<std::string, std::string> hashtable;
hashtable.emplace("www.element14.com", "184.51.49.225");

Rcout << "IP Address: " << hashtable["www.element14.com"] << endl;
return 0;
}

运行中

sourceCpp("./hash_test.cpp")

sourceCpp("./hash_test.cpp")

我收到以下错误(我不是 C++ 专业人士,所以请忽略任何愚蠢的错误)

I get the following errors (I am not a C++ professional, so please ignore any silly mistakes)

hash_test.cpp:11:46: error: expected primary-expression before ‘hashtable’
 std::unordered_map<std::string, std::string> hashtable;
                                              ^
hash_test.cpp:11:46: error: expected ‘}’ before ‘hashtable’
hash_test.cpp:11:46: error: expected ‘,’ or ‘;’ before ‘hashtable’
hash_test.cpp:12:1: error: ‘hashtable’ does not name a type
 hashtable.emplace("www.element14.com", "184.51.49.225");
 ^
hash_test.cpp:14:1: error: ‘Rcout’ does not name a type
 Rcout << "IP Address: " << hashtable["www.element14.com"] << endl;
 ^
hash_test.cpp:15:1: error: expected unqualified-id before ‘return’
 return 0;
 ^
hash_test.cpp:16:1: error: expected declaration before ‘}’ token
 }
 ^
make: *** [hash_test.o] Error 1
Error in sourceCpp("./CDM_Open_Source_ME/kohls_model/hash_test.cpp") : 
  Error 1 occurred building shared library.
In addition: Warning message:
No function found for Rcpp::export attribute at hash_test.cpp:9 

坦率地说,我不知道如何调试代码.请帮忙.

I frankly dont know how to debug the code. Please help.

推荐答案

这实际上只是缺少 hash_test 旁边的 ().

This was really just missing () next to hash_test.

以下工作:

// [[Rcpp::plugins(cpp11)]]
#include <Rcpp.h>
#include <unordered_map>
#include <string>


// [[Rcpp::export]]
int hash_test() { // missed the ()

  std::unordered_map<std::string, std::string> hashtable;

  hashtable.emplace("www.element14.com",
                    "184.51.49.225");

  Rcpp::Rcout << "IP Address: " << hashtable["www.element14.com"] << std::endl;
  return 0;
}

这篇关于unordered_map 在 rcpp 中的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆