如何输出vector中的类的成员和binary_search的位置?
#include <algorithm>
#include <vector>
#include <iterator>
#include <iostream>
#include <functional>
#include <string>
using namespace std;
class A
{
public:
int id;
string name;
};
class comp
{
public:
bool operator() (const A& a, const A& b) const
{
return a.id < b.id;
}
};
int main()
{
vector<A> vt;
A a;
for (int i=0; i<10; i++)
{
a.id = i;
a.name = i;
vt.push_back (a);
}
//下面编译不过去,我要输出vt中包含的所有A类的id
//copy (vt.begin(), vt.end(),
// ostream_iterator<A>(cout, "\n"));
A b;
b.id = 3;
bool ret = binary_search(vt.begin(), vt.end(), b, comp()); //我同时想得到检索到的A的name怎么办?
if (ret)
cout << "Found" << endl;
else
cout << "Not Exist" << endl;
}