C++ - value of uninitialized vector

后端 未结 5 1527
情歌与酒
情歌与酒 2020-12-03 09:28

I understand from the answer to this question that values of global/static uninitialized int will be 0. The answer to this one says that for vectors, the default construc

5条回答
  •  广开言路
    2020-12-03 10:00

    It is not undefined behaviour, a vector automatically initialises all its elements. You can select a different default if you want.

    The constructor is:

    vector( size_type, T t = T() )

    and for int, the default type (returned by int()) is 0.

    In a local function this:

    int x;

    is not guaranteed to initialise the variable to 0.

    int x = int();

    would do so.

    int x();

    sadly does neither but declares a function.

提交回复
热议问题