gpt4 book ai didi

rcpp - 迭代 Rcpp 中的命名列表

转载 作者:行者123 更新时间:2023-12-01 12:23:39 29 4
gpt4 key购买 nike

我在 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"] ;
}

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

关于rcpp - 迭代 Rcpp 中的命名列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41893975/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com