The following code defines a vector and an iterator. Rewrite the statement that
ID: 3724429 • Letter: T
Question
The following code defines a vector and an iterator. Rewrite the statement that defines the iterator so it uses the auto key word. vector strv {"one", "two", "three"); 60. vector. :iterator it strv.begin(); vector or ints Haneu v. write a statement 5); e following statement dermes vector v {1, 2, 3, = 4, 62. The following statement defines a vector of ints named v. The vector's initial contents are: 1, 2, 3, 4, 5. Write code to insert the value 999 into the vector, so its contents are: 1, 2, 999, 3,4, 5. vector v = {1, 2, 3, 4, 5); and the values should be ints. 64. Suppose a program defines a map as follows: mapcint, string> customers Write lowing elements: 9001, "Jen Williams" 9002, "Frank Smith" 9003, "Geri Rose" ook statements that use the map class's emplace member function to insert the fol- at the following vector definition: vector v = {7, 2, 1, 6, 4, 3, 5); wite code that displays a message indicating whether 65. the value 6 is found in to search the vector for ssage indica the vector. Use the STL binary_search( the value 6. ) algorithm function object created from theExplanation / Answer
Here I’m using c++ code..
60)
Here the auto keyword is requsting to the compiler is to deduce the variable type from initialization
This is expression by using auto keyword::
This is one way..
for (auto it = strv.begin(); it != strv.end(); it++) {
cout << *it << endl;
}
We can write in another one..this is better than before
for (auto& it : strv) {
cout << it << endl;
}
62)
Here I’m writing program using vectors and iterators
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector<int>v1;
vector<int>v2;
//Here I’m taking two vectors of same size
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
v1.push_back(5);
//These are the actual vector contains
//Now we need to add element after 2
for (size_t i = 0; i<5; i++){
//Here we are setting the value of v1[i] to the v2[i]
v1[i]=v2[i];
}
//Here the value will be add in the third position
v1.insert(v1.begin()+2, 999);
}
64) using map c++ program
#include <map>
#include <iostream>
int main ()
{
std::map<int,char> customers;
// This is a insert fuction to add the values
customers.insert ( std::pair<char,int>(9001,’Jen Williams’) );
customers.insert ( std::pair<char,int>(9002,’Frank Smith’) );
customers.insert ( std::pair<char,int>(9003,’Geri Rose’) );
std::pair<std::map< int,char>::iterator,bool> ret;
ret = customers.insert ( std::pair<int,char>(9003,’Geri Rose’) );
if (ret.second==false) {
std::cout << "element is ' already existed";
std::cout << " with a value of " << ret.first->second << ' ';
}
// showing the contents :
std::cout << “This map contains: ";
for (it=customers.begin(); it!=customers.end(); ++it)
std::cout << it->first << " => " << it->second << ' ';
//Instead of =>if i give the give spece it wil print space between values..
return 0;
}
This will give
9001=>Jen Williams
9002=>Frank Swith
9003=>Geri Rose
65)
we can search an element by using our defined function or default function
#include <algorithm>
#include <iostream>
#include <vector>
bool bsfunction(int i,int j)
{
return (i<j);
}
int main () {
int myints[] = {7,2,1,6,4,3,5};
std::vector<int> v(myints,myints+7);
std::cout << "looking for a 6... ";
if (std::binary_search (v.begin(), v.end(), 6, bsfunction))
std::cout << "found! "; else std::cout << "not found. ";
// this is for default comparision and searching
}
Output :
looking for element 6.. element is found
looking for element 13 ..element is not found
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.