Study your flashcards anywhere!

Download the official Cram app for free >

  • Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off

How to study your flashcards.

Right/Left arrow keys: Navigate between flashcards.right arrow keyleft arrow key

Up/Down arrow keys: Flip the card between the front and back.down keyup key

H key: Show hint (3rd side).h key

A key: Read text to speech.a key


Play button


Play button




Click to flip

12 Cards in this Set

  • Front
  • Back
Like an array, a ___ has a base type, and like an array, a ___ stores a collection of
values of its base type. However, the syntax for a ___ type and a ___ variable declaration
is different from the syntax for arrays.
You declare a variable, v, for a vector with base type int as follows:
vector<int> v;
The notation vector<Base_Type> is a ___ class, which means you can plug in any
type for Base_Type and that will produce a class for vectors with that base type.
Vector elements are indexed starting with ___, the same as arrays.
the following changes the value of the ith element of the vector v and then outputs
that changed value. (i is an int variable.)
v[i] = 42;
cout << "The answer is " << v[i];
you cannot initialize the ith element using v[i]; you can
only change an element that has already been given some value.
To add an element to
a vector for the first time, you normally use the member function push_back.
the following gives initial values to elements 0, 1, and
2 of the vector sample:
vector<double> sample;
You can
write out all the elements currently in the vector sample as follows:
for (int i = 0; i < sample.size( ); i++)
cout << sample[i] << endl;

The function size returns a value of type unsigned int, not a value of type int.
If you want to be very safe, you can always apply a type cast
to convert the returned unsigned int to an int, or in cases like this for loop, use a
loop control variable of type unsigned int as follows:
for (unsigned int i = 0; i < sample.size( ); i++)
cout << sample[i] << endl;
There is a vector constructor that takes one integer argument and will initialize the
number of positions given as the argument. For example, if you declare v as follows:
vector<int> v(10);

then the first ten elements are initialized to 0 and v.size( ) would return 10. You can
then set the value of the ith element using v[i] for values of i equal to 0 through 9. In
particular, the following could immediately follow the declaration:
for (unsigned int i = 0; i < 10; i++)
v[i] = i;

To set the ith element for i greater than or equal to 10, you would use push_back.
Using Square Brackets beyond the Vector Size
If v is a vector and i is greater than or equal to v.size( ), then the element v[i] does not yet
exist and needs to be created by using push_back to add elements up to and including position
i. If you try to set v[i] for i greater than or equal to v.size( ), as in
v[i] = n;
then you may or may not get an error message, but your program will undoubtedly misbehave at
some point.
The size of a vector is the number of elements in the vector. The capacity of a vector is the number
of elements for which it currently has memory allocated. For a vector v, the size and capacity
can be recovered with the member functions v.___( ) and v.___( ).