How to find a particular string is existing in a vector or not?

Hi,
I wrote a code where i need to a find in order to search whether a particular string is existing or not?

main.cpp
set<Person>p;
p.insert(Person("Amy"));
p.insert(Person("Gracy"));
p.insert(Person("Ellis"));
p.insert(Person("Jacob"));
p.insert(Person("Chris"));

set<Person::const_iterator iter;
iter=s.find("Jacob");

Person.h


Person(const std::string& n=""); //constructor declaration


I overloaded the operator '<'

bool operator<(const Person& p1,const Person& p2);

Person.cpp

Person::Person(const string& n) //constructor definition
{
setName(n);
}

bool operator<(const Person& p1,const Person& p2)
{
return p1.getName() < p2.getName();
}

What is the problem with code,i am getting the below errors while doing it:

error C2664: 'std::_Tree_const_iterator<_Mytree> std::_Tree<_Traits>::find(const Course &)' : cannot convert parameter 1 from 'const char [5]' to 'const Person &'
1> with
1> [
1> _Mytree=std::_Tree_val<std::_Tset_traits<Course,std::less<Person>,std::allocator<Person>,false>>,
1> _Traits=std::_Tset_traits<Course,std::less<Person>,std::allocator<Person>,false>
1> ]
1> Reason: cannot convert from 'const char [5]' to 'const Person'
1> No constructor could take the source type, or constructor overload resolution was ambiguous
1> Generating Code...
1> Compiling...
1> Person.cpp
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========





i suppose you have declared set as

set<Person> p;
so why are you finding it in s


iter=s.find("Jacob");
The set store Person objects so you have to pass a Person object to the find function

set<Person::const_iterator iter; should be set<Person>::const_iterator iter;
Last edited on
Hi bluecoder and Peter87,
Sorry that is my typo mistake.Peter87 thanks for your solution.
Please use [code] tags.

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.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  // 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";

Hope this helps.
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

bool operator==(Person& p) const; //declaration

Person.cpp
bool Person::operator==(Person& p) const /definition
{
return getName()==c.getName();
}
Last edited on
Hello Duoas,
Thanks for your reply.I will make sure that in the future i will post the code with tags.


Try using reference to const
bool operator==(const Person& p) const;
Topic archived. No new replies allowed.