c++ 函数调用在进入下一个循环的时候会再次初始化参数,将函数体直接写进去就正常
2015-12-28 12:40
274 查看
#include"stdafx.h" #include"string" #include<iostream> #include<vector> #include<string.h> using namespace std; int add_color(vector<string> v1, vector<int> v2, string s) { cout << v1.size() << " " << v2.size() << endl; //cout << "test" << endl; if (v1.size() == 0) { v1.push_back(s); v2.push_back(1); // cout << v1[0]; } else { int i = 0; for (; i<v1.size();) { if (!strcmp(v1[i].c_str(), s.c_str())) { v2[i]++; //i++; break; } else { i++; } } if (i == v1.size()) { v1.push_back(s); v2.push_back(1); } } cout << v1.size() << " " << v2.size() << endl; return 0; } int find_max(vector<string> v1, vector<int> v2) { int max; max = 0; for (int i = 0; i<v2.size() - 1; i++) { for (int j = i + 1; j<v2.size(); j++) { if (v2[i] <= v2[j]) { max = j; } } } return max; } int main(void) { int N; cout << "input N:"; cin >> N; vector<string> v1; vector<int> v2; string s; while (N) { //string s; cin >> s; add_color(v1, v2, s); //add_color(v1, v2, s); cout << v1.size() << endl; N--; } cout << endl; int max = find_max(v1, v2); cout << v1[max] << endl; }
在调用add_color的时候,无法正常执行下去,将函数体直接写在循环里就没有问题
问了别人,是参数调用的问题
C++参数调用有三种,值传递,指针传递,引用传递
值传递实际上只是传递的实际参数的一个副本,并不是原参数,这样的原因是可以实现对实际参数的保护;
指针传递,则传递的是地址;
引用传递,对形参的操作等同于对实参的操作,即传递的不会是实参的副本,而是实参;
具体的例子可以看一下http://www.cnblogs.com/Romi/archive/2012/08/09/2630014.html
相关文章推荐
- C++ Primer笔记7_STL之关联容器
- Problem B: C语言习题5.14--for循环画三角形
- Problem A: C语言习题5.19--递归方法实现逆序
- C++实现链栈的基本操作
- 浅谈 c++ 与 c 中的动态内存开辟问题
- VC++6.0版本号程序转成VS2010版
- c++ 在main 之前的全局对象
- C++中 #define的用法
- C语言可变参数函数的使用方法讲解
- C语言中可变参数的用法
- C语言还有不定参数
- C\C++各路高手以及操作系统专家请进来杀死这个进程
- 课程设计——c++中读取txt文件
- C++ STL priority_queue
- C语言---类型转换
- GC基本算法及C++GC机制
- vs2008中C++项目编译过程中越来越慢
- const——c与c++不同
- C语言字符串操作总结大全
- 【C++】动态开辟二维数组