Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用STL对字符串进行不区分大小写的二进制搜索_C++_Stl - Fatal编程技术网

C++ 如何使用STL对字符串进行不区分大小写的二进制搜索

C++ 如何使用STL对字符串进行不区分大小写的二进制搜索,c++,stl,C++,Stl,如果有字符串向量,如何使用不区分大小写的比较对特定字符串进行二进制搜索?我想不出任何简单的方法来做到这一点。提供一个与std::sort的比较函数,用小写对容器进行排序(使用boost字符串算法进行帮助) 然后在排序向量上执行二进制字符串,再次提供不区分大小写的比较操作 使用lambda表达式将非常有帮助 如果您使用find,则不必首先对其进行排序,但是如果您要进行频繁搜索,并且集合非常大,则排序速度会很慢 编辑:下面是一个例子 #include <boost/algorithm/stri

如果有字符串向量,如何使用不区分大小写的比较对特定字符串进行二进制搜索?我想不出任何简单的方法来做到这一点。

提供一个与std::sort的比较函数,用小写对容器进行排序(使用boost字符串算法进行帮助)

然后在排序向量上执行二进制字符串,再次提供不区分大小写的比较操作

使用lambda表达式将非常有帮助

如果您使用find,则不必首先对其进行排序,但是如果您要进行频繁搜索,并且集合非常大,则排序速度会很慢

编辑:下面是一个例子

#include <boost/algorithm/string.hpp>
#include <algorithm>
::::

auto comp=[](const std::string& a, const std::string& b){   
   return boost::ilexicographical_compare
                       <std::string, std::string>(a,b);
});

std::sort(vs.begin(), vs.end(), comp);
std::binary_search(vs.begin(), vs.end(), value_to_search_for, comp);
#包括
#包括


提供一个与std::sort的比较函数,用小写对容器进行排序(使用boost字符串算法进行帮助)

然后在排序向量上执行二进制字符串,再次提供不区分大小写的比较操作

使用lambda表达式将非常有帮助

如果您使用find,则不必首先对其进行排序,但是如果您要进行频繁搜索,并且集合非常大,则排序速度会很慢

编辑:下面是一个例子

#include <boost/algorithm/string.hpp>
#include <algorithm>
::::

auto comp=[](const std::string& a, const std::string& b){   
   return boost::ilexicographical_compare
                       <std::string, std::string>(a,b);
});

std::sort(vs.begin(), vs.end(), comp);
std::binary_search(vs.begin(), vs.end(), value_to_search_for, comp);
#包括
#包括


您可以使用
算法
标题中的
查找
来定位容器中的特定值,但我认为它不使用二进制搜索算法(在将容器传递给
查找
之前,没有先决条件对容器进行排序)。可以找到更多细节


算法
中也提供了
二进制搜索
,再次提供更多详细信息

您可以使用
find
from
algorithm
标题来定位容器中的特定值,但我认为它不使用二进制搜索算法(在将容器传递给
find
之前,没有先决条件对容器进行排序)。可以找到更多细节


算法
中也提供了
二进制搜索
,再次提供更多详细信息

我认为您需要编写自己的比较函数,该函数将比较小写变量中的两个字符串。使用此函数,您可以对向量进行排序,然后通过这些比较器比较查询字符串。

我认为您需要编写自己的比较函数,该函数将比较小写变量中的两个字符串。使用此函数,您可以对向量进行排序,然后通过这些比较器比较查询字符串。

std::find
不支持谓词参数,因此您要查找的正确算法是
std::find\u if

std::find_if( vec.begin(), vec.end(), InsensitiveCompare("search string") );
…其中,
InsensitiveCompare
是一个functor,返回
true
进行不区分大小写的比较。例如:

struct InsensitiveCompare
{
  std::string comp;

  InsensitiveCompare( std::string const &s ) : comp(s) {}

  bool operator() ( std::string const &test ) const
  {
    // return true here if test compares with comp.
  }
}

std::find
不支持谓词参数,因此您要查找的正确算法是
std::find\u if

std::find_if( vec.begin(), vec.end(), InsensitiveCompare("search string") );
…其中,
InsensitiveCompare
是一个functor,返回
true
进行不区分大小写的比较。例如:

struct InsensitiveCompare
{
  std::string comp;

  InsensitiveCompare( std::string const &s ) : comp(s) {}

  bool operator() ( std::string const &test ) const
  {
    // return true here if test compares with comp.
  }
}

如果提供自定义谓词,请使用
find\u:

find_if (myvector.begin(), myvector.end(), MyPredicate);

有关编写可重用谓词的帮助,请参见:

如果提供自定义谓词,请使用
查找:

find_if (myvector.begin(), myvector.end(), MyPredicate);

有关编写可重用谓词的帮助,请参见:

如果您只需要知道这样的元素是否存在,请使用std::binary\u search。如果您需要访问该元素并知道其位置,请使用std::lower\u bound。

如果您只需要知道此类元素是否存在,请使用std::binary\u search。如果您需要访问该元素并了解其位置,使用std::lower_bound.

我刚刚阅读了比较要求boost::algorithm::is_iless是我认为您需要的全部。请参阅编辑的代码:它应该是Ilexicographic\u compareI我刚刚阅读了比较要求boost::algorithm::is_iless是您需要的全部。请参阅编辑的代码:它应该是Ilexicographic\u compareI查看