我在R中有一个命名列表:
l = list(a=1, b=2)
我想在Rcpp中使用此列表,并迭代值和名称。理想情况下,它可能是这样的(为了简洁起见使用C ++ 11格式化):
void print_list (List l)
for (pair < String, int > &p: l)
cout << p.first << ": " << p.second;
有没有办法(甚至使用普通的C ++)?
答案 0 :(得分:4)
其中许多用例实际上在单元测试中可见。
// [[Rcpp::export]]
CharacterVector integer_names_get( IntegerVector y ){
return y.names() ;
}
// [[Rcpp::export]]
int integer_names_indexing( IntegerVector y ){
return y["foo"] ;
}
这应该足以让你获得你想要迭代的向量。