Different ways to print elements of vector in C++

In this article, you will learn about the different ways to print elements of vectors in C++. But before discussing the different ways, you must know about the Vectors with their advantages and disadvantages.

What are Vectors?

  • Vectors are similar to dynamic arrays in that the container manages their storage.
  • They can automatically resize themselves in response to the insertion or deletion of an element.
  • Vector elements are stored in a contiguous manner, allowing iterators to access and traverse them.
  • Data is inserted at the end of vectors. Inserting at the end requires a variable amount of time because the array may occasionally need to be extended.
  • As no scaling occurs, removing the final element just requires a fixed amount of time. Inserting and erasing at the beginning or in the middle is linear in time.

Advantages of the Vectors:

There are several advantages of the vectors. Some main advantages of the vectors are as follows:

  1. Dynamic size: You can easily add or remove elements because vectors can resize dynamically. Thus, vectors are preferable to arrays, which have fixed dimensions.
  2. Random access: Like an array, elements in a vector can be indexed continuously. Random access functions can now be optimized.
  3. Automated memory management: Vector handles memory allocation and allocation automatically. Unlike arrays, you don't have to manually free or allocate memory.
  4. Support from the standard library: Because vectors are included in the C++ standard library (<vector> header), they are compatible with many different platforms and compilers
  5. Algorithm Compatibility: Vectors are easily handled and manipulated to integrate with standard algorithms provided by standard libraries.

Disadvantages of the Vectors:

There are several disadvantages of the vectors. Some main disadvantages of the vectors are as follows:

  1. Overhead: Vectors have less overhead than arrays because they hold additional mathematical data (such as size and capacity).
  2. Adjacent memory: As vectors store elements in adjacent memory locations, it can be more efficient to insert or remove elements from a vector than it is with linked lists or other data structures
  3. Resizing overhead: A vector may need to allocate the old memory, allocate new memory, and move all parts of it to new memory if they need to be expanded beyond what is currently available. Resizing a vector can be expensive when it comes to large vectors.

Different ways to Print Elements of a Vector:

There are several ways to print elements of a vector. Some main ways are as follows:

1. Using a for loop:

Program:

Let us take a program to print elements of a vector using a for loop in C++.

Output:

Different ways to print elements of vector in C++

2. Using Iterators:

Program:

Let us take a program to print elements of a vector using iterators in C++.

Output:

Different ways to print elements of vector in C++

3. Using Standard Algorithms with Iterators:

Program:

Let us take a program to print elements of a vector using standard algorithms with iterators in C++.

Output:

Different ways to print elements of vector in C++

4. Printing elements in reverse order with reverse iterators:

Program:

Let us take a program to print elements of a vector in reverse order with reverse iterators in C++.

Output:

Different ways to print elements of vector in C++

5. Using overload (<<) operator:

Program:

Let us take a program to print elements of a vector using overload(<<) operator in C++.

Output:

Different ways to print elements of vector in C++

6. Printing elements to the standard output using an ostream_iterator and the copy algorithm:

Program:

Let us take a program to print elements of a vector using ostream_iterator and the copy algorithm in C++.

Output:

Different ways to print elements of vector in C++

7. Working with std:: lambda functionaccumulate to concatenate elements together to form a string, which is then printed:

Program:

Output:

Different ways to print elements of vector in C++

8. Applying pointer arithmetic and a pointer to the data of the vector:

Program:

Output:

Different ways to print elements of vector in C++




Latest Courses