簡體   English   中英

Upper_bound 和 lower_bound 無法編譯

[英]Upper_bound and lower_bound doesn't compile

我想獲得搜索名稱的第一個位置和最后一個位置。

我無法編譯這段代碼,盡管我看到過類似的指令正在執行。 在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>&)'|

std::lower_boundstd::upper_bound的第三個參數必須是Client對象或可以轉換為Client 如果您在Client中添加一個構造函數,允許您從std::string隱式構造一個Client ,您的代碼將起作用。 這是一個不需要對代碼進行任何其他更改的快速修復。

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; });

這個給你。

#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::equal_range調用,而不是單獨調用std::lower_boundstd::upper_bound 在這種情況下,您應該定義一個函數對象,如下面的演示程序所示

#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 表達式。

當您搜索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; });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM