Enter your Sign on user name and password.

• Follow us on:
Start learning today, and be successful in your academic & professional career. Start Today!
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Intermediate C++

• ## Related Services

 1 answerLast reply by: Ram Manohar OrugantiTue Nov 4, 2014 6:27 PMPost by Ram Manohar Oruganti on November 4, 2014Hi,I thought that begin and end give the value of at the start and end. So, how come the iterator could know the address when given a value? 1 answerLast reply by: Alvin SylvainTue Nov 20, 2012 7:20 PMPost by Edwin Clement on November 17, 2012How can I write a function maximum that computes the largest element in a list

### Containers, Vector

• Create a program that declares a vector for 25 integers. Use the vector constructor to initialize it so that each element equals 0. Use a “for” loop to print out each element to confirm that it was properly set.
• Create a program that declares a vector for doubles, but do not initialize it. I.e., it will start off empty. Use a “for” loop for 50 times, and each loop, use “push_back()” to add a new value to the vector. The value should be computed based on the index.
• E.g., something like, 5.0*index or (including <cmath>), sin((double)index / 57.2958) etc. Whatever you think would be an interesting computation.
• In a separate loop, “cout” the values to confirm that they are set as you expected.
• Modify the program above so that the “cout” is in the same loop as the “push_back()” method. In that same section, “cout” the new size and new capacity of the vector.
• Modify the program above to add a “while” loop after the vector data has been set. The loop contents will include a “pop_back()” method call, plus “cout” the new size and new capacity. The “while” loop should terminate when the vector is empty.
• Create a compilation unit with the following. You’ll need it for the next assignments.
• class named T
• has two private fields, int “aInt” and double “aDouble”
• has public methods to set and get each field
• has default constructor that sets both fields to 0
• has constructor that sets “aInt” to parameter, and “aDouble” to 0
• has constructor that sets “aDouble” to parameter, and “aInt” to 0
• has constructor that set both fields to parameters
• has constructor that copies the fields from a reference to T parameter
(e.g., `T(T &t) {aInt = t.aInt; aDouble = t.aDouble;}`)
(also known as a copy constructor)
• has destructor that prints out, “Destroying, aInt (value), aDouble (value)”, where “value” is set to the current field values
• Modify the first assignment to make a vector for type T. Initialize the int field to -1. “cout” both fields in the for loop.
• Modify the program above to initialize the double field to -1.5.
• Modify the program above to initialize each element to (-1, -1.5). Hint: to set both fields, you can instantiate a variable of type T, initialized to those values, and use that variable in the vector constructor.
• Modify the program above use an iterator instead of an index to loop through each element. Also in this loop, change each element according to some calculation, so that each value is different. “cout” the before and after value.
• Modify the program above to erase the middle 5 elements from the vector. I.e., remove elements 10 through 14 and leave the rest. “cout” the new vector contents, size and capacity.

### Containers, Vector

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

• Intro 0:00
• Lesson Overview 0:11
• Containers 1:50
• Vector
• List
• Set, Map
• Data Access
• Iterators
• Container Information
• Vector 4:53
• Memory Re-Allocation
• Declare Vector of 'Type' 7:37
• Size & Capacity
• Vector<type> Variable
• Vector<type> Variable (Initial Size, Initial Value)
• Vector<type> Variable (Initial Size)
• Access Vector Data 10:27
• Example: Typical Array Access
• Method Call Access
• Other Data Access 12:26
• Front
• Back
• Use Empty Function to Test
• Vector Information 14:36
• Resize
• Reserve
• Vector Iterators 19:11
• Example: Data Elements May be Accessed Like Standard Array
• Data Elements May be Accessed Using Iterators
• Vector Iterator Begin/ End 23:30
• Begin
• Eng
• Reverse Begin
• Reverse Iterator
• 'Short?' Cuts 25:21
• Declaring Loop Index Inside the 'for' Loop
• With Iterators
• Modifying Vector Data 26:54
• Push Back
• Pop Back
• Insertions 29:32
• Example: Insert New Value at Beginning
• Example: Insert Value in Middle
• More Vector Operations 32:12
• Empty
• Erase
• Clear
• Assign
• Compile-Time Initialization 34:34
• Example 1
• Example 2
• Example 3
• Caution-Invalidating Iterators 37:59
• Contained! 39:56