How to Initialize a Vector with Values between a Range in C++? Last Updated : 27 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, vectors are dynamic containers that can resize automatically when elements are inserted or deleted during the runtime. In this article, we will learn how to initialize a vector with a range of values in C++. Example: Input: start = 1, end =5 Output: myVector: {1, 5, 3, 4} Initialize a Vector with Values between a Range in C++We can initialize a vector with a range of values by using a loop and a random number generator srand() with modular arithmetic such that the number generated would be in between the upper and lower bound. ApproachCreate a random number generator object and use the current time as a seed for a random generator.Define the start and end of the range for the random numbers.Use a for loop with n iterations to generate random numbers within the given range and insert them into the vector. To make the number in the desired range, use the formula: start + rand() % (end - start + 1).C++ Program to Initialize a Vector with Values between a Range C++ // C++ Program to show how to Initialize a Vector with // Values between a Range #include <cstdlib> #include <ctime> #include <iostream> #include <vector> using namespace std; int main() { // Use current time as seed for random generator srand(time(0)); // Start and end of range int start = 1, end = 5; // Vector declaration vector<int> myVector; // Initializing the vector with a range of random values for (int i = start; i <= end; i++) { int randNum = start + rand() % (end - start + 1); myVector.push_back(randNum); } // Printing the vector cout << "Vector: "; for (int i : myVector) { cout << i << " "; } cout << endl; return 0; } OutputVector: 2 5 1 4 3 Time Complexity: O(N) where N is the number of elements.Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article How to Initialize a Vector with Values between a Range in C++? G gaurav472 Follow Improve Article Tags : C++ Programs C++ STL cpp-vector cpp-random CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How to Initialize a Vector with Default Values in C++? Initialization is the process of assigning the value to the elements of vector. In this article, we will learn how to initialize the vector with default value in C++.The simplest method to initialize the vector with default value is by using constructor during declaration. Letâs take a look at a sim 2 min read How to Use a Range-Based for Loop with a Vector in C++? C++ introduced the range-based for loop as a convenient way to iterate over elements in a container. In this article, we'll look at how to iterate over a vector using a range-based loop in C++. Example Input:myVector: {1, 2, 3, 4, 5}Output:1 2 3 4 5Range-Based for Loop with Vector in C++The syntax o 1 min read How to generate a vector with random values in C++? Generating a vector with random values means creating a vector of n elements and initialize each element some random values. In this article, we will learn different methods to initialize a vector with random values in C++.The recommended method to initialize a vector with random values is by using 3 min read How to Initialize a Vector with Zero in C++? Initializing a vector with value zero means assigning the initial value 0 to all elements of vector. In this article, we will learn the different methods to initialize the vector with zero in C++.The simplest method to initialize a vector with zeros is by using vector constructor. Let's take a look 2 min read How to Initialize a Map with a Range of Key-Value Pairs in C++? In C++, a map is an associative container that stores elements formed by combining a key and a value. In this article, we will learn how to initialize a map with a range of key-value pairs in C++. Initializing a Map with a Range of Key-Value Pairs in C++To initialize a std::map with a range of key-v 2 min read Like