C++ Vectors vs Arrays

Spread the love

#include <vector>

vector<int> v;

for( int i=1; i < 5 ;i++)

#include <iostream>
#include <vector>
using namespace std;

int main() 
    vector<int> g1; 
    for (int i = 1; i <= 5; i++) 
    cout << "Output of begin and end: "; 
    for (auto i = g1.begin(); i != g1.end(); ++i) 
        cout << *i << " "; 

Line 2: #include <vector> // Use of vectors requires this library

Line 7: vector<int> // Vectors are like arrays except they are dynamic and can grow and shrink ( Yes, really ) and have iterators in addition to the regular int i=0;i < array.size(); i++

Line 10: push_back() is how one adds data to the end of a vector

I know this stuff, let me just go to the c++ vectors vs arrays homework part. Really!

C++ Vectors vs Arrays - image of source code showing the differences. Colored text on white background.

C++ Vectors vs Arrays – Vector Manipulation

Focus on the output of the image above.

Output of begin and end: 1 2 3 4 5 // output of regular array movement

Output of cbegin and cend: 1 2 3 4 5 // uses vector iterators

Output of rbegin and rend: 5 4 3 2 1 // vector reverse iterators

The Code that produced that output looks like this

    cout << "Output of begin and end: "; 
    for (auto i = g1.begin(); i != g1.end(); ++i) 
        cout << *i << " "; 
    cout << "\nOutput of cbegin and cend: "; 
    for (auto i = g1.cbegin(); i != g1.cend(); ++i) 
        cout << *i << " "; 
    cout << "\nOutput of rbegin and rend: "; 
    for (auto ir = g1.rbegin(); ir != g1.rend(); ++ir) 
        cout << *ir << " "; 
    cout << "\nOutput of crbegin and crend : "; 
    for (auto ir = g1.crbegin(); ir != g1.crend(); ++ir) 
        cout << *ir << " "; 

C++ Vectors vs Arrays – Usage of Vectors

So Practically speaking, where exactly do we use vectors in c++ vector design and implementation?

The Answer: Pretty well anywhere that we might use a list of things that can grow or shrink.

Think : How about coding a chat room. The number of participants in that chat room will grow and shrink based on all kinds of reasons. 

The messages that those participants put into the chat room will grow or shrink because a number of people are always chatting and a few are frequently deleting their own chat messages and hoping that those messages will also get deleted on everyone else’s chat window. There may be 4 or 400 or even 40,000 different chat rooms. Thats an awful lot of lists of chat rooms.

So, if you think about the sorts of things that humans do online, there are dynamic lists all around you. Think TikTok, Instagram, Facebook and anywhere else that people can network and communicate live. These are the obvious places for vectors or specialized vectors that have even more properties.

C++ Vectors vs Arrays – can’t we use arrays for these things?

The primary reason that arrays are unsuited is because that arrays don’t grow dynamically therefore how would you allocate slots for a chat room when you don’t know how many simultaneous maximum messages might be used? 

If you keep maxing out an array and recreating another one with 10-15% more room, then copying the first array to the second, what happens when the array size needs to shrink ( perhaps because half the room just left and went to another room where they are about to create another problem, ha ha.

Further what happens when in the middle of your copy, half the array participants suddenly leave that room? 

Oops. These are real problems. Everyone writing a chat room or handling dynamic list of objects whether those objects are game assets or sockets or whatever, these real problems that exist in multiplayer or multi-user set ups are real and happening right now as you are reading this.

Vectors can grow and shrink and there is a price to pay for this convenience but the code is easier to maintain and the price/performance question leans in favor of “those vectors that can grow and shrink“. Which is/are all vectors, by the way.

Homework — Pointing to a position in the String Array

  • 1) The last example above is super short. Please memorize
  • 2) From memory rewrite the first code block above with a vector size of 10
  • 3) Compile and test. Then add the second code block to the first
  • 4) Compile, Test and Confirm that the iterators went through the vector

Next Lesson 11, C++ Classes

Go back to the top of this C++ Vectors vs Arrays Document