我想获得搜索名称的第一个位置和最后一个位置。
我无法编译这段代码,尽管我已经看到了类似的指令正在执行。在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>&)'|