Concept explainers
A vector is a container which performs like an extensible array.
- The size of the vector is dynamic.
- The series of elements are stored using the same variable name.
- It uses random-access iterator.
- If an element is added or removed, it adjusts its size automatically, which accommodates the number of elements contained in the vector.
Explanation of Solution
Emplacement:
The technique used by member function to insert new elements in a container is emplacement. When new object is inserted into a container it avoids creation of temporary objects in memory.
emplace() and emplace_back() emplacement member functions:
Two member function in a vector class uses emplacement technique namely: “emplace()” and “emaplace_back()”.
- These two member functions avoid the creation of memory when new object is inserted into a container.
- There is no need to instantiate the objects which are going to be inserted.
- The objects are just passed as an argument into the emplacement function.
- The emplacement function handles the creation of object and then forwards to the argument to its constructor.
Comparing “emplace()” and “emplace_back()” member function with “insert()” and “push_back()” :
The “insert()” and “push_back()” member functions creates a temporary memory when new objects are inserted into a container.
Whereas, the “empalce” and “emplace_back()” member function avoids the creation of memory when new objects are inserted into a container.
Want to see more full solutions like this?
Chapter 17 Solutions
Starting Out with C++ from Control Structures to Objects (9th Edition)
- In C++arrow_forwardin c++ Write a function named “getLowest” that accepts a vector of pointers to Student objects. It will go through the list and return pointers to the Student or GradStudent with the lowest student ID and GPA. Please note that the vector contains pointers to either Student or GradStudent objects. Please show that you have tested with a vector of pointers of mixed Student and GradStudent objects (in the same vector) For example, if these students and grad students are in the list Student(2000, "John Smith", 4.0) GradStudent(3000,"Nancy Brown", 3.5, 2021) ; Student(1000, "Bob Johnson", 3.0) ; GradStudent(4000, "Tim Jackson", 2.0, 2020)) ; It will return pointers to these two objects: Lowest ID: ID(1000) NAME(Bob Johnson) GPA(3.00) Lowest GPA: ID(4000) NAME(Tim Jackson) GPA(2.00) GRADUATION-YEAR(2020)arrow_forwardplease help me with codingarrow_forward
- please explain whyarrow_forwardIn C++,define a vector object and initialize it with 3 values and then define an iterator that points to elements of this object, then ask the user to enter 3 more values and then add them to the previous vector and then print all elements in the vector.arrow_forwardTranslator Using as a basis the program that allows to deal cards from a deck to a player's hand, add functionality that allows to update the deck of cards. Updating the deck of cards involves moving the cards from the deck to the beginning of the vector as the player requests a new card. At each deal, the cards in the deck must be printed to make the code in c language. The following functions must be implemented additionally: updateDeck. - This function receives two arguments: a vector with the deck of cards to update and the current size of the deck of cards. It will move the cards in the deck to the beginning of the vector. printVector - This function will receive two parameters: a vector and its dimension. The function will print the vector, and will print the values (the vector) of the initial hand, the updated deck, and the final hand. to make the code in c language Expected output :arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education