Message-ID: <CADNH-PvsjCrDRr3iq8ioFoaqpjfEQmo8TYxAJd5bvx-n5=5_RA@mail.gmail.com>
Date: 2012-08-31T06:53:50Z
From: Christian Gunning
Subject: [Rcpp-devel] Filling a list with iterator, or setting names from CharacterVector/SEXP?
In-Reply-To: <CADNH-PuoVEmdTd+1XhsK++RTpoTcS1y71yHxov6RhjPu09+U9Q@mail.gmail.com>
To clarify a little, I can avoid most of this hassle by constructing
my named lists in R. I would like to compute on the names of those
lists on the C++ end, though -- things akin to R's
which(names(mylist)=='myname') and easily looping through names.
-Christian
On Thu, Aug 30, 2012 at 9:52 PM, Christian Gunning <xian at unm.edu> wrote:
>
>
> using namespace Rcpp ;
> class World {
> public:
> World(SEXP my_) : my(my_) {
> mynames = my.attr("dimnames");
> CharacterVector rownames = mynames[0];
> nrows = rownames.size();
> myrows = Rcpp::List(0);
> for (int i=0; i<nrows; i++) {
> Rcpp::CharacterVector tmpname(rownames[i]);
> std::string thisname = as<std::string>(tmpname);
> myrows[thisname] = i;
> }
> }
> Rcpp::List myrows;
> Rcpp::List getrows() {
> return myrows;
> }
>
> private:
> Rcpp::NumericMatrix my;
> Rcpp::List mynames;
> int nrows;
> };
--
A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal ? Panama!