vector 的使用

来源:互联网 发布:中国家暴数据统计 编辑:程序博客网 时间:2024/04/28 13:45

size/resize  以及 capacity/reserve 之间的区别

size 取得 vector 中元素的个数,resize 重新分配空间,并且将分配的空间初始化为0,

capacity : return number of elements for while memory has been allocated

reserve:Allocates elements for vector to ensure a minimum size,preserving its contents if the new size is larger than existing size,buf not initializationvalue for zero.

所以我们只可以使用operator[] 或 at() 去修改实在存在于容器中的元素.

 

1.尽量做到const 正确性

2。尽量使用 != 而不是使用 < 来比较两个迭代器

3。尽量使用前缀的 -- 与 ++,因为后缀的++ 与-- 使用到临时变量

4。避免无谓的求值

5。尽量使用"/n"而不是endl. 使用endl 会迫使输出流刷新其缓冲区

6。尽量使用标准库中的copy 与for_each,而不是自己手写循环

原创粉丝点击