-1

I am trying to figure out two questions. How do I find the size of the array. I used a site and they showed something like this.

    int myarray[5];
    int main()
    {
        cout<<myarray.size()<<endl;
        return 0;
    }

I know there is a another way but I know you can you size or I think its called length, can anyone help?

I am also trying to use two arrays and find the unique number in them so I did:

int myarray1[5]={2, 3, 4, 5, 6};
int myarray2[3]={2, 3, 4};

int main()
{
    for (int i=0; 0<size.array1; i++){
            for (int j=0; 0<size.array2; j++){

                if array[i]!=array[j]{
                        <<cout i;

                }
            }


    }
}

What do you guys think?

Pete
  • 57,112
  • 28
  • 117
  • 166
Pter Coder
  • 39
  • 4
  • 2
    possible duplicate of [finding size of int array](http://stackoverflow.com/questions/2037736/finding-size-of-int-array) – EdChum May 08 '15 at 08:18
  • I know you can use sizeof but I was told that it is possible to use just size. With sizeof you have to divide it by the size. With just "size" it give the answer straight away, but I dont know how to do it. I have another question too. – Pter Coder May 08 '15 at 08:23
  • *"but I was told that it is possible to use just size"* - whomever told you that was either mistaken, or you're mistaken to think they were talking about inbuilt arrays (`.size()` would work for e.g. C++11 `std::array<>`, which you'd be better off using than inbuilt arrays). – Tony Delroy May 08 '15 at 08:27
  • You should ask one question at a time. – juanchopanza May 08 '15 at 08:47

2 Answers2

1

Arrays are not classes and they do not have member functions. So this statement

cout<<myarray.size()<<endl;

is invalid and the compiler will issue an error.

To get the size of the array you could write either

cout << sizeof( myarray ) / sizeof( *myarray ) <<endl;

Or you can use class std::extent declared in header <type_traits>

For example

#includde <type_traits>

//...

cout << std::extent<decltype( myarray )>::value <<endl;

The other approach is to use standard class std::array declared in header <array> instead of the ordinary array. For example

#include <iostream>
#include <array>

std::array<int, 5> myarray;

int main()
{
    std::cout << myarray.size() << std::endl;

    return 0;
}

If you want to find unique elements in two arrays and the arrays are sorted then you can use standard algorithm std::symmetric_difference declared in header <algorithm>. For example

#include <iostream>
#include <iterator>
#include <algorithm>


int main()
{
    int myarray1[5] = { 2, 3, 4, 5, 6 };
    int myarray2[3] = { 2, 3, 4 };

    for ( int x : myarray1 ) std::cout << x << ' ';
    std::cout << std::endl;

    for ( int x : myarray2 ) std::cout << x << ' ';
    std::cout << std::endl;

    std::set_symmetric_difference( myarray1, myarray1 + 5,
                                   myarray2, myarray2 + 3,
                                   std::ostream_iterator<int>( std::cout, " " ) );
    std::cout << std::endl;                                


    return 0;
}

The program output is

2 3 4 5 6 
2 3 4 
5 6 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

See How do I determine the size of my array in C? for an answer with respect to plain static arrays - it works like it works in C. (-> sizeof(myarray)/sizeof(myarray[0]))

But if you're using C++[11] you probably want to avoid arrays like that entirely and use std::array instead.

#include <iostream>
#include <array>

int main()
{
    std::array<int, 5u> myarray;
    std::cout<< myarray.size() << std::endl; // valid
    return 0;
}

Or the other code:

int main()
{
    std::array<int, 5u> myarray1 = {2, 3, 4, 5, 6};
    std::array<int, 3u> myarray2 = {2, 3, 4};
    for (unsigned i=0; 0 < size.array1; ++i){
        for (unsigned j=0; 0<size.array2; ++j){
            if (array[i]!=array[j]{
                    std::cout << i << " ";
            }
        }
    }
}
Community
  • 1
  • 1
Pixelchemist
  • 24,090
  • 7
  • 47
  • 71