Getting started with Vectors

I recently started learning about vectors in c++ but I have issues regarding where to utilize them .
Can you suggest some beginner problems which need vectors?

vectors are especially used when number of elements to be stored is unknown unlike array.

vectors can be used for representing a graph in adjacency list (which is the basic to solve all kinds of graph problems which includes dfs, bfs etc.)

1 Like

You can also look into how to use them for hashing when multiple keys fall into the same bucket.

Think of vectors as an array which is of the dynamic type. Basically, the questions from vectors are never asked in particular. But they are used in almost all the problems.

  1. Graphs/Trees Representation - Adjacency list
  2. Dynamic array, where size of array is not known.
  3. Various inbuilt functions, like sort, lower_bound,upper_bound, size, binary_search,pop and many other are easily implementable through vectors.
  4. Used as hashing, initializing the vector with some specific value and used as hashing.
  5. Segment tree can also be built using vectors.

As of now, I remember these topics only. Specific problems to learn vectors other than this instead of array start using vectors and then you’ll know the beauty of vectors over arrays.

Hope this helps!

1 Like