在Rcpp中迭代命名列表

时间:2017-01-27 12:25:12

标签: rcpp

我在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 ++)?

1 个答案:

答案 0 :(得分:4)

其中许多用例实际上在单元测试中可见。

我引用unitTests/cpp/Vector.cpp

获取姓名:

// [[Rcpp::export]]
CharacterVector integer_names_get( IntegerVector y ){
    return y.names() ;
}

按名称索引

// [[Rcpp::export]]
int integer_names_indexing( IntegerVector y ){
    return y["foo"] ;
}

这应该足以让你获得你想要迭代的向量。