迭代 Rcpp 中的命名列表

Iterate over named List in 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++)?

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

这里引用自tinytest/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"] ;
}

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