c++ - 找到两个字符串 vector 的交集

标签 c++ algorithm vector

我有两个字符串 vector ,想找到同时存在于这两个 vector 中的字符串,用公共(public)元素填充第三个 vector 。编辑:我添加了带有相应输出的完整代码 list ,这样事情就清楚了。

  std::cout << "size " << m_HLTMap->size() << std::endl;

  /// Vector to store the wanted, present and found triggers
  std::vector<std::string> wantedTriggers;
  wantedTriggers.push_back("L2_xe25");
  wantedTriggers.push_back("L2_vtxbeamspot_FSTracks_L2Star_A");
  std::vector<std::string> allTriggers;

  // Push all the trigger names to a vector
  std::map<std::string, int>::iterator itr = m_HLTMap->begin();
  std::map<std::string, int>::iterator itrLast = m_HLTMap->end();
  for(;itr!=itrLast;++itr)
  {
    allTriggers.push_back((*itr).first);
  }; // End itr

  /// Sort the list of trigger names and find the intersection
  /// Build a typdef to make things clearer
  std::vector<std::string>::iterator wFirst = wantedTriggers.begin();
  std::vector<std::string>::iterator wLast = wantedTriggers.end();
  std::vector<std::string>::iterator aFirst = allTriggers.begin();
  std::vector<std::string>::iterator aLast = allTriggers.end();

  std::vector<std::string> foundTriggers;

  for(;aFirst!=aLast;++aFirst)
  {
    std::cout << "Found:" << (*aFirst) << std::endl; 
  };

  std::vector<std::string>::iterator it;

  std::sort(wFirst, wLast);
  std::sort(aFirst, aLast);
  std::set_intersection(wFirst, wLast, aFirst, aLast, back_inserter(foundTriggers));

  std::cout << "Found this many triggers: " << foundTriggers.size() << std::endl;
  for(it=foundTriggers.begin();it!=foundTriggers.end();++it)
  {
    std::cout << "Found in both" << (*it) << std::endl;
  }; // End for intersection

然后是输出

这是部分输出, vector 中有超过 1000 个元素,所以我没有包含完整输出:

Found:L2_te1400
Found:L2_te1600
Found:L2_te600
Found:L2_trk16_Central_Tau_IDCalib
Found:L2_trk16_Fwd_Tau_IDCalib
Found:L2_trk29_Central_Tau_IDCalib
Found:L2_trk29_Fwd_Tau_IDCalib
Found:L2_trk9_Central_Tau_IDCalib
Found:L2_trk9_Fwd_Tau_IDCalib
Found:L2_vtxbeamspot_FSTracks_L2Star_A
Found:L2_vtxbeamspot_FSTracks_L2Star_B
Found:L2_vtxbeamspot_activeTE_L2Star_A_peb
Found:L2_vtxbeamspot_activeTE_L2Star_B_peb
Found:L2_vtxbeamspot_allTE_L2Star_A_peb
Found:L2_vtxbeamspot_allTE_L2Star_B_peb
Found:L2_xe25
Found:L2_xe35
Found:L2_xe40
Found:L2_xe45
Found:L2_xe45T
Found:L2_xe55
Found:L2_xe55T
Found:L2_xe55_LArNoiseBurst
Found:L2_xe65
Found:L2_xe65_tight
Found:L2_xe75
Found:L2_xe90
Found:L2_xe90_tight
Found:L2_xe_NoCut_allL1
Found:L2_xs15
Found:L2_xs30
Found:L2_xs45
Found:L2_xs50
Found:L2_xs60
Found:L2_xs65
Found:L2_zerobias_NoAlg
Found:L2_zerobias_Overlay_NoAlg
Found this many triggers: 0

可能的原因

我开始认为我编译代码的方式是罪魁祸首。我目前正在使用 ROOT(物理数据分析框架)进行编译,而不是进行独立编译。我感觉它不能很好地与 STL 算法库一起工作,这就是问题的原因,特别是考虑到有多少人似乎有代码为他们工作。我会尝试做一个独立的编译并重新运行。

最佳答案

传递 foundTriggers.begin()foundTriggers 为空,因为输出参数不会导致输出被推送到 foundTriggers。相反,它将递增迭代器超过 vector 的末尾而不调整它的大小,随机破坏内存。

您想使用插入迭代器:

std::set_intersection(wFirst, wLast, aFirst, aLast, 
    std::back_inserter(foundTriggers));

更新:正如评论中所指出的那样, vector 的大小已调整为至少对结果足够大,因此您的代码应该可以工作。请注意,您应该使用从 set_intersection 返回的迭代器来指示交集的结束 - 您的代码会忽略它,因此您还将迭代输出结束时留下的空字符串。

您能否发布一个完整的测试用例,以便我们可以查看交集是否真的为空?

关于c++ - 找到两个字符串 vector 的交集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14688301/

相关文章:

c++ - 使用 valgrind 在内存泄漏检测中抑制 "dl-hack3-cond-1"

c++ - 将 vector 传递给模板函数

java - 在java中按角度和轴旋转 vector

c++ - CentOS 6.02 64 位操作系统中的 OpenSSL 构建错误

c++ - 与arm微 Controller 的串行通信

c++ - 为什么 `pyvenv` 不安装 `python-config` ?

algorithm - 需要帮助在二叉搜索树中的两个节点之间的路径中查找最大值

algorithm - 在 julia 中计算排列的最佳方法

algorithm - C语言原始数据高速压缩

c++ - 具有不同模板的派生类对象的 vector