简体   繁体   中英

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?

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM