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. Go to the editor]

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.
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.
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.
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 vector.
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.
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 an empty vector if none.
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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/cpp-exercises/vector/index.php