Upper_bound和lower_bound无法编译

时间:2019-11-25 21:10:27

标签: c++ sorting compiler-errors lower-bound upperbound

我想获得搜索名称的第一位和最后一位。

尽管我已经看到类似的指令正在执行,但是我无法编译此代码。给出lower_bound和upper_bound的错误。

用C ++ 11编译

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

using namespace std;

class Client
{
public:
    int id;
    string name;
    int number;
};

int main()
{
    vector<Client>::iterator low;
    vector<Client>::iterator up;
    string s_name;

    Client c1;
    c1.id = 1;
    c1.name = "jhon";
    c1.number = 123;

    Client c2;
    c2.id = 2;
    c2.name = "Mart";
    c2.number = 987;

    Client c3;
    c3.id = 3;
    c3.name = "Jhon";
    c3.number = 256;

    Client c4;
    c4.id = 4;
    c4.name = "Anna";
    c4.number = 851;

    vector<Client> vCli{c1, c2, c3, c4};

    sort(vCli.begin(), vCli.end(), [](Client a, Client b) { return a.name < b.name; });

    s_name = "Jhon";

    low = lower_bound(vCli.begin(), vCli.end(), s_name, [](Client a, Client b) { return a.name < b.name; });
    up = upper_bound(vCli.begin(), vCli.end(), s_name, [](Client a, Client b) { return a.name < b.name; });

    cout << (low - vCli.begin()) << endl;
    cout << (up - vCli.begin()) << endl;

    return 0;
}
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\5.1.0\include\c++\bits\predefined_ops.h|144|
error: no match for call to '(main()::<lambda(Client, Client)>) (Client&, const std::__cxx11::basic_string<char>&)'|

3 个答案:

答案 0 :(得分:2)

std::lower_boundstd::upper_bound的第三个参数必须是Client对象或可以转换为Client的对象。如果您在Client中添加了一个构造函数,使您可以从Client隐式构造一个std::string,那么您的代码将可以正常工作。这是一个快速解决方案,不需要对代码进行任何其他更改。

Client s;
s.name =  "Jhon";

low = lower_bound (vCli.begin(), vCli.end(), s, [](Client a, Client b) { return a.name <  b.name; });
up  = upper_bound (vCli.begin(), vCli.end(), s, [](Client a, Client b) { return a.name <  b.name; });

答案 1 :(得分:1)

您在这里。

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>

class Client
{ 
public:
    int id;
    std::string name;
    int number; 
};

int main() 
{
    std::vector<Client> vCli =
    {
        { 1, "Jhon", 123 },
        { 2, "Mart", 987 },
        { 3, "Jhon", 256 },
        { 4, "Anna", 851 },
    };

    std::sort( std::begin( vCli ), std::end( vCli ),
               []( const Client &c1, const Client &c2 )
               {
                    return c1.name < c2.name; 
               } );

    std::string s_name = "Jhon";

    auto low = std::lower_bound( std::begin( vCli ), std::end( vCli ), s_name,
                                []( const Client &c, const std::string &s )
                                {
                                    return c.name < s;
                                } );

    auto up = std::upper_bound( std::begin( vCli ), std::end( vCli ), s_name,
                                []( const std::string &s, const Client &c )
                                {
                                    return s < c.name;
                                } );

    for ( auto first = low; first != up; ++first )
    {
        std::cout << first->id << ", "  
                  << first->name << ", "
                  << first->number << '\n';
    }


    return 0;
}

程序输出为

1, Jhon, 123
3, Jhon, 256

您可以使用一个std::lower_bound来代替单独调用std::upper_boundstd::equal_range。在这种情况下,您应该定义一个函数对象,如下面的演示程序所示

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>

class Client
{ 
public:
    int id;
    std::string name;
    int number; 
};

struct Compare_by_name
{
    bool operator ()( const Client &c, const std::string &s ) const
    {
        return c.name < s;
    }

    bool operator ()( const std::string &s, const Client &c ) const
    {
        return s < c.name;
    }
};

int main() 
{
    std::vector<Client> vCli =
    {
        { 1, "Jhon", 123 },
        { 2, "Mart", 987 },
        { 3, "Jhon", 256 },
        { 4, "Anna", 851 },
    };

    std::sort( std::begin( vCli ), std::end( vCli ),
               []( const Client &c1, const Client &c2 )
               {
                    return c1.name < c2.name; 
               } );

    std::string s_name = "Jhon";

    auto low_up = std::equal_range( std::begin( vCli ), std::end( vCli ), s_name,
                                    Compare_by_name() );   


    for ( auto first = low_up.first; first != low_up.second; ++first )
    {
        std::cout << first->id << ", "  
                  << first->name << ", "
                  << first->number << '\n';
    }


    return 0;
}

此函数对象也可以用于std::lower_boundstd::upper_bound而不是其lambda表达式。

答案 2 :(得分:1)

在搜索比较器中的std::string时,一个参数必须是std::string而不是Client

low = lower_bound(vCli.begin(), vCli.end(), s_name, [](const Client& a, const std::string& b) { return a.name < b; });
up = upper_bound(vCli.begin(), vCli.end(), s_name, [](const std::string& a, const Client& b) { return a < b.name; });