Difference between vectors in C++

Difference between vectors in C++

Difference between vectors in C++

In C++, the vector is a dynamic array that can grow in size. The three declarations you’ve provided are different ways to declare a vector, and here’s what each one means:

  1. vector<int> result;
  • This declares a vector named result that will store integers (int). Initially, it is empty, and its size will grow dynamically as integers are added to it.

2. vector<int> result[10];

  • This is actually an array of vectors. It declares an array named result with 10 elements, where each element is a vector that can store integers. Each vector in the array can grow independently in size.

3. vector<int> result(nums.size());

  • This declares a vector named result that will store integers, and it is initialized to have a size equal to nums.size(). Assuming nums is another vector, result will have the same number of elements as nums from the start.

In summary:

  • The first declaration is a single vector with a dynamic size.
  • The second is an array of 10 vectors, each with a dynamic size.
  • The third is a single vector whose initial size is set to the size of another vector, nums.

Here’s how you might visualize the differences:


    // A single empty vector that can grow dynamically.
vector<int> result;
    // An array of 10 vectors, each can grow dynamically.
vector<int> result[10];
    // A single vector initialized to the size of 'nums'.
vector<int> result(nums.size());

 

Remember, the second declaration is not a vector but an array of vectors, which is a common point of confusion. The third declaration assumes that nums is a previously defined vector and uses its size to initialize the result vector. I hope this clarifies the differences for you! If you have any more questions or need further explanation, feel free to ask.

Comments

Anonymous said…
good
Anonymous said…

rina opens her phone and logs into the FoodNow app to order lunch.
she browses the list of nearby restaurants, selects a fast-food place, and adds a burger , fries and drink to her cart. when she prococeeeds to checkout she chooses to pay using her debit card.
the app connects to a secure payment gateway to process her payment.abstract
however, the payment fils due to a network issue, tina gets an option to switch to 'cash on delivery' and accepts it. the system confirms her order and sends it to the restaurent.
at the restaurant the chef notices they are out of fries and marks it in the system, rina is notified through the app and chooses to replace the fries with the onion rings .
the chef updates the order and starts preparing it.
once ready, the system assigns a delivery rider, the rider picks up the food and starts the journey to rinas location. meanwhile rina tracks the riders location in real time.
the food arrives after 25 min and she confirms the delivery on the app.
later, rina leaves a 5 star rating for the restaurant and a positive review for the rider , if there had been a serious problem like missing food she could have reported it to the apps customer support team, where an admin would handle refunds or disputes

Popular posts from this blog

12 Best Websites to Practice Coding for Beginners

Using Generic in TypeScript about types in the Code

Usage of Common “Header files” in C programming