- vector, push_back does not name a type
函数外只能放变量(类)的定义和声明,push_back is a statement, must put into a function.
- 内置类型在函数内默认初始化结果可能不为0
- for(init;condition;increment){statements} 控制流中首先init会被执行一次,随后判断condition是否满足条件(注意:不是初始化后马上运行一次statements,若初始化后就不满足condition,直接跳出for循环。)若满足条件,则执行{}内语句,执行结束后执行increment,并再次判断condition条件是否满足,若满足条件继续执行{}内语句,执行结束后执行increment,如此循环;若不满足条件,则跳出for循环。
- 在嵌套循环中,break 语句只会中断其所在位置的循环,不是跳出所有循环
- vector 对于一维vector,可以直接默认初始化,直接push_back;
vector<int> test;
test.push_back(0)
对于二维vector,如果默认初始化,默认初始化为空,不能使用坐标索引,需要确定行的维度后才能使用坐标索引向行中push_back;
vector<vector<int>> test;
test[0].push_back(0); //错误!对于二维vector,如果默认初始化,默认初始化为空,仍然不能使用坐标索引,至少需要确定行的维度,随后可以向列中push_back;
test.resize(4);
test[0].push_back(0);
另外,vector<vector> 二维vector不是矩阵,所以每一行的列数都可以自定义,不同行的列数不必相等。
- 类中方法以外的地方不允许调用构造函数进行初始化
class test{
vector<float> test1{2,0.2,0.5,0.5,1,0.5,0.5,1}; // OK
vector<float> test2(30,1.0); //wrong, error: expected identifier before numeric constant
};
- 当函数含有默认形参时,如果已经在声明中写明默认形参的内容,那么在定义时就不能再在相应的形参位置写出值。
例如:
声明中:
void test(const std::string& defaultstring="yly");
定义中:
void test(const std::string& defaultstring="yly"){ //错误: error: default argument given for parameter
....................................
}
void test(const std::string& defaultstring /*="yly" */){ //正确
....................................
}
- opencv向vector中push_back要在图像上使用.clone()方法