Showing posts with label CPP Vector. Show all posts
Showing posts with label CPP Vector. Show all posts

Tuesday, March 15, 2016

Declare a vector object of 10 string objects and an array object of 10 string objects. Show the necessary header files and don’t use using. Do use a const for the number of strings.

Leave a Comment

Question:

Declare a vector object of 10 string objects and an array object of 10 string objects. Show the necessary header files and don’t use using. Do use a const for the number of strings.

Answer:

#include <string>
#include <vector>
#include <array>
const int Str_num {10};  // or = 10
...
std::vector<std::string> vstr(Str_num);
std::array<std::string, Str_num> astr;

Source:

C++ Primer Plus Sixth Edition Chapter Review

Read More

Write a code fragment that asks the user to enter a positive integer and then creates a dynamic array of that many ints. Do this by using new, then again using a vector object.

Leave a Comment

Question:

Write a code fragment that asks the user to enter a positive integer and then creates a dynamic array of that many ints. Do this by using new, then again using a vector object.

Answer:

This assumes that the iostream and vector header files have been included and
that there is a using directive:
unsigned int size;
cout << "Enter a positive integer: ";
cin >> size;
int * dyn = new int [size];
vector<int> dv(size);

Source:

C++ Primer Plus Sixth Edition Chapter Review

Read More