Why do random access iterator's arithmetic operators accept / return int and not size_t?

后端 未结 2 481
萌比男神i
萌比男神i 2021-01-18 08:41

Since most operations on std::vector require / return size_t - that\'s the type I use for indexing. But now I\'ve enabled all compiler warnings to

2条回答
  •  清酒与你
    2021-01-18 09:13

    It allows for things like it += index; where index can be both positive or negative (according to some logic).

    Comparing with the following:

    if (some_condition)
        it += index;
    else
        it -= index;
    

    Which would be needed if we could only pass unsigned values.

提交回复
热议问题