size_t

can someone explain the exact use of size_t for ARRAYS?

and after a general explanation I would like a relation to such a code:

1
2
3
4
5
6
7
8
const size_t max_size = 100;
int numbers[max_size] {};
std::cout << "Enter integers('x' to stop)\n";
size_t size = 0;
while(std::cin >> numbers[size++] && size < max_size)
    ; //Empty loop body
std::cin.clear(); //Clear failed state
std::cin.ignore(); //Discard first symbol in stream (our X) 
I read this one before posting that post , I just need a better explanation compared to the code I just posted.
It is usually the largest unsigned type the system has. It is also the type returned by the STL container size() functions, and is a good type to use for the size of containers. That is what you did on line 1 of the code, which is good. Although constexpr is stronger: it's const'ness can't be cast away with const_cast.

It is a member of std, so should be pre-pended with std::

Beginners often write this:

1
2
3
for (int counter = 0; counter < MyContainer.size(); ++counter) {

}


But that generates a warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
So making counter a std::size_t type fixes that.

I would have a different name for the size variable, perhaps counter?
Topic archived. No new replies allowed.