-3

I have a complex number array. I want to know the number of elements in that array. For a complex number array, a pair of real and imaginary element will be considered as 1 element.

I have declared my complex array using

std::vector< std::complex<double> > Complex_Data   

I have declared a macro to calculate number of elements in a simple array

#define ELEMENT_COUNT(a) (sizeof(a) / sizeof(*a))  

This macro works for normal array but not complex array. How do I modify this macro or how do I write code to calculated number of elements in a complex array.

The error that I get during compilation is

error: no match for ‘operator*’ (operand type is ‘std::vector<std::complex<double> >’)  
    #define ELEMENT_COUNT(a) (sizeof(a) / sizeof(*a))
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
KharoBangdo
  • 321
  • 5
  • 14
  • 5
    [`std::vector::size()`](https://en.cppreference.com/w/cpp/container/vector/size)? – BoBTFish Aug 20 '19 at 09:34
  • 1
    `std::vector` provides a function `std::vector::size()` – πάντα ῥεῖ Aug 20 '19 at 09:35
  • 2
    "This macro works for normal array" I doubt that, in case you refer to a `std::vector`. Better dont use such ancient stuff when dealing with vectors, it cannot give you the correct answer – 463035818_is_not_an_ai Aug 20 '19 at 09:48
  • A vector is not a dumb array. It knows its size by utilizing the `size()` function. There is no need for macros as you've written to determine the number of elements in a vector or any standard container. – PaulMcKenzie Aug 20 '19 at 11:08

1 Answers1

1

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
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335