If your compiler supports C++ 17 Standard then you can use the general function std::size
declared in the header <iterator>
For example
#include <iostream>
#include <complex>
#include <iterator>
#include <vector>
int main()
{
std::complex<double> a[] = { { 1.1, 1.1 }, { 2.2, 2.2 }, { 3.3, 3.3 }, { 4.4, 4.4 }, { 5.5, 5.5 } };
std::vector<std::complex<double>> v = { { 1.1, 1.1 }, { 2.2, 2.2 }, { 3.3, 3.3 }, { 4.4, 4.4 }, { 5.5, 5.5 } };
std::cout << std::size( a ) << '\n';
std::cout << std::size( v ) << '\n';
}
The program output is
5
5
Otherwise as the class template std::vector
is a standard container then as usual (except the class std::forward_list
) it has a member function size
that returns the number of elements in a vector.
For example
#include <iostream>
#include <complex>
#include <vector>
int main()
{
std::vector<std::complex<double>> v = { { 1.1, 1.1 }, { 2.2, 2.2 }, { 3.3, 3.3 }, { 4.4, 4.4 }, { 5.5, 5.5 } };
std::cout << v.size() << '\n';
}
The program output is
5
Pay attention to that instead of your macro you can use standard class std::extent
declared in the header <type_traits>
to determine the number of elements in an array.
For example
#include <iostream>
#include <complex>
#include <type_traits>
int main()
{
std::complex<double> a[] = { { 1.1, 1.1 }, { 2.2, 2.2 }, { 3.3, 3.3 }, { 4.4, 4.4 }, { 5.5, 5.5 } };
std::cout << std::extent_v<decltype( a )> << '\n';
}
The program output is
5
Or if the compiler does not define the construction std::extent_v
then instead of
std::extent_v<decltype( a )>
you can write
std::extent<decltype( a )>::value