[英]Insert into vector in reverse order
Say I have two vectors (parents) of same size filled with some values. 假设我有两个大小相同的向量(父母),其中填充了一些值。 Out of these two vectors, I want to create two new vectors (children) of same size.
在这两个向量中,我想创建两个相同大小的新向量(子代)。 For this, I cut the parents at a given position and fill the children after the following scheme:
为此,我按照以下方案在给定的位置剪掉了父母,并填补了孩子们:
Parent 1:
{1, 2, 3, 4, 5}
^cut
Parent 2:
{6, 7, 8, 9, 0}
^cut
Child 1:
{3, 4, 5, 0, 9}
Child 2:
{2, 1, 6, 7, 8}
In more detail , I cut parent 1 at position k
and parent 2 at position nk
, where n
is the size of parent 2. 更详细地讲 ,我在位置
k
切割了父级1,在位置nk
切割了父级2,其中n
是父级2的大小。
Child 1 will then first be filled with the second part of parent 1. After that, the second part of parent 2 will be appended to child 1 in reverse order. 然后,首先将父1的第二部分填充到子1中。此后,将以相反的顺序将父2的第二部分附加到子1上。 Child 2 will initialy be filled with the first part of parent 1 in reverse order.
子2最初将以相反的顺序填充父1的第一部分。 After that, the first part of parent 2 will be appended to child 2 in forward order.
之后,父级2的第一部分将以向前的顺序附加到子级2。
Now, inserting the parts in forward direction is not a problem. 现在,将零件向前插入就不成问题了。 The backward insertion is what I can't get to work.
向后插入是我无法工作的。
/* Set iterators at the cutting position */
int cut = 2;
auto cut1 = std::begin(parent1);
auto cut2 = std::end(parent2);
std::advance(cut1, cut);
std::advance(cut2, -cut);
/* Fill child1 with second part of parent1 */
child1.insert(child1.end(), cut1, std::end(parent1));
/* Add second part of parent2 in reverse order to child1 */
// ?
/* Fill child2 with first part of parent1 in reverse order */
// ?
/* Add first part of parent2 to child2 */
child2.insert(child2.end(), std::begin(parent2), cut2);
I was looking at std::back_inserter
and std::reverse_iterator
, but I'm not sure how they might work together and what is the most elegant way. 我在看
std::back_inserter
和std::reverse_iterator
,但是我不确定它们如何协同工作以及最优雅的方法是什么。
Also I found this and this question, which are not exactly the same but both seem to suggest, that I might have to work with simple for loops. 我也发现了这个问题,虽然这个问题并不完全相同,但似乎都暗示着我可能必须使用简单的for循环。 Since both these questions are already a little dated, is there a nice way to achieve the required behavior with standard algorithms?
由于这两个问题都已经过时,是否有一种很好的方法可以用标准算法实现所需的行为?
You can use reverse iterators to iterate through parents like: 您可以使用反向迭代器遍历父对象,例如:
#include <iostream>
#include <vector>
int main() {
std::vector<int> parent1{1, 2, 3, 4, 5};
std::vector<int> parent2{6, 7, 8, 9, 0};
std::vector<int> child1;
std::vector<int> child2;
int cut = 2;
{
auto cut1 = std::begin(parent1); // set cut1 to element 1
auto cut2 = parent2.rbegin(); // set cut2 to element 0 (reverse)
std::advance(cut1, cut); // iterate cut1 to element 3
std::advance(cut2, cut); // iterate cut2 to element 8 (reverse)
child1.insert(child1.end(), cut1, std::end(parent1)); // copy elements 3, 4 ,5
child1.insert(child1.end(), parent2.rbegin(), cut2); // copy elements 0, 9 (reverse)
}
{
auto cut1 = parent1.rbegin(); // set cut1 to element 5 (reverse)
auto cut2 = parent2.begin(); // set cut2 to element 6
std::advance(cut1, parent1.size() - cut); // iterate cut1 to element 2 (reverse)
std::advance(cut2, parent2.size() - cut); // iterate cut2 to element 9
child2.insert(child2.end(), cut1, parent1.rend()); // copy elements 2, 1 (reverse)
child2.insert(child2.end(), parent2.begin(), cut2); // copy elements 6, 7, 8
}
for (const auto& el : child1) {
std::cout << el << " ";
}
std::cout << std::endl;
for (const auto& el : child2) {
std::cout << el << " ";
}
std::cout << std::endl;
return 0;
}
Since C++14 you can use std::rbegin(parent2)
instead of parent2.rbegin()
. 从C ++ 14开始,您可以使用
std::rbegin(parent2)
代替parent2.rbegin()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.