1 #include"stdafx.h"
2 #include"string"
3 #include<iostream>
4 #include<vector>
5
6 #include<string.h>
7 using namespace std;
8
9 int add_color(vector<string> v1, vector<int> v2, string s)
10 {
11 cout << v1.size() << " " << v2.size() << endl;
12 //cout << "test" << endl;
13 if (v1.size() == 0)
14 {
15
16 v1.push_back(s);
17 v2.push_back(1);
18 // cout << v1[0];
19 }
20
21 else
22 {
23 int i = 0;
24 for (; i<v1.size();)
25 {
26 if (!strcmp(v1[i].c_str(), s.c_str()))
27 {
28 v2[i]++;
29 //i++;
30 break;
31 }
32 else
33 {
34 i++;
35 }
36
37 }
38 if (i == v1.size())
39 {
40 v1.push_back(s);
41 v2.push_back(1);
42 }
43 }
44
45 cout << v1.size() << " " << v2.size() << endl;
46
47 return 0;
48 }
49
50 int find_max(vector<string> v1, vector<int> v2)
51 {
52 int max;
53 max = 0;
54 for (int i = 0; i<v2.size() - 1; i++)
55 {
56 for (int j = i + 1; j<v2.size(); j++)
57 {
58 if (v2[i] <= v2[j])
59 {
60 max = j;
61 }
62 }
63 }
64 return max;
65 }
66
67 int main(void)
68 {
69 int N;
70 cout << "input N:";
71 cin >> N;
72
73 vector<string> v1;
74 vector<int> v2;
75 string s;
76
77 while (N)
78 {
79 //string s;
80 cin >> s;
81 add_color(v1, v2, s);
82 //add_color(v1, v2, s);
83 cout << v1.size() << endl;
84 N--;
85 }
86
87 cout << endl;
88 int max = find_max(v1, v2);
89 cout << v1[max] << endl;
90
91 }
在調用add_color的時候,無法正常執行下去,將函數體直接寫在循環裡就沒有問題
問了別人,是參數調用的問題
C++參數調用有三種,值傳遞,指針傳遞,引用傳遞
值傳遞實際上只是傳遞的實際參數的一個副本,並不是原參數,這樣的原因是可以實現對實際參數的保護;
指針傳遞,則傳遞的是地址;
引用傳遞,對形參的操作等同於對實參的操作,即傳遞的不會是實參的副本,而是實參;
具體的例子可以看一下http://www.cnblogs.com/Romi/archive/2012/08/09/2630014.html