M
M
Muriam2019-03-02 20:01:15
C++ / C#
Muriam, 2019-03-02 20:01:15

How to count the number of elements in an array?

something similar to

#include <stdio.h>
#include <iostream>
#include cstddef 
 
using namespace std;
  
int main()
{
 
    int mas[] = { 1, 2, 3 };
    cout << sizeof(mas) << endl;

    return 0;
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vasily Melnikov, 2019-03-02
@Muriam

In such a variant.
sizeof (mas) / sizeof(mas[0])
But don't do that

V
Vitaly, 2019-03-02
@vt4a2h

std::size(mas)
or But is better to use in general. It has a size method.
std::array

V
Vasily Pivovarov, 2019-03-02
@livevasiliy

You can stop like this a little under the 11th standard
#include < array > // remove spaces
using namespace std;
array i_arr;
cout << "i_arr size: " << i_arr.size() << endl

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question