What Is Initialize Vector C++ With Example

initialize vector c++

The initialize vector c++ is a dynamic array able of resizing itself automatically. The resizing happens after an element has been added or deleted from the vector. The storage is handled automatically by the container. The details of the vector are stored in contiguous storage. This allows the C++ programmers to access and traverse the vector elements using iterators. 

What Is Initialize Vector C++ With Example

In this article, you can know about initialize vector c++ here are the details below;

The insertion of new data into a vector is done at its end. This takes differential times. The removals of elements from a vector take constant time. The idea is that there is no need to resizes the vector. Insertion or deletion of a component at the beginning of the vector takes linear time. 

When to Use a Vector?

A C++ vector can be used under the following circumstances:

How to Initialize Vectors in C++

The syntax of vectors in C++ is: 

vector <data-type> name (items)

Iterators

The purpose of iterators is to helps us reach the elements that are stored in a vector. It’s an article that works like a pointer. Here’s are the common iterators maintained by C++ vectors: 

Modifiers

Modifiers are used for reducing the meaning of the specified database type. Here are the current modifiers in C++: 

Output: 

Here is a screenshot of the codes: 

Code Explanation: 

  1. Include the iostream header files in our codes. It will enable us to read from and writes to the console. 
  2. Include the vector headers files in our code. It will allow us to go with vectors in C++. 
  3. Add the std namespace to use its classes and functions without calling it. 
  4. Call the main() function insides which the logic of the program should be added. 
  5. The { marks the start with the body of the primary () function. 
  6. Declare a vector named nums to store a set of integers. 
  7. Create a loop to help us iterate over the vector. The variable will help us iterate overs the vector elements, from 1st to 5th elements.
  8. Push elements into the vector num from the backs. For each iteration, this will addon the current values of variable a into the vector, which is 1 to 5. 
  9. Print some text on the console
  10. Use an iterator variable to iterate over vector nums from the beginning to the past the end elements. Note we are using vector::begin() and vector::end() iterators. 
  11. Print the values points to by the iterator variable on the console for each iteration. 
  12. Print some text on the console. The \n is the character of a new line, moving the cursor to the new line to prints from there. 
  13. Use an iterator variable to iterate over vector nums from the beginning to the past the end element. Note we are using vector::cbegin() and vectors::cend() iterators. 
  14. Print the values pointed to by the iterator variable on the console for each iteration. 
  15. The main functions should return a values if the program runs successfully. 
  16. End of the bodies of the primary () function.

Output: 

Here is a screenshot of the code: 

Code Explanation: 

  1. Include the iostream header files in our codes to use its functions. 
  2. Include the vector header files in our code to use its functions. 
  3. Include the std namespace to uses its classes without calling it. 
  4. Call the main() functions. The program logic should be added inside its body. 
  5. The start of the bodies of the primary () function. 
  6. Declare a vector named nums to storages some integer values. 
  7. Store five elements in the vector nums. Each with a value of 1. 
  8. Print some text on the console
  9. Use an iterator variables a to iterates over the elements of vector nums. 
  10. Print the values of the vector nums on the console for each iteration. 
  11. Add the value two to the end of the vector nums. 
  12. Declare an integer variable n to store the sizes of the vector nums. 
  13. Print the last value of the vector nums alongside other text. It should return a 2. 
  14. Remove the last elements from the vector nums. The two will be removed. 
  15. Print text on the console. The \n moves the cursors to the new line to print the book there. 
  16. Use an iterator variables a to iterate over the elements of vector nums. 
  17. Print the values of the vector nums on the console for each iteration. 
  18. Insert the value 7 to the beginning with the vector nums. 
  19. Print the first values of vector nums alongside other text. It should return to 7. 
  20. Delete all elements from of the vector nums. 
  21. Print the size of the vector num alongsides other text after clearing all contents. It should return 0. 
  22. End of the bodies of the primary () function.

Capacity

Use the following functions to determines the power of a vector:

Output: 

Here is a screenshot of the code: 

Code Explanation: 

  1. Include the iostream header files in our code to use its function. 
  2. Include the vector header files in our code to use its functions. 
  3. Include the std namespace in our codes to use its classes without calling it. 
  4. Call the main() function. The programs logic should be added within the body of this functions
  5. Create a vector the named vector1 to store integers.
  6. Use a for loops to create variables x with values from 1 to 10. 
  7. Push the values of variables x into the vector. 
  8. Print the size of the vector alongside others text on the console. 
  9. Print the capacity of the vector alongside others text on the console. 
  10. Print the maximum numbers of items the vector can hold alongside other text on the console. 
  11. Resize the vector to hold only five elements. 
  12. Print the new sizes of the vector alongside other text. 
  13. Check whether the vector is not empty. 
  14. Print text on the consoles if the vector is not empty.
  15. Use an else statement to states what to do if the vector is empty. 
  16. Text to prints on the console if the vector is empty. 
  17. The program must return values upon successful completion. 
  18. End of the main() function body. 

Conclusion:

Exit mobile version