1

i have array declaration

array<int, 5> niz;

Now i need to found maximum in that array and to remove it . How to achieve that with class array?


EDIT
so array size cant be modified, so can i swift two elements in array then?

1
  • 1
    This is impossible. The array size is constant. Commented Nov 5, 2018 at 17:49

2 Answers 2

4

It cannot be done. A std::array has a fixed, compile-time determined number of elements. If you want a container that supports a changing number of elements, you can use for example std::vector.

Sign up to request clarification or add additional context in comments.

3 Comments

Is it possible to change , for example , first and second element of array then?
@AndroidBeginner try it yourself. It is very useful in learning.
@AndroidBegginer You mean to swap them? See std::swap.
0

While you cannot remove an element from an array, you can move (by swapping) the max element to the end of the array and keep a dynamic size.

const unsigned int fixed_size=5;
unsigned int dynamic_size = fixed_size;
std::array<int, fixed_size> myArray;

And when you find the max (will let you implement this part), swap it with the last index within the dynamic range. And decrement the dynamic size.

std::swap( myArray[dynamic_size-1], myArray[max_index] );
--dynamic_size;

And this will iterate over the array, excluding the max element.

for( unsigned int i=0; i<dynamic_size; ++i ) printf( "%u: %d\n", i, myArray[i] );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.