Search
 
SCRIPT & CODE EXAMPLE
 

CPP

is there anything like vectorx[100]

Whenever we declare a vector like:

	vector<int> v; 
Here, we do not specify the size of our vector. 
Whenever we just want to add an element we just push it into the vector
using the  v.push _ back(val)  function (where val is the value you want to 
push).

Suppose you haven't added any elements into the vector. 
Now, if you go on access any element position like  v[0]  or  v[7]  like one 
can when he declares an array, you will get a runtime error. Why?
Because vectors are dynamic in nature. Since, you haven't declared any size or 
pushed any element it's size is zero. So no element exists right now.

But if you declare it using:

vector<int> v(N); 
The compiler would there only create an dynamic array of
size N with all values initialized to zero. Now, you can randomly 
access elements but only from  v[0]  to  v[N−1] . But, if you want to 
add more than N elements then you can anytime use the push _ back()  
function to add elements.

Now, coming to

vector<int> v[N]; 
It creates a static array of N vectors of the first type i.e.  v[0]  
is now a whole new and different vector than v[1] which is totally different 
than  v[2]  and so on. If you want to push a value into the 1st vector 
that is  v[0] , you will do it by  v[0].push _ back(val) . 
To access that element you will call it by  v[0][0] (kind of a 2d matrix, 
isn't it?!). So, basically it is an array of initially empty vectors to
which you can add elements. These are very useful while you want to implement 
graphs where each node has it's own vector and the nodes to which it is joined 
are pushed into that particular node's vector.
Comment

PREVIOUS NEXT
Code Example
Cpp :: permutation and combination program in c++ 
Cpp :: how to input a file path in c++ 
Cpp :: Chef and Races codechef solution in c++ 
Cpp :: tic tac toe in cpp 
Cpp :: simple interest rate 
Cpp :: typeid to string c++ 
Cpp :: converting a for loop to a while loop C++ 
Cpp :: tutti i tipi di equazioni trigonometriche 
Cpp :: Call db.close() on Button_Click (QT/C++) 
Cpp :: declare a structer in cpp 
Cpp :: how to get the numbers in a vector c++ sfml 
Cpp :: c++ get last element in array 
Cpp :: random c++ 
Cpp :: how to code a segment tree in c++ 
Cpp :: how to create a structure c++ 
Cpp :: how to change the type of something in c++ 
Cpp :: how to make an enum in c++ 
Cpp :: c++ environment setup 
Cpp :: how to make a c++ iostream program restart when finished 
C :: C bitwise integer absolute value 
C :: pygame detect click 
C :: div en langage c 
C :: how to prevent user from entering char when needing int in c 
C :: see if two strings are equal in C 
C :: add 2 numbers in c 
C :: A binary tree whose every node has either zero or two children is called 
C :: c assign pointer to struct 
C :: typedef pointer 
C :: multiplication table in c using array 
C :: remove axis numpy array 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =