赞
踩
函数 | 总结 |
---|---|
cin>> | 遇到空格和换行自动停止读入,换行空格仍在缓存区中,下次读入自动跳过 读取后面的字符 |
cin.get() | 读入字符 |
getline(cin,str) | 读取整行数据到str中 |
cin.getline(char*,int) | 接收字符串储存到char*中,长度为n 可以接受空格 |
char a; cin.get(a) | a中可以储存被cin丢弃的换行符 |
void fun1()
{
int a, b;
while (cin >> a >> b)
{
cout << a + b << endl;
}
}
void fun2()
{
int n, a, b;
cin >> n;
for (int i = 0;i < n;i++)
{
cin >> a >> b;
cout << a + b << endl;
}
}
void fun3()
{
int a, b;
while (cin >> a >> b)
{
if (a == 0 && b == 0)break;
cout << a + b << endl;
}
}
void fun4()
{
int n, a, b;
b = 0;
while (cin >> n)
{
if (n == 0)break;
for (int i = 0;i < n;i++)
{
cin >> a;
b += a;
}
cout << b << endl;
}
}
void fun5() { int n; int a, b; while (cin >> n) { for (int i = 0;i < n;i++) { int num; while (cin >> num)//结束后继续输入返回这里 { a = 0; for (int j = 0;j < num;j++) { cin >> b; a += b; } cout << a << endl; } } } }
void fun6()
{
int n, a, b;
b = 0;
while (cin >> n)
{
if (n == 0)break;
for (int i = 0;i < n;i++)
{
cin >> a;
b += a;
}
cout << b << endl;
}
}
void fun7()
{
int a, sum;
sum = 0;
while (cin >> a)
{
sum += a;
if (getchar() == '\n')
{
cout << sum << endl;
sum = 0;
}
}
}
void fun8() { int n; string s; vector<string>ret; cin >> n; for (int i = 0;i < n;i++) { cin >> s; ret.push_back(s); } sort(ret.begin(), ret.end()); for (int i = 0;i < n;i++) { cout << ret[i] << endl; } }
void fun9() { string s; //cin >> n; while (1) { vector<string>ret; while (cin >> s) { ret.push_back(s); if (getchar() == '\n') { break; } } sort(ret.begin(), ret.end()); for (int i = 0;i < ret.size();i++) { cout << ret[i] << endl; } } }
vector<string> splitstr(string s,char c) { s += c;//方法一 手动补齐末尾 vector<string> ret; int begin = 0; int i = 0; for (i = 0;i < s.size();i++) { if (s[i] == c) { if (i != begin) { ret.push_back(s.substr(begin, i-begin)); begin = i + 1; } else { begin++; } } } if (begin != i) { ret.push_back(s.substr(begin, i - begin)); }//方法2 判断最后一个字符是否为分割符 return ret; } void fun10() { while (1) { vector<string>ret; while (cin >> s) { ret = splitstr(s, ','); if (getchar() == '\n') { break; } } sort(ret.begin(), ret.end()); for (int i = 0;i < ret.size();i++) { cout << ret[i] << endl; } } }
,a,b,c,d, 返回 a b c d
vector<string> splitstr(string s,char c) { s += c;//方法一 手动补齐末尾 vector<string> ret; int begin = 0; int i = 0; for (i = 0;i < s.size();i++) { if (s[i] == c) { if (i != begin) { ret.push_back(s.substr(begin, i-begin)); begin = i + 1; } else { begin++; } } } //方法2 判断最后一个字符是否为分割符 //if (begin != i) { ret.push_back(s.substr(begin, i - begin)); } return ret; }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。