一、语句作用域
1.for循环里定义的变量的作用域只在for循环里有用;
2.悬垂else,当多个if对应一个else时,将else匹配给最后出现尚未匹配的if子句;
3.Case标号必须是整型常量表达式,两个case不能有相同值;
二、While
1.(6.12)编写一个小程序,从标准输入读入一系列string对象,寻找一系列重复出现的单词,找出最多的次数的,输出次数和对象;
#include<string>
#include<iostream>
#include<vector>
#include<iterator>
using namespace std;
void main() {
string last, curs, maxs;
int count = 0, max = 1;
vector<string> vecs;
cout << "please input a series of string:(ctrl+z to end)" << endl;
while (cin >> curs) {
if (curs == last) {
++count;
}
else
{
count = 1;
}
if (max == count) {
vecs.push_back(last);
}if (max < count) {
vecs.clear();
vecs.push_back(last);
max = count;
}
last = curs;
}
if (vecs.size() > 1) {
for (vector<string>::iterator iter = vecs.begin(); iter != vecs.end(); iter++) {
maxs += " " + *iter;
}
}
else {
maxs = vecs[0];
}
if (max != 1) {
cout << "string:" << maxs << " count:" << max << endl;
}
else
{
cout << "no word is repeated" << endl;
}
system("pause");
}