You are using a std::set <Person>, but you are searching it as if it were a std::string. That won't work.
To determine whether or not a set contains an element, you must first have one of those elements to compare it with. The elements are not strings; each element is a Person, right?
It is not obvious, but the way to check to see if your set has that element, you should count the number of those elements in your set. (Since a set will only have a maximum of one of each element, the count will be either 1 or 0.)
// A 'set' is an unordered group of things. 'p' is a set of 'Person's.
cout << "I am creating a group of people. A person is either in the group or he is not.\n";
set<Person> p;
// Add some people to the set
cout << "I am adding five people to the group.\n";
p.insert(Person("Amy"));
p.insert(Person("Gracy"));
p.insert(Person("Ellis"));
p.insert(Person("Jacob"));
p.insert(Person("Chris"));
// Let's see if an element is in the set or not.
cout << "Enter a person's name, and I will tell you whether or not he or she is in the group.\n";
cout << "(Make sure to capitalize properly): ";
string name;
getline(cin,name);
if (p.count(Person(name)))
cout << "Yes, " << name << " is in the group.\n";
else
cout << "No, " << name << " is not in the group.\n";
// Let's list all the elements in the set.
cout << "Here are all the people in the group:\n";
for (set<Person>::const_iterator person = p.begin(); person != p.end(); ++person)
cout << person->getName() << "\n";
Hi,
When i am trying to implement with vector i am getting many errors.
vector<Person>p;
p.push_back(Person("Amy"));
p.push_back(Person("Gracy"));
p.push_back(Person("Ellis"));
p.push_back(Person("Jacob"));
p.push_back(Person("Chris"));
vector<Person>::const_iterator iter;
iter=find(p.begin(),p.end(),Person("Jacob"));
if(iter!=p.end())
{
cout<<"Value Found:";
}
Below are the errors:
c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(41): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'Person' (or there is no acceptable conversion)
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(470): could be 'bool std::operator ==(const std::_Exception_ptr &,const std::_Exception_ptr &)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(475): or 'bool std::operator ==(std::_Null_type,const std::_Exception_ptr &)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(481): or 'bool std::operator ==(const std::_Exception_ptr &,std::_Null_type)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(408): or 'bool std::operator ==(const std::error_code &,const std::error_condition &)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(416): or 'bool std::operator ==(const std::error_condition &,const std::error_code &)'
1> c:\users\x\documents\visual studio 2010\projects\set\set\Person.h(14): or 'bool Course::operator ==(Person &) const'
1> while trying to match the argument list '(Person, const Person)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(74) : see reference to function template instantiation '_InIt std::_Find<Course*,_Ty>(_InIt,_InIt,const _Ty &)' being compiled
1> with
1> [
1> _InIt=Person *,
1> _Ty=Person
1> ]
1> c:\users\x\documents\visual studio 2010\projects\set\set\main.cpp(76) : see reference to function template instantiation '_InIt std::find<std::_Vector_iterator<_Myvec>,Course>(_InIt,_InIt,const _Ty &)' being compiled
1> with
1> [
1> _InIt=std::_Vector_iterator<std::_Vector_val<Person,std::allocator<Person>>>,
1> _Myvec=std::_Vector_val<Person,std::allocator<Person>>,
1> _Ty=Course
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
In order to overcome the above errors i even overloaded the "==" operator in Person.h