如果你有这样的矢量,那么将它加倍或乘以的最短路径是什么?
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"}};
当然这些版本不起作用:
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, ""}}
当然我可以使用这个,但它非常蹩脚:
for (size_t curr = 0, size = v3.size(); curr < size; ++curr)
v3.push_back(v3.at(curr));
有更短的方法吗?