In: Computer Science
In c++ how do I search a vector for a name and then determine if the name is found?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
//create a vector
vector<string> myvector;
//add some names in the vector
myvector.push_back("Damon");
myvector.push_back("Elena");
myvector.push_back("Stefan");
//name to search
string name_search="Stefan";
int flag=0;
for(int i=0;i<myvector.size();i++)
if(myvector.at(i)==name_search)
{cout<<"The name is in the vector at
position:"<<(i+1)<<"\n";flag=1;break;}
if(flag==0)
cout<<"The name is not in the vector\n";
return 0;
}