• <small id='XqeUK'></small><noframes id='XqeUK'>

  • <i id='XqeUK'><tr id='XqeUK'><dt id='XqeUK'><q id='XqeUK'><span id='XqeUK'><b id='XqeUK'><form id='XqeUK'><ins id='XqeUK'></ins><ul id='XqeUK'></ul><sub id='XqeUK'></sub></form><legend id='XqeUK'></legend><bdo id='XqeUK'><pre id='XqeUK'><center id='XqeUK'></center></pre></bdo></b><th id='XqeUK'></th></span></q></dt></tr></i><div id='XqeUK'><tfoot id='XqeUK'></tfoot><dl id='XqeUK'><fieldset id='XqeUK'></fieldset></dl></div>

      <tfoot id='XqeUK'></tfoot>
    1. <legend id='XqeUK'><style id='XqeUK'><dir id='XqeUK'><q id='XqeUK'></q></dir></style></legend>
          <bdo id='XqeUK'></bdo><ul id='XqeUK'></ul>

        C++中的向量交集

        时间:2023-09-15
          <tbody id='GfB4E'></tbody>
          <legend id='GfB4E'><style id='GfB4E'><dir id='GfB4E'><q id='GfB4E'></q></dir></style></legend>

          <small id='GfB4E'></small><noframes id='GfB4E'>

            • <bdo id='GfB4E'></bdo><ul id='GfB4E'></ul>

              <tfoot id='GfB4E'></tfoot>
                • <i id='GfB4E'><tr id='GfB4E'><dt id='GfB4E'><q id='GfB4E'><span id='GfB4E'><b id='GfB4E'><form id='GfB4E'><ins id='GfB4E'></ins><ul id='GfB4E'></ul><sub id='GfB4E'></sub></form><legend id='GfB4E'></legend><bdo id='GfB4E'><pre id='GfB4E'><center id='GfB4E'></center></pre></bdo></b><th id='GfB4E'></th></span></q></dt></tr></i><div id='GfB4E'><tfoot id='GfB4E'></tfoot><dl id='GfB4E'><fieldset id='GfB4E'></fieldset></dl></div>
                  本文介绍了C++中的向量交集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有这个功能

                  vector<string> instersection(const vector<string> &v1, const vector<string> &v2);
                  

                  我有两个字符串向量,我想找到两者中都存在的字符串,然后用公共元素填充第三个向量.

                  I have two vectors of strings and I want to find the strings that are present in both, which then fills a third vector with the common elemnts.

                  如果我的向量是...

                  v1 = <"a","b","c">
                  v2 = <"b","c">
                  

                  推荐答案

                  尝试 std::set_intersection,例如:

                  Try std::set_intersection, for example:

                  #include <algorithm> //std::sort
                  #include <iostream> //std::cout
                  #include <string> //std::string
                  #include <vector> //std::vector
                  
                  std::vector<std::string> intersection(std::vector<std::string> &v1,
                                                        std::vector<std::string> &v2){
                      std::vector<std::string> v3;
                  
                      std::sort(v1.begin(), v1.end());
                      std::sort(v2.begin(), v2.end());
                  
                      std::set_intersection(v1.begin(),v1.end(),
                                            v2.begin(),v2.end(),
                                            back_inserter(v3));
                      return v3;
                  }
                  
                  int main(){
                      std::vector<std::string> v1 {"a","b","c"};
                      std::vector<std::string> v2 {"b","c"};
                  
                      auto v3 = intersection(v1, v2);
                  
                      for(std::string n : v3)
                          std::cout << n << ' ';
                  }
                  

                  这篇关于C++中的向量交集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:调用 std::vector::clear() 会将 std::vector::capacity() 设置 下一篇:单个向量中不同类别的对象?

                  相关文章

                  最新文章

                    <bdo id='glpSP'></bdo><ul id='glpSP'></ul>

                    <tfoot id='glpSP'></tfoot>
                  1. <legend id='glpSP'><style id='glpSP'><dir id='glpSP'><q id='glpSP'></q></dir></style></legend>

                    <small id='glpSP'></small><noframes id='glpSP'>

                      <i id='glpSP'><tr id='glpSP'><dt id='glpSP'><q id='glpSP'><span id='glpSP'><b id='glpSP'><form id='glpSP'><ins id='glpSP'></ins><ul id='glpSP'></ul><sub id='glpSP'></sub></form><legend id='glpSP'></legend><bdo id='glpSP'><pre id='glpSP'><center id='glpSP'></center></pre></bdo></b><th id='glpSP'></th></span></q></dt></tr></i><div id='glpSP'><tfoot id='glpSP'></tfoot><dl id='glpSP'><fieldset id='glpSP'></fieldset></dl></div>