1

If you have a vector like this one, what is the shortest way to double or multiply it?

vector<pair<int, string>> v = {{1, "a"}, {2, "b"}, {3, "c"}};
vector<pair<int, string>> desired_v = {{1, "a"}, {2, "b"}, {3, "c"}, {1, "a"}, {2, "b"}, {3, "c"}};

Of course these version won't work:

v1.insert(v1.end(), v1.begin(), v1.end());
// {{1, "a"}{2, "b"}{3, "c"}{1, ""}{2, ""}{3, ""}}

for (auto p : v2)
    v2.push_back(p);
// {{1, "a"}{2, "b"}{3, "c"}{1, "a"}{2, ""}{3, ""}}

Of course I can use this one, but it is pretty lame:

for (size_t curr = 0, size = v3.size(); curr < size; ++curr)
    v3.push_back(v3.at(curr));

Is there a shorter way to do this?

tobias88
  • 121
  • 1
  • 6

0 Answers0