ACM模式下C++的输入输出格式模板
读取字符
代码
int main() {
char ch = getchar();
while (ch) {
cout << ch;
ch = getchar();
}
return 0;
}
输出
c
c
h
h
a
a
r
r
读取字符串
代码
int main() {
string line;
while (getline(cin, line)) {
cout << line;
}
return 0;
}
输出
hello
hello
world
world
读取整数
代码
int stringToInteger(string input) {
return stoi(input);
}
int main() {
string line;
while (getline(cin, line)) {
int intValue = stringToInteger(line);
}
return 0;
}```
**输出**
```cpp
89
89
65
65
读取数组
代码
int main() {
string line;
while (getline(cin, line)) {
//getline(cin, line);
vector<int> vec = stringToIntegerVector(line);
for (int i = 0; i < vec.size(); i++)
{
cout << "vec[" << i << "] = " << vec[i] << ",";
}
cout << endl;
}
return 0;
}
输出
1,2,3,4,5
vec[0] = 1,vec[1] = 2,vec[2] = 3,vec[3] = 4,vec[4] = 5,