std :: list固定大小 [英] std::list fixed size

查看:385
本文介绍了std :: list固定大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建具有固定元素计数的std::list?

How can I create std::list with a fixed element count?

推荐答案

#include <list>

// list with 5 elements, using default constructor
const size_t fixedListSize(5);
std::list<int> mylist(fixedListSize);  

如果您希望它始终只包含5个元素,则必须将其包装在Facade类中,以防止插入和删除.

If you want it to always have exactly 5 elements you'd have to wrap it in a facade class to prevent insertion and erasure.

如果这确实是您想要的,那么最好使用其他容器代替list,因为如其他答复中所述,您将隐藏list的最有利功能.

If that is indeed what you want, you'd be better off using a different container instead of list, since as noted in other responses you would be hiding the most advantageous features of list.

这篇关于std :: list固定大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