If I have the code:
T a;
T b;
T c;
// ...
T z;
How can I iterate through them without creating std::vector<T&>
of them?
Any beautiful solution, something like (pseudo):
for (auto& it : [a, b, c, d, e, f]) {
// ...
}
(Without copies.)
If I have the code:
T a;
T b;
T c;
// ...
T z;
How can I iterate through them without creating std::vector<T&>
of them?
Any beautiful solution, something like (pseudo):
for (auto& it : [a, b, c, d, e, f]) {
// ...
}
(Without copies.)
for (auto& var : {std::ref(a), std::ref(b), std::ref(c), std::ref(d), std::ref(e), std::ref(f)}) {
// ...
}
Should do the job.
If you don't want to actually modify the "variables" then you might be able do something like
// TODO: Put your own variables here, in the order you want them
auto variables = { a, b, c, .... };
// Concatenate all strings
std::string result = std::accumulate(std::begin(variables), std::end(variables), "",
[](std::string const& first, std::string const& second)
{
return first + ' ' + second; // To add spacing
});
Note that this requires all "variables" to be of the same type (std::string
). If you have a variable that is not a string you can use std::to_string
to convert them in the first step.