简体   繁体   English

将Boost变体的向量过滤为新向量?

[英]filter a vector of boost variant into a new vector?

I am looking for the best way to filter a vector of the boost variant which has been defined like this: 我正在寻找最好的方法来过滤定义为boost的矢量的矢量:

 boost::variant<T1*, T2, T3> Var;
 std::vector<Var> Vec;

when I call this vector, what is the best way to filter only T2 bounded type and insert into new vector? 当我将此向量称为“向量”时,仅过滤T2有界类型并将其插入新向量的最佳方法是什么? or in other way, I want something like this 或者以其他方式,我想要这样的东西

std::vector<T2> T2Vec = ...(how to filter it from Vec)....

thank you! 谢谢!

EDIT: Since using a "visitor" is more robust, I'm also wondering of anyone could give me a solution using "visitor"? 编辑:由于使用“访客”更强大,我也想知道有人可以使用“访客”给我解决方案吗?

thanks again! 再次感谢!

The simplest solution would be to loop over Vec , checking if the element is a double , then push the double into a T2Vec . 最简单的解决方案是遍历Vec ,检查元素是否为double ,然后将double推入T2Vec This is a C++03 version: 这是C ++ 03版本:

for (std::vector<Var>::const_iterator it = Vec.begin(); it != Vec.end(); ++it)
{
  if (it->which() == 1) T2Vec.push_back(boost::get<T2>(*it));
}

C++11 version: C ++ 11版本:

for (const auto& v : Vec)
{
  if (v.which() == 1) T2Vec.push_back(boost::get<T2>(v));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM