Teaching Newbies since 2014

kauress

  • Home
  • Javascript ❤
    • JavaScript
    • Node.js
  • WebDev Tuts
  • screencasts
  • Resources
  • VR & AR
  • Contact
  • Github
  • Twitter
  • YouTube
  • RSS
  • C++
You are here: Home / C++ / Vectors in C++

January 5, 2020 by: Kauress

Vectors in C++

Vectors are containers containing values of the same data type. I like to think of them like arrays. Vectors can resize automatically when an element is inserted or deleted . Since C++ is pretty close to hardware, vector elements are placed in adjacent storage so that they can travel across. Like arrays in JS, vector have methods such as insert( ), pop_back( ), erase( ) etc. An example of a 1 dimension vector is:
    vector<int> row1 = {3, 4, 5};
An example of a 2 dimensional vector is:
    vector<vector<int>> row2 {{1,2}, {3,4}};
To use vectors include them in your main program like so:
#include <vector>
using namespace std;
Declare and initialize a vector called “myvector”:
vector<int> myvector = {1,2,3};
Access and print the 2nd element in the vector called “myvector” above:
cout << myvector[1];
An example program:
#include <iostream>
#include <vector>
using namespace std;

int main() {
    // declaring and initializing vectors in 1 go
    vector<int> v2 = {3, 4, 5};
   vector<int> myvector = {1,2,3};
    cout << v2[1] << "\n";
    cout << myvector[1] << "\n";
}
/*
This prints out:
4
2
As remember arrays start at index 0 so if we want to access the 2nd element then we have to access the 1st index in the vector
*/
Now access and print out an element from a 2D vector:
int main() {
    vector<vector<int>> b = {{1, 1, 2, 13},
                             {2, 1, 2, 30},
                             {3, 1, 2, 300}};
    cout << b[0][3] <<"\n";
//prints out the last element in row 0 which is at index 3 and is the number 13
}
Vector sizes: Just like we can find out how many elements are in an array with the .length property. The same way we use the .size( ) method to find out how big an array is. Size of a 1D vector:
#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> myvector = {0, 1, 2, 3,5,6,7};
    
    // Print the length of vector a to the console.
    cout << myvector.size() << "\n";
}
Size of a 2D vector: To do this access the row that you want and call the .size( ) method on it. For example:
#include <iostream>
#include <vector>
using namespace std;

int main() {

    vector<vector<int>> b = {{1, 2, 3,4, 5,6,7,8,9,10},
                             {2, 1, 2, 3},
                             {3}};
    // Print the length of an inner vector of b here.
    cout << b[0].size() << "\n";
// access row 0 (which is the 1st row) 
// prints  out 10 as there are 10 elements

   cout << b[2].size() << "\n";
// prints out the number of elements in the last row which is 1
}
String data type in C++
For loops in C++

Comments

  1. Alphonso Kittredge says

    April 5, 2020 at 7:39 pm

    Saved as a favorite!, I love your blog!

    Reply
  2. Harland Zullinger says

    April 8, 2020 at 2:26 pm

    tҺe website іѕ really good, I like your web site!

    Reply

Leave a Reply to Harland Zullinger Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2021 ·Kauress