w3resource

C++ Vector: Exercises, Practice, Solution

C++ Vector [6 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts.]

The C++ Standard Library vector class is a class template for sequence containers, it stores elements of a given type in a linear arrangement, and allows fast random access to any element.

1. Write a C++ program to check whether numbers in a vector can be rearranged so that each number appears exactly once in a consecutive list of numbers. Return true otherwise false. Go to the editor
Example:
1 2 5 0 3 6 7
Check consecutive numbers in the said vector! 0
Click me to see the sample solution

2. Write a C++ program that returns the elements in a vector that are strictly smaller than their adjacent left and right neighbours. Go to the editor
Example:
Original Vector elements:
1 2 5 0 3 1 7
Vector elements that are smaller than its adjacent neighbours:
0
1
Click me to see the sample solution

3. Write a C++ program to create an n x n matrix by taking an integer (n) as input from the user. Go to the editor
Example:
Input: 2
Input an integer value: Create an n x n matrix by said integer:
2 2
2 2
Click me to see the sample solution

4. Write a C++ program to capitalize the first character of each element of a given string vector. Return the new vector. Go to the editor
Example:
Original Vector elements:
red green black white Pink
Capitalize the first character of each vector element:
Red Green Black White Pink
Click me to see the sample solution

5. Write a C++ program to verify that all of the letters in the second string appear in the first string as well. Return true otherwise false. Go to the editor
Example:
Original String elements: Python Py
Check - First string contains all letters from second string: true
Click me to see the sample solution

6. Write a C++ program that takes a vector of strings and returns only those strings that contain a number(s). Return empty vector if none. Go to the editor
Example:
Original Vector elements:
red green23 1black white
Find strings that contain a number(s) from the said vector:
green23 1black
Click me to see the sample solution

CPP Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Share this Tutorial / Exercise on : Facebook and Twitter

C++ Programming: Tips of the Day

How to use the PI constant in C++?

On some (especially older) platforms (see the comments below) you might need to

#define _USE_MATH_DEFINES and then include the necessary header file:
#include<math.h>

and the value of pi can be accessed via:

M_PI

In math.h (2014) it is defined as:

# define M_PI           3.14159265358979323846  /* pi */

but check your math.h for more. An extract from the "old" math.h (in 2009):

/* Define _USE_MATH_DEFINES before including math.h to expose these macro
 * definitions for common math constants.  These are placed under an #ifdef 
 * since these commonly-defined names are not part of the C/C++ standards.
 */

However:

  1. on newer platforms (at least on my 64 bit Ubuntu 14.04) I do not need to define the _USE_MATH_DEFINES
  2. On (recent) Linux platforms there are long double values too provided as a GNU Extension:
# define M_PIl          3.14159265358979323846

Ref: https://bit.ly/3G4BgzQ