defining function to find match

Write your question here.
i defined the first function to find matches but i need to change it into the second function provided. it will search through a vector and return a vector of pointers to the vector of objects that match the search input. i am having trouble defining the second function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  void AddressBook::match(std::string namesearch)
{
	for (std::vector<Person>::const_iterator itr = perV.begin(); itr < perV.end(); ++itr)
	{
		if (strstr(itr->firstName.c_str(), namesearch.c_str()) || 
			strstr(itr->lastName.c_str(), namesearch.c_str()))
		{
			std::cout << itr->firstName << ' ' << itr->lastName
				<< std::endl << "Email: " << itr->email << std::endl;
		}
		
	}
}



std::vector<const Person*> AddressBook::matches(std::string prefix) const
{
	
	{
		
	}
	return std::vector<const Person*>();
}
Last edited on
First, there is no need to resort to strstr(); the std::string has find(). http://www.cplusplus.com/reference/string/string/find/


Lets look at what the match() does. It does print data from each person that does match.

The matches() is not supposed to print. What it should do with each person that does match?

Store pointer to person. Append the pointer to a list. In the end, the function returns that list.
std::vector<const Person*> AddressBook::matches(std::string prefix) const

{
std::vector<const Person*> ptrV;
for (auto iter = perV.begin(); iter < perV.end(); ++iter)
{
if (strstr(iter->firstName.c_str(), prefix.c_str()) ||
strstr(iter->lastName.c_str(), prefix.c_str()))
{
auto ptr = &(*iter);
ptrV.push_back(ptr);
}

return ptrV;
}
}

will this work
Topic archived. No new replies allowed.