遍历列表
for i in list:
print i
c++11 的特性
1、这种情况是改变i的值并不改变vector,相当于只读类型,浅拷贝
for(int i:vector) //变量类型必须和容器的基本类型相匹配
{
cout<<i;
}
2、这种情况改变i的值会影响原来的vector,相当于深拷贝
for(auto &i:vector //&i 申明变量是引用类型,auto 是自动将vector的类型复制给i
{
i=0;
cout<<vector<<endl;
}
异常捕获
python
try:
num=eval("qq")
except Exception as e:
print(e)
c++
try{
;
}
catch(exception e) //catch(异常类:对象)
{
cout<<e.what()<<endl;
}
抛出异常
python
raise 异常类(msg)
可以自定义自己的异常,如:
class RangeError(Exception):
def __init__(self,msg):
Exception.__init__(self,msg) #重写父类的init 函数
example: raise RangeError("out of range")
c++
#include<stdexcept>
class RangeError:public runtime_error //runtime_error 这个类包括了常用的运行时错误
{
public:
RangeError(const string& msg): runtime_error(msg) {}
};