-2

In C++

If there exists an object a that is an array, vector<int> a[], how do I obtain the size of a? Or, at least, be able to traverse through the array?

I want to be able to traverse through all the elements in this supposedly 2D matrix. Not sure how to implement the for loop for this.

for(int i = 0; i < ???; i++){
}

a.size() does not work here.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • What coding language? – ravenspoint Jul 07 '21 at 18:31
  • 1
    vector a[], This is not C++ – ravenspoint Jul 07 '21 at 18:32
  • C++ @ravenspoint – Dhanyaa Bharadwaj Jul 07 '21 at 18:32
  • I've edited the question, sorry for the confusion @ravenspoint – Dhanyaa Bharadwaj Jul 07 '21 at 18:33
  • To define a 2D matrix of integers you need to write std::vector< std::vector< int > > – ravenspoint Jul 07 '21 at 18:33
  • Its not a 2D matrix, it only appears like one. My doubt is if the question already makes use of an array of vectors, how to go about it? – Dhanyaa Bharadwaj Jul 07 '21 at 18:36
  • Very bad idea mix using vectors and arrays. Everything will become horribly confused. Arrays are an ancient c thing which, among other snags, do not keep track of how long they are. As posed, your question's only answer is: you can't. – ravenspoint Jul 07 '21 at 18:37
  • @DhanyaaBharadwaj In what context do you need to do this? Is the loop in the same code that declares the array? If so, then what you are asking for is possible. Or, is the loop located elsewhere? If you pass an array to a function, all size information is lost, unless it is passed explicitly in a separate function parameter. Can you use `std::array` instead? That has a `size()` member – Remy Lebeau Jul 07 '21 at 18:40
  • True, but GFG still uses it, and that is why the issue. At least how to run a for loop through it? Any idea? – Dhanyaa Bharadwaj Jul 07 '21 at 18:41
  • yes, the array is passed to a function. I'm stuck at being able to traverse through it @RemyLebeau – Dhanyaa Bharadwaj Jul 07 '21 at 18:43
  • Does this answer your question? [How do I determine the size of my array in C?](https://stackoverflow.com/questions/37538/how-do-i-determine-the-size-of-my-array-in-c) – Retired Ninja Jul 07 '21 at 18:45
  • no @RetiredNinja, that question is for a integer array. I'm talking about array of vectors – Dhanyaa Bharadwaj Jul 07 '21 at 18:47
  • You could also use a template, but only when you have an actual array, not if it has decayed to a pointer because you passed it to a function. https://stackoverflow.com/questions/4073276/use-templates-to-get-an-arrays-size-and-end-address – Retired Ninja Jul 07 '21 at 18:48
  • 1
    @DhanyaaBharadwaj Array are arrays. int, double, vector, SomeReallyBigClass, all the same. – Retired Ninja Jul 07 '21 at 18:48
  • @RetiredNinja i'm unable to use sizeof() operator (according to the linked post, and ofc the common way of doing it) and obtain the size here. Please let me know your approach – Dhanyaa Bharadwaj Jul 07 '21 at 18:53
  • 1
    @DhanyaaBharadwaj My approach in C++ is to use `std::vector` or `std::array`. Both are superior to regular arrays. – Retired Ninja Jul 07 '21 at 19:03
  • @DhanyaaBharadwaj several of the answers to the [question linked by RetiredNinja](https://stackoverflow.com/questions/37538/) tell you how to solve this issue - if all you have is a pointer into the array, then you have to pass the size as a separate parameter. – Remy Lebeau Jul 07 '21 at 19:12
  • Use `std::array` instead of C-style arrays. Then `.size()` works. In fact, just forget that C-style arrays exist in the language. – Jesper Juhl Jul 18 '23 at 10:42

3 Answers3

3

It depends.

If the loop is in the same scope that declares the array, then it is possible to determine the array's size directly, eg:

{
    ...

    vector<int> a[SomeSize];

    for(size_t i = 0; i < SomeSize; ++i){ // OK!
        ...
    }

    for(size_t i = 0; i < sizeof(a) / sizeof(*a); ++i){ // OK!
        ...
    }

    for(size_t i = 0; i < std::size(a); ++i){ // OK!
        ...
    }

    for(auto iter = std::begin(a); iter != std::end(a); ++iter){ // OK!
        ...
    }

    for(auto &elem : a){ // OK!
        ...
    }

    ...
}

On the other hand, if the array is passed to a function, then it is possible to determine the array's size only if the array is passed by reference, or if the size is passed explicitly as a separate parameter, eg:

void func1(vector<int> a[]) // same as 'vector<int>* a'
{
    for(size_t i = 0; i < sizeof(a) / sizeof(*a); ++i){ // NOPE, can't work!
        ...
    }

    for(size_t i = 0; i < std::size(a); ++i){ // NOPE, can't work!
        ...
    }

    for(auto iter = std::begin(a); iter != std::end(a); ++iter){ // NOPE, can't work!
        ...
    }

    for(auto &elem : a){ // NOPE, can't work!
        ...
    }
}

void func2(vector<int> *a, int size)
{
    for(size_t i = 0; i < size; ++i){ // OK!
        ...
    }

    for(auto iter = a; iter != a + size; ++iter){ // OK!
        ...
    }

    for(auto iter = std::begin(a); iter != std::end(a); ++iter){ // NOPE, can't work!
        ...
    }

    for(auto &elem : a){ // NOPE, can't work!
        ...
    }
    */
}

template<size_t N>
void func3(vector<int> (&a)[N])
{
    for(size_t i = 0; i < N; ++i){ // OK!
        ...
    }

    for(size_t i = 0; i < sizeof(a) / sizeof(*a); ++i){ // OK!
        ...
    }

    for(size_t i = 0; i < std::size(a); ++i){ // OK!
        ...
    }

    for(auto iter = std::begin(a); iter != std::end(a); ++iter){ // OK!
        ...
    }

    for(auto iter = a; iter != a + N; ++iter){ // OK!
        ...
    }

    for(auto &elem : a){  // OK!
        ...
    }
}
vector<int> a[SomeSize];
func1(a);
func2(a, SomeSize);
func2(a, std:size(a));
func3(a);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

To define a 2D matrix of integers you need to write

std::vector< std::vector< int > > v2d;

To traverse this you need

for( auto& v1d : v2d )
   for( int i : v1d )
ravenspoint
  • 19,093
  • 6
  • 57
  • 103
  • Please clarify for vector a[] , i.e., an array of vector – Dhanyaa Bharadwaj Jul 07 '21 at 18:38
  • 2
    `vector a[]` has no size and will only compile if it is a function parameter. As a function parameter, it is an array that [has decayed to a pointer](https://stackoverflow.com/questions/1461432) and the size information has been lost because a pointer only knows where something is. It knows nothing about how large that something is. – user4581301 Jul 07 '21 at 18:49
  • @user4581301 your comment seems to apply to the OP, not this answer. – ravenspoint Jul 07 '21 at 19:03
  • My comment addresses the comment immediately above. – user4581301 Jul 07 '21 at 19:08
  • @user4581301 If your comment is directed at someone else, you should use the @ addressing feature. – ravenspoint Jul 07 '21 at 19:09
  • My understanding is the owner of the preceding comment automatically gets notified. Point is moot now, though. The issue is well covered by Remy's answer. – user4581301 Jul 07 '21 at 19:11
0

I don't know if it is still helpful. But since you just have to traverse it for gfg problem, you can just access the index positions you need and as long as your sollution is correct you won't go out of bound.

For example if you are given adjecency list, vector<int> adj[] of a graph and number of vertices V, you can just access any index you need and it will be there. You won't need to traverse the entire range. This is only relevent as for the way GFG questions are crafted.