Question

In: Computer Science

In c++ how do I search a vector for a name and then determine if the...

In c++ how do I search a vector for a name and then determine if the name is found?

Solutions

Expert Solution

#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;
}


Related Solutions

How do i change the vector v to size 20 in C++. Also given the following,...
How do i change the vector v to size 20 in C++. Also given the following, which line of code stores 40 into the 4th column of the 6th row of array dataVals? const int rows = 7; const int cols = 5; int dataVals[rows][cols]; Select one: a. dataVals[5, 3] = 40; b. dataVals[5 * 3] = 40; c. dataVals[3][5] = 40; d. dataVals[5][3] = 40;
What is an interrupt vector table. How do I create an interrupt vector table in Arm...
What is an interrupt vector table. How do I create an interrupt vector table in Arm Architecture . Please include as much information as you can, including pictures, examples etc. Thank you very much.
PLEASE DO THIS IN C#Design and implement a programming (name it NextMeeting) to determine the day...
PLEASE DO THIS IN C#Design and implement a programming (name it NextMeeting) to determine the day of your next meeting from today. The program reads from the user an integer value representing today’s day (assume 0 for Sunday, 1 for Monday, 2 for Tuesday, 3 for Wednesday, etc…) and another integer value representing the number of days to the meeting day. The program determines and prints out the meeting day. Format the outputs following the sample runs below. Sample run...
How do I determine price indifference?
How do I determine price indifference?
In Clojure, how do I enter a vector of vectors and print the specified index in...
In Clojure, how do I enter a vector of vectors and print the specified index in each vector? i.e. I enter [1 2 3 4] [5 6 7 8] and my function gives me [1 5] when I call (index vector 0)?
Hi i have this for a practical tomorrow (C++): a) Write a function vector merge(vector a,...
Hi i have this for a practical tomorrow (C++): a) Write a function vector merge(vector a, vector b) that merges two vectors, alternating elements from both vectors. If one vector is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer vector. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 Then merge returns the vector 1 9 4 7 9 4...
1. How do I write a query that displays the name (concatenate the first name, middle...
1. How do I write a query that displays the name (concatenate the first name, middle initial, and last name), date of birth, and age for all students? Show the age with no decimal places, and only include those students who are 21 or older. Order by age, as shown below: (Hint: Use the TRUNC function. The ages may be different, depending on the date that the query is run.) SELECT S_FIRST || ' ' || S_MI || ' '...
how do i run 3 files on c++?
how do i run 3 files on c++?
Create a family tree with a 3D vector in C++. Then, determine the worst, average and...
Create a family tree with a 3D vector in C++. Then, determine the worst, average and best time complexities of the program.
Please use C++ for Mac (XCode) In this program, a user can search for a name...
Please use C++ for Mac (XCode) In this program, a user can search for a name within a list of names. Here are the steps the program will implement: 1. Reads the names from the file names.txt into a vector that stores strings. 2. Sorts the names in alphabetical order using selection sort. 3. The program then prompts the user to enter a name. 4. The program then uses binary search to see if the name is in the list....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT