c++ - Take variable number of arguments and put them in std::vector -


i'm making class - let's call container - containing std::vector , special logic decides how vector values picked. want add method adding multiple values class 1 call. method adds 1 item:

void loopgenerator::add(randomstripe &stripe) {     stripes.push_back(new singlestripe(stripe)); } 

i'd similar method called this:

loopgenerator gen = loopgenerator(); gen.add(randomstripe(), randomstripe(), randomstripe() ... , as want ... ); 

and add parameters inner std::vector.

is possible standard libraries, or best without them?

you can use std::initializer_list. example

#include <initializer_list> #include <algorithm> #include <vector> #include <iterator>       //...  void loopgenerator::add( std::initializer_list<randomstripe> stripe ) {     std::transform( stripe.begin(), stripe.end(),                     std::back_inserter( stripes ),                    []( const randomstripe &s ) { return new singlestripe( s ); } ); } 

and call like

gen.add( { randomstripe(), randomstripe(), randomstripe(), /*...*/ } ); 

Comments

Popular posts from this blog

javascript - How to synchronize the Three.js and HTML/SVG coordinate systems (especially w.r.t. the y-axis)? -

javascript - How do I find how many occurences are there of a highlighted string, and which occurence is it? -

java - Reading data from multiple zip files and combining them to one -