James MV
James MV

Reputation: 8717

Optimizing declaring repeat variables?

Is there anyway in which to avoid doing something like this:

std::queue<myStruct>  part1, part2, part3, part4, part5, part6, part7, part8, part9, part10;

void setup(){
myVector.push_back(part1);
myVector.push_back(part2);
myVector.push_back(part3);
myVector.push_back(part4);
myVector.push_back(part5);
myVector.push_back(part6);
myVector.push_back(part7);
myVector.push_back(part8);
myVector.push_back(part9);
myVector.push_back(part10);
}

Although this only goes up to part10 I may be going up towards 50 or more. Is it just a part of coding that somewhere it has to be declared or is there a dynamic way I can declare and assign these to the myVector these queues without writing it like this?

TIA

Upvotes: 0

Views: 80

Answers (2)

Trevor Hickey
Trevor Hickey

Reputation: 37806

lets say your struct looks like this:

struct myStruct{

    //random variables that your struct may contain
    int num;
    std::string str;
    char ch;

    //Default Constructor
    //This allows your to create your structs without specifying any values;
    myStruct():
    num{999},str{"default"},ch{'z'}{}

    //Parameterized Constructor
    //This allows you to create your structs with specified values during initialization
    myStruct(int const numIn, std::string const& strIn, char const chIn):
    num{numIn},str{strIn},ch{chIn}{}

};

this will allow you to create instances the following way:

//calling default constructor
myStruct part1, part2, part3;

//calling parameterized constructor
myStruct part4{4,"4",'4'}, part5{5,"5",'5'}, part6{6,"6",'6'};

Now you want to put each one into a container and then put those containers into another container?

//this is a vector holding one deque holding 6 structs
vector<deque<myStruct>> vec{{part1,part2,part3,part4,part5,part6}};
+-------+
|  vec  |
|       |
|  [0]  |
+-------+
    |
   \'/
+-------+
|  deq  |
|       |-> part1
|  [0]  |
+-------+
|  deq  |
|       |-> part2
|  [1]  |
+-------+
|  deq  |
|       |-> part3
|  [2]  |
+-------+
|  deq  |
|       |-> part4
|  [3]  |
+-------+
|  deq  |
|       |-> part5
|  [4]  |
+-------+
|  deq  |
|       |-> part6
|  [5]  |
+-------+


//this is a vector holding 6 deques each holding 1 struct
vector<deque<myStruct>> vec2{{part1},{part2},{part3},{part4},{part5},{part6}};
+-------++-------++-------++-------++-------++-------+
|  vec  ||  vec  ||  vec  ||  vec  ||  vec  ||  vec  |
|       ||       ||       ||       ||       ||       |
|  [0]  ||  [1]  ||  [2]  ||  [3]  ||  [4]  ||  [5]  |
+-------++-------++-------++-------++-------++-------+
    |        |        |        |        |        |
   \'/      \'/      \'/      \'/      \'/      \'/
+-------++-------++-------++-------++-------++-------+
|  deq  ||  deq  ||  deq  ||  deq  ||  deq  ||  deq  |
|       ||       ||       ||       ||       ||       |
|  [0]  ||  [0]  ||  [0]  ||  [0]  ||  [0]  ||  [0]  |
+-------++-------++-------++-------++-------++-------+
    |        |        |        |        |        |
   \'/      \'/      \'/      \'/      \'/      \'/
  part1    part2    part3    part4    part5    part6

We can do something even better though. Let's initialize your structs while pushing them into a deque, while pushing the deque(s) into a vector.

//this is a vector holding one deque holding 6 structs
vector<deque<myStruct>> vec{{{1,"1",'1'},{2,"2",'2'},{3,"3",'3'},{4,"4",'4'},{5,"5",'5'},{6,"6",'6'}}};

//this is a vector holding 6 deques each holding 1 struct
vector<deque<myStruct>> vec2{{{1,"1",'1'}},{{2,"2",'2'}},{{3,"3",'3'}},{{4,"4",'4'}},{{5,"5",'5'}},{{6,"6",'6'}}};

keep in mind though, this is all C++11 initialization features. I recommend you read this article to get accustomed. http://www.informit.com/articles/article.aspx?p=1852519

To compile code like this, make sure your compiler is up-to-date and you have the appropriate library files. If your using gcc, compile with this flag:

g++ -std=c++0x -o main main.cpp

Upvotes: 0

Seth Carnegie
Seth Carnegie

Reputation: 75130

One easy way:

std::vector<std::queue<myStruct>> myVector(10);

Done!

Upvotes: 8

Related Questions