find string in vector

Oct 15, 2013 at 8:15pm
So the program reads contents from a text file into a vector and then the user enters in a string that they want to search for. The program iterates through the vector to find the string and then saves that line to another vector to display later(incase there is more then 1 instance of the string found). Here is what I have atm:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void doSearch(vector<string> &phonelist, string searcher, vector<string> &holdNumbers)
{

	int i = 0;
	string value;

	for (i = 0; i < phonelist.size(); i++)
	{
		if (phonelist.at(i) == searcher)
		{
			value = phonelist.at(i);
			holdNumbers.push_back(value);
		}
	}
	cout << holdNumbers.at(0);
}


I just get an R6010 error -abort() has been called.
Oct 15, 2013 at 9:02pm
Line 15 will cause an exception to be thrown if no matching numbers were found.
Oct 15, 2013 at 9:46pm
Consider something like http://www.cplusplus.com/reference/algorithm/copy_if/

(and then naturally print only if !empty() )
Last edited on Oct 15, 2013 at 9:46pm
Topic archived. No new replies allowed.