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;
Pass or Fail (C++) Given a vector, where each element in the vector is a [name,...
Pass or Fail (C++) Given a vector, where each element in the vector is a [name, grade] pair sort range by name, partition range into pass and fail, preserving alphabetical order within partition. The grades are the number of points earned (600 points earns a passing grade). std::vector<std::pair<std::string, int>> v { {"josh", 851}, {"mark", 600}, {"charles", 412}, {"sebnem", 1000}, {"abdol", 905}, {"imen", 300} }; --------EXPECTED OUTPUT------------------------------------------------- std::vector<std::pair<std::string, int>> w { {"abdol", 905}, {"josh", 851}, {"mark", 600}, {"sebnem", 1000}, // Everyone...
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?
C++ Sort Vector of Strings (case-sensitive) Without using libraries, how would I arrange a vector like...
C++ Sort Vector of Strings (case-sensitive) Without using libraries, how would I arrange a vector like this alphabetically? vector words {"September", "test" "seven", "apple"} The output should be: "apple, seven, September, test"
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 || ' '...
In C++: Set up a vector using an extendable array and name it ArrayVector (use an...
In C++: Set up a vector using an extendable array and name it ArrayVector (use an extendable array). It can be a template/generic class or you can set it up with a certain data type. Use a test driver to try out your ArrayVector by performing various operations.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT