[Rcpp-devel] map<string,T> is now wrappable

Romain Francois francoisromain at free.fr
Sun Jan 31 11:52:12 CET 2010


Hello,

This did not make the cut for Rcpp 0.7.4 which was released yesterday, 
but I've made some changes to that std::map<std::string,T> can be 
wrap()'ed ... if T can. For example :


Here is a simple example from the unit tests :

	funx <- cfunction(signature(),
	'
	std::map< std::string, int > m ;
	m["b"] = 100;
   	m["a"] = 200;
   	m["c"] = 300;
   	return wrap(m) ;
	',
	Rcpp=TRUE, verbose=FALSE, includes = "using namespace Rcpp;" )


 > res <- funx()
 > res
   a   b   c
200 100 300
 > str( res )
  Named int [1:3] 200 100 300
  - attr(*, "names")= chr [1:3] "a" "b" "c"
	

Here is a more involved example, wrapping map<string,vector<int>> :

	funx <- cfunction(signature(),
	'
	typedef std::pair<std::string,std::vector<int> > _pair ;
	std::multimap< std::string,std::vector<int> > m ;
	std::vector<int> b ; b.push_back(1) ; b.push_back(2) ;
	m.insert( _pair("b", b) );
   	
	std::vector<int> a ; a.push_back(1) ; a.push_back(2) ; a.push_back(2) ;
   	m.insert( _pair("a", a) );
   	
   	std::vector<int> c ; c.push_back(1) ; c.push_back(2) ; 
c.push_back(2) ; c.push_back(2) ;
   	m.insert( _pair("c",  c) );
   	return wrap(m) ;
	',
	Rcpp=TRUE, verbose=FALSE, includes = "using namespace Rcpp;" )

 > res <- funx()
 > str( res )
List of 3
  $ a: int [1:3] 1 2 2
  $ b: int [1:2] 1 2
  $ c: int [1:4] 1 2 2 2

Have fun !

Romain

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/KfKn : Rcpp 0.7.2
|- http://tr.im/JOlc : External pointers with Rcpp
`- http://tr.im/JFqa : R Journal, Volume 1/2, December 2009




More information about the Rcpp-devel mailing list