array of vectors or vector of arrays? - c++

I'm new to C++ STL, and I'm having trouble comprehending the graph representation.
vector<int> adj[N];
So does this create an array of type vector or does this create a vector of arrays? The BFS code seems to traverse through a list of values present at each instance of adj[i], and hence it seems works like an array of vectors.
Syntax for creating a vector is:
vector<int> F;
which would effectively create a single dimensional vector F.
What is the difference between
vector< vector<int> > N;
and
vector<int> F[N]

So does this (vector<int> adj[N];) create an array of type vector or does this create a
vector of arrays?
It creates array of vectors
What is the difference between
vector< vector<int> > N;
and
vector<int> F[N]
In the first case you are creating a dynamic array of dynamic arrays (vector of vectors). The size of each vector could be changed at the run-time and all objects will be allocated on the heap.
In the second case you are creating a fixed-size array of vectors. You have to define N at compile-time, and all vectors will be placed on the stack†, however, each vector will allocate elements on the heap.
I'd always prefer vector of vectors case (or the matrix, if you could use third-party libraries), or std::array of std::arrays in case of compile-time sizes.
I'm new to C++ STL, and I'm having trouble comprehending the graph
representation.
You may also represent graph as a std::unordered_map<vertex_type,std::unordered_set<vertex_type>>, where vertex_type is the type of vertex (int in your case). This approach could be used in order to reduce memory usage when the number of edges isn't huge.
†: To be precise - not always on stack - it may be a part of a complex object on the heap. Moreover, C++ standard does not define any requirements for stack or heap, it provides only requirements for storage duration, such as automatic, static, thread or dynamic.

Short Answer:
It's an array of vector <int>s.
Long Answer:
When reading a declaration such as
vector<int> adj[N];
The compiler uses a method known as the "spiral-" or "clockwise-rule" in order to interpret what it means. The idea behind the spiral rule is that you start at the variable name, and move outwards in a clockwise spiral in order to figure out what type of variable it is. For example:
char* str [10];
Can be interpreted like this:
____
| |
char* str [10];
|_________|
Making str an array of 10 char*s.
Therefore, vector<int> adj[N]; is an array of vectors rather than a vector of arrays
Practice makes perfect:
1: What does int * foo [ ]; mean?
Answer:
"foo" is an array of pointers to integers
2: What does int * (*foo [ ] )(); mean?
Answer:
"foo" is an array of pointers to functions returning pointers to integers
3: What does int * (*(*foo [ ] )())(); mean?
Answer:
"foo" is an array of pointers to functions returning pointers to functions returning pointers to integers

vector<int> arr[N];
It displays an array of vector, each array[i] would have a vector stored in it that can traverse through many values. It is like a an Array of Linked List where the heads are only stored in array[i] positions.
vector<vector<int> > N vs vector<int> F[N]
The difference between 2D Vector and an Array of Vector is that 2D Vectors can span in size while array of vectors have their dimension fixed for the array size.

Under the hood a vector still uses array, it is implementation specific but is safe to think that:
vector<int>
internally creates an int[].
What vectors gives you is that it abstract from you the part where if you want to re-size you don't have to re-allocate manually etc, it does that for you (plus much more of course).
When you do: vector<vector<int>> you are going to create a vector of vectors, meaning a 2D matrix. You can nest it as much as you want.
Vector takes in a type T and allocates an array of that type. so if you pass vector as type T, it will effectively do what you did in your first line, an array of vector<int>.
Hope it makes sense

Related

Initializing an array of vector

I am trying to initialize an array of a vector of ints.
This is my code:
vector<int> *vec[] = new vector<int>[n+1];
I get the following compilation error:
initialization with '{...}' expected for aggregate object
What's wrong with this ?
If you need an array (in the broad sense) of elements of type vector<int>, I advise you to use:
std::vector<std::vector<int>> vec(n+1);
vec will be a vector of vectors. The number of vectors will be n+1 like it seems you wanted. std::vector will manage the memory for you, so there's no need for new/delete.
In C++ we also have std::array, but it looks like the number of elements in vec is dynamically dependant on n, which makes a topmost std::vector the proper fit.
There are many advantages using C++ std::vector/std::array over C style arrays. See e.g. the answers here: std::vector versus std::array in C++.
If you must use a C style topmost array, see the other answer.
The problem is that you're trying to initialize an array of pointers to vector with a "pointer to a vector".
To solve this you can either remove the [] from the left hand side of the declaration or not mix std::vector and raw pointers.
//--------------v----------------------->removed [] from here
vector<int> *vec = new vector<int>[n+1];

What is the difference in the vector<int> vec[n] and vector<vector<int>> vec in C++?

