Sig
Sig

Reputation: 91

Is there a way to cycle through variables? (C++)

I am creating a class with 18 different variables, most of them doubles. I need to declare all of them and also perform the same operation on each of them. Is there any way to use a loop to cycle through all the declarations? For example, instead of saying:

double a = 0.0;

double b = 0.0;

double c = 0.0;

...

double z = 0.0;

Is it possible to loop them like this:

while(variables are undefined)

define the next undefined variable

end loop

EDIT: Thank you for the answers, everyone. I didn't want to use an array at first because it would make remembering which element represents what value more difficult. I will look into map, and if that doesn't work I think I found a way to organize the array that I can remember.

Upvotes: 0

Views: 3736

Answers (5)

cenouro
cenouro

Reputation: 735

Use a map or an array/vector. You can loop through them by using those.

Upvotes: 1

Bryn McKerracher
Bryn McKerracher

Reputation: 683

As mentioned, it's best to use an array or vector in C++ like so:

Using a Vector:

std::vector<double> MyVector;
//Use 'push_back' to add values to the vector
MyVector.push_back(0.00);

To perform an operation on all the values in your vector, you can do this:

for (auto& i : MyVector)
    someOperation(i);

In your case, we're adding elements, so we can use a loop like this:

for (unsigned int i = 0; i < 20; ++i) 
    MyVector.push_back(some_value);

Using an Array:

If you know how many values you're going to need, you could also use a fixed array like so (otherwise it's generally easier to use a vector):

double MyArray[20] = {0}; //Creates an array of 20 doubles and sets them all to 0

//We can set individual values using the [] operator:
MyArray[0] = 2.3; //This sets the first element to 2.3

Performing an operation on the array's elements is similar to what we did with the vector:

for (unsigned int i = 0; i < 20; ++i) 
    someOperation(MyArray[i]);

Similarly, for defining arrays with a loop:

for (unsigned int i = 0; i < 20; ++i) 
    MyArray[i] = some_value;

Upvotes: 2

Humam Helfawi
Humam Helfawi

Reputation: 20284

If you are not able to change your struct, leave them as variables and make a vector of pointers to them:

class foo{
public:
foo(){
    all.push_back(&a);
    all.push_back(&b);
    ...
    all.push_back(&z);
}
std::vector<double*> all; 
private:
double a,b,c,...z;

};

and then you can sth like:

int main(){
    foo f;
    for(auto& i:f.all){
        *i=0.0;
    }
}

Upvotes: 0

Ajay Kulkarni
Ajay Kulkarni

Reputation: 3039

You'd probably want to try this:

double double_values[18];
for(int i = 0; i < 18; i++) {
    //Your code here
}  

Upvotes: 0

0xd0d0
0xd0d0

Reputation: 328

Since you're using variables all of the same type, and you know how many you need, you can use an array to store the values and then access them using a loop.

For example:

double dbl_values[18];
for(int i = 0; i < 18; i++) {
    dbl_values[i] = something; // Define variables here
}

Upvotes: 2

Related Questions