mercredi 19 février 2020

Is there a way to initialize a vector by index in c++11?

It is easy to fill the elements of a vector by index after it has been initialized:

std::vector<int> myVector {0, 0, 0};
int indexOfInterest = 1;
myVector[indexOfInterest] = 999;
// myVector now contains {0, 999, 0}

However, is there a way to initialize a vector directly with the index? indexOfInterest may change in my code in the future, and I would like to avoid hard-coding the vector with

std::vector<int> myVector {0, 999, 0};

Is there some syntax like

int indexOfInterest = 1;
std::vector<int> myVector[3] {indexOfInterest : 999};
// desired: myVector contains {0, 999, 0}

that can be used in C++11 to achieve this effect?

Aucun commentaire:

Enregistrer un commentaire