【问题标题】:String vector's sort compare and delete字符串向量的排序比较和删除
【发布时间】:2013-04-23 13:01:41
【问题描述】:

在 C++ 中我有

vector < vector <string> > Kblist;

Kblist里面有很多子句,clauses=kblist.size();的个数和Kblist里面的每一个子句都是字符串型的向量,句中的每一个词都在Kblist[i]里面分割。

"I love you"中找到一个句子,在"you love i"中找到另一个句子并从Kblist中删除这两个句子的最快方法是什么,我的代码可能正在运行,但我认为它是因为太多循环太慢了。所以我想知道有没有更好的解决方案,比如使用排序、clause1==clause2 或其他方法。

          for (int a=0; a<KBlist.size(); a++){
                for (int b=a+1; b<KBlist.size(); b++){
                    int checksize=0;
                    if (KBlist[a].size()==KBlist[b].size()) {
                        for (int c=0; c<KBlist[a].size(); c++){ 
                            for (int d=0; d<KBlist[b].size(); d++){
                                if (KBlist[a][b]==KBlist[c][d]&&KBlist[a][b+1]==KBlist[c][d]) {
                                    checksize=checksize+1; 
                                    break;
                                }
                            }
                        }
                       if (checksize==c.size()) {
                                        inset=1;
                                        break;
                                    }
                    }
                }
            }
        }while (duplicate==0);

【问题讨论】:

    标签: c++ sorting vector compare


    【解决方案1】:

    您可以遍历每个 std::vector 并使用标准库的算法。 有std::find

    // find example
    #include <iostream>     // std::cout
    #include <algorithm>    // std::find
    #include <vector>       // std::vector
    
    int main () {
      int myints[] = { 10, 20, 30 ,40 };
      int * p;
    
      // pointer to array element:
      p = std::find (myints,myints+4,30);
      ++p;
      std::cout << "The element following 30 is " << *p << '\n';
    
      std::vector<int> myvector (myints,myints+4);
      std::vector<int>::iterator it;
    
      // iterator to vector element:
      it = find (myvector.begin(), myvector.end(), 30);
      ++it;
      std::cout << "The element following 30 is " << *it << '\n';
    
      return 0;
    }
    

    std::find_if

    // find_if example
    #include <iostream>     // std::cout
    #include <algorithm>    // std::find_if
    #include <vector>       // std::vector
    
    bool IsOdd (int i) {
      return ((i%2)==1);
    }
    
    int main () {
      std::vector<int> myvector;
    
      myvector.push_back(10);
      myvector.push_back(25);
      myvector.push_back(40);
      myvector.push_back(55);
    
      std::vector<int>::iterator it = std::find_if (myvector.begin(), myvector.end(), IsOdd);
      std::cout << "The first odd value is " << *it << '\n';
    
      return 0;
    }
    

    当您使用std::string 时,这应该不是什么大问题。

    【讨论】:

      【解决方案2】:

      在您的场景中,最好将std::multiset&lt; vector &lt;string&gt; &gt; 与比较器一起使用,以您需要的方式比较std::vector&lt;string&gt;。这将为您提供具有相邻重复值和廉价插入/擦除的排序容器。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-12-05
        • 2015-04-03
        • 2015-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多