Kim
Kim

Reputation: 23

I can't understand how to use size_t in C++

When I have to use size_t, can I use such expressions?

/* size_ is size_t type and has some value*/

and I want to change the value like ++size_; or --size_;

And I also want to make array using size_t like

array[size_]=something;

Are they valid?

Upvotes: 2

Views: 379

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

Yes, you can use all of them because size_t is an unsigned integer type.

Example:

#include <iostream>

int main(void) {
    size_t size_ = 0;
    int array[10] = {0};
    int something = 42;

    std::cout << "initial: " << size_ << std::endl;

    ++size_;

    std::cout << "incremented: " << size_ << std::endl;

    --size_;

    std::cout << "decremented: " << size_ << std::endl;

    array[size_]=something;

    std::cout << "array: " << array[0] << std::endl;

    return 0;
}

Upvotes: 2

Related Questions