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");

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter7: User-defined Simple Data Types, Namespaces, And The String Type
Section: Chapter Questions
Problem 1TF
icon
Related questions
Question

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.

  1. auto x = 2005;
  2. auto x = a1.begin();
  3. auto x = a2.end();
  4. auto x = a1.size();
  5. auto x = m1[3];
  6. auto& x = m1[3];
  7. auto x = *(m1.begin());
  8. auto x = std::accumulate(
  9. a1.begin(), a1.end(), std::string{}.size(),
  10. [](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:

  1. 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");
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Datatypes
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning