函数输入问题:
1 带&和不带& (参数本身还是拷贝一份参数)
2 函数输入指针
#include <iostream> using namespace std; int main( ) { void swap(int *p1,int *p2); //函数声明 int *pointer_1,*pointer_2,a,b; //定义指针变量pointer_1,pointer_2,整型变量a,b cin>>a>>b; pointer_1=&a; //使pointer_1指向a pointer_2=&b; //使pointer_2指向b if(a<b) swap(pointer_1,pointer_2); //如果a<b,使*pointer_1和*pointer_2互换 cout<<"max="<<a<<" min="<<b<<endl; //a已是大数,b是小数 return 0; } void swap(int *p1,int *p2) //函数的作用是将*p1的值与*p2的值交换 { int temp; temp=*p1; *p1=*p2; *p2=temp; }
函数对比
#include <iostream> #include <stdlib.h> using namespace std; void swap1(int p1, int p2); void swap2(int *p1, int *p2); void swap3(int &p1, int &p2); void swap1(int p1, int p2) //函数的作用是将p1的值与p2的值交换 { int temp; temp = p1; p1 = p2; p2 = temp; } void swap2(int *p1, int *p2) //函数的作用是将*p1的值与*p2的值交换 { int temp; temp = *p1; *p1 = *p2; *p2 = temp; } void swap3(int &p1, int &p2) //函数的作用是将p1的值与p2的值交换 { int temp; temp = p1; p1 = p2; p2 = temp; } int main() { int *pointer_1, *pointer_2; //定义指针变量pointer_1,pointer_2,整型变量a,b int a, b; a = 5; b = 6; swap1(a, b); cout << "a=" << a << " b=" << b << endl; // 原值不变 a=5 b=6 a = 5; b = 6; swap2(&a, &b); cout << "a=" << a << " b=" << b << endl; // 原值改变 a=6 b=5 a = 5; b = 6; swap3(a, b); cout << "a=" << a << " b=" << b << endl; // 原值改变 a=6 b=5 a = 5; b = 6; pointer_1 = &a; //使pointer_1指向a pointer_2 = &b; //使pointer_2指向b cout << "pointer_1=" << pointer_1 << " pointer_2=" << pointer_2 << endl; // 指针内存地址 pointer_1=000AABBD50 pointer_2=000AABBD55 cout << "*pointer_1=" << *pointer_1 << " *pointer_2=" << *pointer_2 << endl; // 赋值 *pointer_1=5 *pointer_2=6 *pointer_1 = 5; *pointer_2 = 6; swap1(*pointer_1, *pointer_2); cout << "pointer_1=" << pointer_1 << " pointer_2=" << pointer_2<< endl; // 指针内存地址 pointer_1=000AABBD50 pointer_2=000AABBD55 cout << "*pointer_1=" << *pointer_1 << " *pointer_2=" << *pointer_2 << endl; // 原值不变 *pointer_1=5 *pointer_2=6 *pointer_1 = 5; *pointer_2 = 6; swap2(pointer_1, pointer_2); cout << "pointer_1=" << pointer_1 << " pointer_2=" << pointer_2 << endl; // 指针内存地址 pointer_1=000AABBD50 pointer_2=000AABBD55 cout << "*pointer_1=" << *pointer_1 << " *pointer_2=" << *pointer_2 << endl; // 原值改变 *pointer_1=6 *pointer_2=5 *pointer_1 = 5; *pointer_2 = 6; swap3(*pointer_1, *pointer_2); cout << "pointer_1=" << pointer_1 << " pointer_2=" << pointer_2 << endl; // 指针内存地址 pointer_1=000AABBD50 pointer_2=000AABBD55 cout << "*pointer_1=" << *pointer_1 << " *pointer_2=" << *pointer_2 << endl; // 原值改变 *pointer_1=6 *pointer_2=5 system("pause"); return 0; }
3 函数输入数组
C++中数组作为函数参数是传址。
#include <iostream> using namespace std; // 函数声明 double getAverage(int arr[], int size); int main () { // 带有 5 个元素的整型数组 int arr[5] = {1000, 2, 3, 17, 50}; double avg; // 传递一个指向数组的指针作为参数 avg = getAverage( arr, 5 ) ; // 输出返回值 cout << "平均值是:" << avg << endl; return 0; }
4输入图像当参数
为了避免函数参数传递时出现复制情况,函数的形参多采用传递reference,如cv::Mat &image,传递输入图像的引用,不过这样函数可能会对输入图像进行修改,并反映到输出结果上;如果想避免修改输入图像,则函数形参可传递const reference,这样输入图像不会被修改,同时可以创建一个输出图像Mat,将函数处理的结果保存到输出图像Mat中,例如:void colorReduce4(const cv::Mat &image, cv::Mat &result,int div = 64)。
解决方法1:去掉传入参数的引用符
解决方法2:将传入参数copy一份再传入findContours函数