双向(或乘法)向量的最快和最短的方法是什么

时间:2016-12-12 14:24:31

标签: c++ c++11

如果你有这样的矢量,那么将它加倍或乘以的最短路径是什么?

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));

有更短的方法吗?

0 个答案:

没有答案