C++ 搜索按词典排序的向量<;向量<;int>&燃气轮机;在c++;

C++ 搜索按词典排序的向量<;向量<;int>&燃气轮机;在c++;,c++,sorting,c++11,vector,stl,C++,Sorting,C++11,Vector,Stl,我需要对向量进行排序词典 所以,在按字典排序之前,我的向量向量是: ((0,100,17,2),(2,3,1,3),(9,92,81,8),(0,92,92,91),(10,83,7,2),(1,2,3,3)) 为此,我使用以下功能: std::sort(vecOfVectors.begin(),vecOfVectors.end(),lexicographical_compare); bool compare(vector<int> A,vector<int> B)

我需要对向量进行排序词典

所以,在按字典排序之前,我的向量向量是:

((0,100,17,2),(2,3,1,3),(9,92,81,8),(0,92,92,91),(10,83,7,2),(1,2,3,3))
为此,我使用以下功能:

std::sort(vecOfVectors.begin(),vecOfVectors.end(),lexicographical_compare);
bool compare(vector<int> A,vector<int> B)
{
  int i=0;
  while(i<(A.size()<B.size())?A.size():B.size())
    {
      if(A[i]<B[i])
        {
          return 1;
        }
      else if(A[i]==B[i])
        {
         i++;
        }
      else
        {
          return 0;
        }
      }
 return 0;
}
所以,我的向量在按字典排序后现在是:

((0,92,92,91),(0,100,17,2),(1,2,3,3),(2,3,1,3),(9,92,81,8),(10,83,7,2))
现在给定一个向量,我需要搜索它在这个排序向量中的位置——像二进制搜索这样的东西会很有用。在C++ STL中有一些内置函数,我可以用来执行二进制搜索吗? 例如:
(0,92,92,91)的位置为0;(0100,17,2)的位置为1;(1,2,3,3)的位置为2;(2,3,1,3)的位置为3;(9,92,81,8)的位置为4;(10,83,7,2)的位置是5。

不需要添加
词典学\u比较
,这就是向量的比较方式

根据您要查找的确切用例,或-所有这些操作都在排序向量上进行

下面是一个包含数据和c++11的完整示例。它构造向量,对其排序(显示您提到的顺序),然后在向量中查找单个值

#include <vector>
#include <iostream>
#include <algorithm>

void print(const std::vector<int> & v) {
    std::cout<<"(";
    for(auto x=v.begin(); x!=v.end(); ++x) {
        std::cout<<*x<<",";
    }
    std::cout<<")";
}

void print(const std::vector<std::vector<int>> & v) {
    std::cout<<"(";
    for(auto x=v.begin(); x!=v.end(); ++x) {
        print(*x);
        std::cout<<",";
    }
    std::cout<<")"<<std::endl;
}

int main() {

    std::vector<std::vector<int>> v {
        {0,100,17,2},
        {2,3,1,3},
        {9,92,81,8},
        {0,92,92,91},
        {0,92,92,91},
        {10,83,7,2},
        {1,2,3,3}
    };

    print(v);

    std::sort(v.begin(), v.end());

    print(v);

    std::vector<int> key = { 0,100, 17, 2 };

    auto it = std::lower_bound(v.begin(), v.end(), key);
    if(it!=v.end() && key==*it) {
        std::cout<<"Found it"<<std::endl;
    } else {
        std::cout<<"Not found"<<std::endl;
    }

}
#包括
#包括
#包括
无效打印(const std::vector&v){

正如我在评论中所说,std::cout创建一个函数:

std::sort(vecOfVectors.begin(),vecOfVectors.end(),lexicographical_compare);
bool compare(vector<int> A,vector<int> B)
{
  int i=0;
  while(i<(A.size()<B.size())?A.size():B.size())
    {
      if(A[i]<B[i])
        {
          return 1;
        }
      else if(A[i]==B[i])
        {
         i++;
        }
      else
        {
          return 0;
        }
      }
 return 0;
}
bool比较(向量A、向量B)
{
int i=0;

while(看,谢谢你.)你能用一个向量的例子来说明这一点吗?你忘了
std::equal_range
它可以是
.end()
,在这种情况下你不能取消引用它。你必须检查
如果(it!=vecOfVector.end()&*i==key).
。在std::sort(v.begin(),v.end()中;我需要提供词典吗_compare@JannatArora不,不需要。不需要添加
lexicographical\u compare
,这已经是向量比较的方式。不需要任何二进制搜索,只需创建一个函数compare(vector,vector)这是MadhuKumar的一个例子。你在网上搜索了一个二进制搜索算法吗?C++是5的大小。你可以用A siz(5)替换。向下投票:仅适用于长度为5的固定向量。向量已经有一个重载的
操作符orry,不,不是真的。它只适用于
int
。充分概括这一点,您将重新发明
操作符