我有两个STL向量A和B,需要将它们合并到第三个,其中元素应该以某种方式排序,输出向量中的每个第n个元素应该是向量B.我的当前代码看起来像这样:
std::vector<int> a(10, 4);
std::vector<int> b(10, 8);
std::vector<int> c;
static const std::size_t STEP(3);
std::vector<int>::const_iterator bIt = b.begin();
for(std::vector<int>::const_iterator aIt = a.begin();
aIt != a.end(); ++aIt)
{
c.push_back(*aIt);
if((c.size() + 1) % STEP == 0)
{
c.push_back(*bIt);
++bIt; //assume b is large enough
}
}
Run Code Online (Sandbox Code Playgroud)
矢量c现在看起来像:4 4 8 4 4 8 ...
这很好,但我很好奇,如果没有更优雅的解决方案.有没有办法使用STL算法而不是我的手写循环?