I just started learning graphs data structure and came through the adjacency matrix and adjacency lists.
The Adjacency list use vector<int> vec[n], where n is the number of vertices.
But I think that the working of vector<int> vec[n] i.e so-called adjacency list is same as the vector of vectors...i.e vector<vector<int>> vec.
Please help me out with this!
Let's start simple:
int arr[n];
std::vector<int> vec;
arr is a C array of ints. This data type is very bare bone. You can't easily get its size and lacks all of the modern OOP features. It has a lot of other problems e.g.:
in most contexts it decays to pointer
the return type cannot be array
you can't have array type as function parameter - what looks like a C array type is in fact a pointer type
vec is a proper C++ container representing a dynamic vector. You can access its size, you can grow it, shrink it etc.
Don't use C arrays. Use standard containers. std::vector should be the first choice. If the size is known at compile time and it doesn't change use std::array instead.
vector<int> vec[n]
Is a freakish Frankenstein's monster. It's a C array of vectors of ints.
is same as the vector of vectors...i.e vector<vector<int>> vec
Close. The C++ equivalent would be std::array<std::vector<int>, n>.
Note: there are some compilers (most notably gcc and clang) that accept n as a non constant size for a C array (the so called Variable Length Arrays), but that isn't standard and portable.
Both are not equivalent
std::vector<int> vec[n];
declares an fixed size array of size n where n must be known at compile time (or a compiler extension must be available). During the initialization of vec the constructors of all n vectors are called. These vectors are allocated on the stack.
std::vector<std::vector<int>> vec;
creates an empty vector of vectors though. The constructor of std::vector<int> is never called. You need to call
vec.resize(n);
to change the size of vec to n calling the constructors of std::vector<int> n times. In contrast to std::vector<int> vec[n]; n does not need to be a compile time constant with this approach. Furthermore you can apply other operations on a std::vector<std::vector<int>> changing the number of elements further.
In the second version the std::vector<int> objects are allocated on the heap. (The elements of the std::vector<int>s are stored different locations on the heap.)
In this declaration
std::vector<int> vec[n];
there is declared an array. You can not resize the array for example removing or adding new elements.
On the other hand, using this declaration
std::vector<std::vector<int>> vec( n );
you can resize the vector removing or adding new elements.
Another problem is that arrays do not have the assignment operator.
Also arrays do not have comparison operators.
vector<int> vec[n];
is an array (a so called C-Style array) of size n with elements of type vector<int>
vec can't grow / shrink in size, however it's elements can (.resize(), .push_back(), .pop_back())
vector<vector<int>> vec;
is a two dimensional vector of type int
vec can grow / shrink to any size, and it's element can do so as well.
which one is better? For your case the vector<vector<int>> probably, but it really depends on the use, C-Style arrays require you know the size before, but can be more optimized as the memory will be on the stack. For example, if you know the size of the matrix beforehand, you can use a pure 2d C-Style array:
int matrix[n][m];
With C-Style arrays you loose the size when passed to a function, so std::array is preferred here, they are as optimized and come with utilities such as .size(), .back() etc:
std::array<std::array<int, m>, n> matrix;

How do I interpret multidimensional vectors?

I don't understand the syntax of a multidimensional vector.
Multidimensional arrays are pretty straight forward.
foo[3][5];
That's 5 elements with 3 elements each.
Vectors are not so much straight forward.
vector<vector<int>> foo(n);
I read this as for every foo, the "type" is vector. How does using a vector for another vector's type make it bidimensional? And also, is vector<int> still the same as a vector with an identifier, a vector with empty ints?
You've got the understanding of int foo[3][5]; correct: it's an outer array of three inner arrays, each one of which contains 5 ints.
Oversimplfying a little for the sake of this discussion, but if you consider arrays and vectors as interchangeable vector<vector<int>> foo; isn't really any different.
It's an outer vector, each member of which is an inner vector containing ints.
So thinking in terms of matrices, outer array/vector is the column, inner array/vector is the row. It's really not that different. And that row / column nature is what makes it two dimensional.
No arguments that under the hood the implementations are extremely different, but when you're using them, they will behave very similar to one another.
foo isn't really a "multidimensional" array, it's an array whose elements are also arrays.
With
int foo[3][5];
foo is an array with three elements, each of which is an array with five elements.
That is, foo[1] is an array of five ints (its type is int[5]).
A vector of vectors works like an array of arrays, except their sizes are not fixed.
vector<vector<int>> foo(n);
creates a vector with n elements, each of which is an empty vector (because vectors are empty by default).
vector<vector<int>> bar(3, vector<int>(5));
would create a vector with three vector<int> elements, and bar[1] would be a vector with five int elements.

Pointer to Array holding two Vectors

