C++笔记

来源:互联网 发布:xp深度优化工具 编辑:程序博客网 时间:2024/06/06 01:53

前缀操作符,为左值,可引用
后缀操作符,为右值,不可引用,但可const 引用


不能重载的运算符五个   “:: ” “.*”  “ .” “?:”  “sizeof” 
.* 成员指针运算符     


lambda
[capture list] (parameter list) ->return type { function body }


deque在任何位置加元素,迭代器都无效。
但是如果在头尾加元素,引用和指针仍然有效。其它情况下,引用和指针都无效。


deque在除头尾外删除元素,迭代器,引用和指针都无效。
在尾删除元素,除了off-the-end迭代器失效,其它都有效。
在头删除元素,除了off-the-begin迭代器失效,其它都有效。

9.3.6. Container Operations May Invalidate Iterators

After an operation that adds elements to a container
• Iterators, pointers, and references to a vector or string are invalid if the
container was reallocated. If no reallocation happens, indirect references to
elements before the insertion remain valid; those to elements after the insertion
are invalid.
• Iterators, pointers, and references to a deque are invalid if we add elements
anywhere but at the front or back. If we add at the front or back, iterators are
invalidated, but references and pointers to existing elements are not.
• Iterators, pointers, and references (including the off-the-end and the beforethe-
beginning iterators) to a list or forward_list remain valid,

It should not be surprising that when we remove elements from a container,
iterators, pointers, and references to the removed elements are invalidated. After all,
those elements have been destroyed. After we remove an element,
• All other iterators, references, or pointers (including the off-the-end and the
before-the-beginning iterators) to a list or forward_list remain valid.
• All other iterators, references, or pointers to a deque are invalidated if the
removed elements are anywhere but the front or back. If we remove elements at
the back of the deque, the off-the-end iterator is invalidated but other iterators,
references, and pointers are unaffected; they are also unaffected if we remove
from the front.
• All other iterators, references, or pointers to a vector or string remain valid
for elements before the removal point. Note: The off-the-end iterator is always
invalidated when we remove elements.


using PCHAR = char *;

typedef char* PCHAR;

两者都必须为类型,而

#define PCHAR char*

不需要为类型


关联容器插入和删除不会导致其它的iterator失效

但若遍历删除前,使用i++,防止原来的删除的iterator失效


容器适配器

queue: 默认deque,可为list,在前面弹出
priority_queue: 默认为vector,可为deque,随机访问
stack: 默认deque,可为vector和list


queue: empty size front back push_back pop_front
stack: empty size back push_back pop_back
priority: empty size front push_back pop_back


static_cast
必须满足可转换,不可转换成不相关的类型
void指针到其它类型指针
改变通常的标准转换
避免出现可能多种转换的歧义


dynamic_cast
必须为指针,引用。运行时类型检查,不符合返回NULL。
上转下,保证安全
无法使用virtual
若上转下,必须保证含有虚函数
下转上,等价于static_cast,无需含有虚函数


不能把临时对象绑定为非 const 的引用的形参
fun("dasd");
void fun(string &str){} 

关联容器提供

count 

find 

lower_bound

upper_bound

equal_bound

list提供

remove

remove_if

unique

sort

merge

reverse

0 0
原创粉丝点击