vector取size问题
缘起
最近迷上了sizeof
,感觉比sizeof()
炫酷多了
1 | int buff[1024]; |
冲突
然后我就习惯性地使用着sizeof
,直到我写了如下代码
1 | int tmp[10] = {1,-2,3,10,-4,7,2,-5}; |
分析
原因分析:
sizeof是对精确的数据类型操作
sizeof(type)
sizeof expression
- Yields the size in bytes of the object representation of type.
- Yields the size in bytes of the object representation of the type of expression, if that expression is evaluated.
而vector是一个类
A std::vector is a class. It’s not the actual data, but a class that manages it.
Use std::vector.size() to get the size of the actual data.
所以应该是
1 | int tmp[10] = {1,-2,3,10,-4,7,2,-5}; |