Hello,
Im tring to write a function which get filename.txt and insert the file values into a 1D or 2D vector. it does work for the 1D vectors, but i get an error for the 2D.
my code:
The memory layout of a vector of vectors is the same as an array of pointers to individually-allocated arrays. Given &L[0][0], you can only iterate until &L[0][n-1]. Incrementing that pointer by one drops you off into oblivion and does not give you &L[1][0] as it does with 2D arrays.
And, yes, pointers complicated this task unnecessarily.
Pointers are dangerous and lead to access violations. So I advise that you only use pointers when you *have to* use pointers.
In a vector of vectors you can not use pointer arithmetic outside of any single vector. So if you take the address of the first element of a vector &L[0][0] you can only address elements L[0][0] to L[0][n - 1].
You can NOT use pointer arithmetic to treat all the elements as a contiguous lump of memory.
Pointers are dangerous and lead to access violations. So I advise that you only use pointers when you *have to* use pointers.
I would agree that you shouldn't overuse pointers, but don't go out of your way to avoid them either. There are cases where pointers are not necessarily required, but they simplify things a lot. Instead of shying away from pointers, I would recommend to new users that they focus a lot on pointers until they understand when and how it gives an advantage.
No, it'd be faster, if anything. Passing by value is slower since the entire vector would have to be copied. By using a pointer, or more preferably, a reference, the actual argument is not copied, but pointed to or referred to and no copy operation takes place.