[英]C++ Concatenate attributes of two vectors of different type into new vector
I have two vectors as follows: 我有两个向量,如下所示:
std::vector<Foo> v1;
std::vector<Bar> v2;
both Foo
and Bar
possess the .baz
attribute, for example of type int
. Foo
和Bar
具有.baz
属性,例如int
类型。
What I would like is to end up with a new vector, combining all .baz
attributes, in order: 我想要的是一个新向量,按顺序组合所有
.baz
属性:
std::vector<int> v3;
where v3.size() == v1.size() + v2.size()
其中
v3.size() == v1.size() + v2.size()
Is there a succinct way of achieving this? 是否有实现这一目标的简洁方法?
I don't know that you need something more succinct than simply: 我不知道您需要的不仅仅是简单的东西:
int main() {
std::vector<Foo> foos = get_foos();
std::vector<Bar> bars = get_bars();
std::vector<int> vec;
auto merge = [](auto const& arg) {return arg.baz;};
std::transform(foos.begin(), foos.end(), std::back_inserter(vec), merge);
std::transform(bars.begin(), bars.end(), std::back_inserter(vec), merge);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.