c++ primer 顺序容器

标准库定义了三种顺序容器类型:vector、list 和 deque

顺序容器适配器包括 stack、queue 和 priority_queue 类型

使用迭代器时,不要求容器类型相同。容器内的元素类型也可以不相同,只要它们相互兼容,能够将要复制的元素转换为所构建的新容器的元素类型,即可实现复制。

容器元素类型必须满足以下两个约束:
• 元素类型必须支持赋值运算。
• 元素类型的对象必须可以复制。

IO 库类型不支持复制或赋值。因此,不能创建存放 IO 类型对象的容器。

iter->mem (*iter).mem

// check that there are elements before dereferencing an iterator 
// or calling front or back 
if (!ilist.empty()) { 
// val and val2 refer to the same element 
list<int>::reference val = *ilist.begin(); 
list<int>::reference val2 = ilist.front(); 
// last and last2 refer to the same element 
list<int>::reference last = *--ilist.end(); 
list<int>::reference last2 = ilist.back(); }

通常来说,除非找到选择使用其他容器的更好理由,否则 vector 容器都是最佳选择

string 类型提供容器操作意味着可将操纵 vector 对象的程序改写为操纵 string 对象。

string s("Hiya!"); 
string::iterator iter = s.begin(); 
while (iter != s.end()) 
cout << *iter++ << endl; // postfix increment: print old value
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值