Suppose I have classes
class A {
//...
};
struct B {
explicit B(const A&);
//...
};
and I have a container of A's, from which I would like to construct a container of B's. What is an idiomatic way to do this in c++ 03?
Tried and failed:
std::vector<A> source = fillSourceObjects();
std::vector<B> target;
// 1) won't compile; presumably I need a static helper function,
// but I would like to avoid that
std::transform(source.begin(), source.end(), std::back_inserter(target), B);
std::transform(source.begin(), source.end(), std::back_inserter(target), B::B);
// 2) won't compile; "... error: no match for 'operator=' in '* __result = *__first'
std::copy(source.begin(), source.end(), target.begin());
您可以使用带有序列的std::vector<T>
构造函数将A
s序列转换为B
s序列:
std::vector<B> target(source.begin(), source.end());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.