Learn

Now we know how to create a vector, we can also initialize a vector, giving it values, as we are creating it in the same line.

For example, instead of just creating a double vector named location:

std::vector<double> location;

We can create and initialize location with specific values:

std::vector<double> location = {42.651443, -73.749302};

Here, we are storing a latitude and a longitude.

So it would look something like this:

Vector

Another way we can initialize our vector is by presizing, or setting the size.

Suppose we want to create and initialize a vector with two elements. However, we don’t know what values we want to add yet:

std::vector<double> location(2);

Here, we are creating a double vector and setting the initial size to two using parentheses.

It would look something like this:

presize

Because 0.0 is the default value for double.

Instructions

1.

Suppose the Tokyo Subway costs are as follows:

Ticket Adult Child
24-hour ¥800 ¥400
48-hour ¥1200 ¥600
72-hour ¥1500 ¥750

We have initialized the subway_adult already for you.

Initialize the subway_child vector with:

  • 400
  • 600
  • 750

Note: The vector can still be double even though the values entered are ints.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?