Skip to main content

How To Think Like a Computer Scientist C++ Edition The Pretext Interactive Version

Section 10.3 Copying vectors

There is one more constructor for vectors, which is called a copy constructor because it takes one vector as an argument and creates a new vector that is the same size, with the same elements.
vector<int> countCopy(count);
Although this syntax is legal, it is almost never used for vectors because there is a better alternative:
vector<int> countCopy = count;
The = operator works on vectors in pretty much the way you would expect.
Listing 10.3.1. Take a look at this active code, which uses the copy constructor.

Checkpoint 10.3.1.

Multiple Response How would you make a copy of vector<double> decimals called nums ?
  • vector<double> nums = decimals;
  • This is one way to make a copy.
  • vector<double> decimals = nums;
  • This makes a copy of nums called decimals.
  • vector<double> nums (decimals);
  • This is one way to make a copy.
  • vector<double> decimals (nums);
  • This makes a copy of nums called decimals.

Checkpoint 10.3.2.

You have attempted 1 of 4 activities on this page.