Let's assume that we start with the following variable declarations and initializations: std::string s = "perfect"; std::list a1{"Boo", "is", s, "today"}; const std::list a2{"Boo", "will", "always", "be", s}; std::map m1{{3, 4.5}, {0, -7.25}, {1, 9.75}}; Now assume that you wrote the following variable declarations and initializations using the auto specifier in place of a type. Assume that each is separate from the others (i.e., it's not an error that they all have the same variable name) and that any necessary C++ Standard Library headers have been included already. auto x = 2005; auto x = a1.begin(); auto x = a2.end(); auto x = a1.size(); auto x = m1[3]; auto& x = m1[3]; auto x = *(m1.begin()); auto x = std::accumulate( a1.begin(), a1.end(), std::string{}.size(), [](auto a, auto b) { return a + b.size(); }); For each of these, what type would be inferred by auto for the variable x that has been declared and initialized? Next, answer the following follow-up question: Suppose that a1 had been declared as a std::vector instead of as a std::list. Which of the following lines of code would you expect to be faster, on average, when using vectors than they would have been when using lists? Briefly, in a sentence or two, explain why. a1.push_back("!"); a1.insert(a1.begin(), "Little");
This is in C++
Let's assume that we start with the following variable declarations and initializations:
std::string s = "perfect"; std::list<std::string> a1{"Boo", "is", s, "today"}; const std::list<std::string> a2{"Boo", "will", "always", "be", s}; std::map<int, double> m1{{3, 4.5}, {0, -7.25}, {1, 9.75}};
Now assume that you wrote the following variable declarations and initializations using the auto specifier in place of a type. Assume that each is separate from the others (i.e., it's not an error that they all have the same variable name) and that any necessary C++ Standard Library headers have been included already.
- auto x = 2005;
- auto x = a1.begin();
- auto x = a2.end();
- auto x = a1.size();
- auto x = m1[3];
- auto& x = m1[3];
- auto x = *(m1.begin());
- auto x = std::accumulate(
- a1.begin(), a1.end(), std::string{}.size(),
- [](auto a, auto b) { return a + b.size(); });
For each of these, what type would be inferred by auto for the variable x that has been declared and initialized?
Next, answer the following follow-up question:
- Suppose that a1 had been declared as a std::vector<std::string> instead of as a std::list<std::string>. Which of the following lines of code would you expect to be faster, on average, when using
vectors than they would have been when using lists? Briefly, in a sentence or two, explain why.- a1.push_back("!");
- a1.insert(a1.begin(), "Little");
Trending now
This is a popular solution!
Step by step
Solved in 2 steps