// The language is c++ //please explain step by step By using vector handle dynamic, and for loop to print this message separately in each line. "This is C++ code with no commas semicolons of fullstops:;
// The language is c++ //please explain step by step By using vector handle dynamic, and for loop to print this message separately in each line. "This is C++ code with no commas semicolons of fullstops:;
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
// The language is c++
//please explain step by step
By using
"This is C++ code with no commas semicolons of fullstops:;
Expert Solution
Step 1
NOTE: - To clearly demonstrating the concept, the size of the vector is printed at multiple places.
EXPLANATION: -
Defining Vector Dynamically: -
vector<string>* myVector = new vector<string>(5);
- The above line dynamically creates and initializes the vector named myVector with 5 empty strings.
- Right now myVector is already having 5 elements.
- Vector is dynamic in nature, it automatically increases its size when more elements are added to it.
- The size function on myVector gives the current size of the vector. Since it is a pointer, so arrow pointer is used to access the method
- cout<<"vector size: "<< myVector->size() <<endl;
- Any push_back operation will result in an increment in the size of the vector named myVector.
push_back function: -
- This function adds elements at end of the vector, and each operation increments the vector size by 1.
- myVector->push_back(message);//message is added at index 5
- Vector also follows zero-based indexing.
- A total of 5 push_back operations are performed.
- Hence, the vector size becomes 10 now.
Now vector contains 5 empty strings, and 5 messages, a total of 10 elements.
Displaying the vector: -
- The vector elements are printed using for loop which runs from index 0 till the size of the vector - 1 that is 9.
- To access the element of the vector, dereferencing is required, since it is a pointer.
- First, the vector variable myVector is dereferenced, then the element present at the index variable i is stored in variable myStringusing the rectangular brackets.
- string myString = (*myVector)[i];
- First, the vector variable myVector is dereferenced, then the element present at the index variable i is stored in variable myStringusing the rectangular brackets.
- Initially, there are five empty strings, so to prevent printing the empty strings, if the condition is applied, only if the string is not empty, it is printed on the console.
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images
Knowledge Booster
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.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education