Assume proper includes have been executed, but not using directive or declaration. Write a definition of an iterator for a vector named vec of int values. Write a for loop that will display the contents vec on the screen, separated by spaces starting at the last element in the vector and proceeding to the first. Use iterators for the loop control.

What will be an ideal response?


#include
#include
using std::vector;
using std::cout;
using std::endl;

int main()
{
std::vector::reverse_iterator itr;
std::vector vec;

for(int i=0; i<10; i++)
vec.push_back(i);

for (itr = vec.rbegin(); itr != vec.rend(); itr++)
cout << *itr << " ";
cout << endl;
return 0;
}
Explanation: The parts that are requested in the problem are in bold face. The std:: on vector is necessary to make this code work with VC++6.0 prior to patch level 5. This hack does not affect the program under Borland command line compiler 5.5.

Computer Science & Information Technology

You might also like to view...

In a table, press ________ to move right to left from cell to cell

A) Ctrl + End B) Alt + End C) Tab + PgDn D) Shift + Tab

Computer Science & Information Technology

Points on a motion path can be dragged off of the path line to delete them

Indicate whether the statement is true or false

Computer Science & Information Technology

When you ________ a file that is in Compatibility Mode, you change its version to Word 2013

Fill in the blank(s) with correct word

Computer Science & Information Technology

How are graphics imported into a table?

What will be an ideal response?

Computer Science & Information Technology