I am having something i can't seem to figure out myself again. I don't know how to call this problem.
vector<int> *integerVectors[2] = { new vector<int>, new vector<int>};
(*integerVectors)[0].push_back(1);
(*integerVectors)[1].push_back(1);
When i run this i get an unhandled exeption. What I want is an Array with 2 indexes and each of them holding a vector.
EDIT: The Problem seems to appear when i start pushing back.
this syntax:
MyType *var[size];
creates an array of pointers to MyType of size size, which means that this:
vector<int> *integerVectors[2];
produces a size 2 array of pointers to integer vectors, a fact which is backed up by your ability to initialize integerVectors with an initializer-list of pointers to integer vectors produced by calls to new.
this:
(*integerVectors)
produces a pointer to your first vector pointer. You then call operator[] on it, which offsets the pointer by the size of a vector. But this is no longer a pointer to your array--if you call it with an argument of greater than 0, you'll be referencing an imaginary vector next to the one pointed by your first vector element.
Then you call push_back on the imaginary vector, naturally leading to massive problems at run time.
You either want to offset before dereferencing, as in #Abstraction's suggestion of
integerVectors[i]->push_back(1);
or you want to avoid C-style arrays. You're already using one vector, and nesting them rather than making arrays of them will avoid much confusion of this type in the future, while preserving the correct syntax:
vector<vector<int>*> integerVectors = {new vector<int>, new vector<int>};
integerVectors[1]->push_back(1);
Without the c-style array, your needed syntax is vastly clearer.
Even better, you could just avoid the pointers altogether and use
vector<vector<int>> integerVectors = {vector<int>{}, vector<int>{}};
integerVectors[1].push_back(1);
The first line of which has a few equivalent syntaxes, as pointed out by #NeilKirk:
vector<vector<int>> integerVectors(2);
vector<vector<int>> integerVectors{{},{}};
vector<vector<int>> integerVectors(2, vector<int>{});
vector<vector<int>> integerVectors(2, {});
The curly braces I'm using in initialization are another way to initialize an object, without the possibility of the compiler confusing it for a function in the most vexing parse. (However, you have to be careful with it around things which can be initialized with initializer lists like here, which is why some of these initializations use parentheses, instead) The first syntax default-initializes two vectors, and so does the second, though it explicitly states the same thing. It can also be modified to produce more complex structures: two vectors both with the elements {1,2,3} could be one of the below:
vector<vector<int>> integerVectors = {{1,2,3},{1,2,3}};
vector<vector<int>> integerVectors{{1,2,3},{1,2,3}};
vector<vector<int>> integerVectors(2, vector<int>{1,2,3});
vector<vector<int>> integerVectors(2, {1,2,3});
This line is the problem
(*integerVectors)[1].push_back(1);
Dereferencing *integerVectors gives you the first vector pointer (equivalent of integerVectors[0]. Then you call you call operator[] with 1 as arguent on this pointer which will give reference to vector with address shifted with one vector size forward (equivalent to *(integerVectors[0]+1) ) which is not valid.
The right syntax is
integerVectors[1]->push_back(1);

Query about Properties Vectors in C++

I am kind of a beginner and just came across the concept of vectors in C++. I have a few questions about it
1. Is there a concept of 2-D vectors in C++? if yes, then how do i declare corresponding to a 2-D Matrix a[n][m]? Here, n and m are variables.
2. How are vectors passed as arguments to functions? By default are they passed by reference or by value?
3. Are there any performance benifits of vectors over arrays in C++?
1 - There's no real concept of dimensions per se. But you can create "nested" types. For example:
std::vector <int> intVec;
std::vector < std::vector <int> > doubleIntVec;
Here, intVec can be compared to single-dimension vector, doubleIntVec to double dimension, and so on. The types don't have to be the same, you can do std::vector < std::vector <char> > doubleIntVec for example, which is why "dimension" is not the right term here.
2 - Like any other type, there's no specific treatment of vectors.
3 - Yes, for example if you need resizing them, but you can implement arrays to behave similarly. Other than that the benefit is the standardization, the memory management that comes built in, the additional methods, and the various STL algorithms that can be run on vectors (being it a standard container).
There's no 2-D vectors in C++, to create a matrix, you can use vectors of vectors.
using namespace std;
int m, n;
// ...
vector<vector<int> > v(n);
for (int y = 0; y < n; y++)
v[n].resize(m);
// ...
Computing libraries won't implement them this way, though.
To pass a vector by reference to a function, use:
void function(vector & v);
Omitting the & will result in the vector being copied during the function call.
Vectors have the same performance as C arrays, but are much more practical to use.
No need to manually manage the memory, and the vector size is always accessible.
You also have automatic copy and guarantees on contiguity of values (raw data can be accessed by vector::data()
vector in C++ is just a sequence container. So, it's possible using it to hold a 2D array.
Using std::vector <std::vector<int>>
It depends on the purpose.
Not perfomance wise, but unlike array, std::vector is growable.