C++连接字符串:
C++中只有字符类型,没有字符串类型,因此在C++中将两个字符串相连比较费事,在此我运用了
C++STL中的vector进行字符串的连接。
题目:
Description
写一函数,将两个字符串连接
Input
两行字符串
Output
链接后的字符串
Sample Input
123
abc
Sample Output
123abc
代码:
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
vector<char> A;
void main() {
while (1) {
char c;
cin >> c;
if (c == 'S') {
break;
}
A.push_back(c);
}
for (auto b : A) {
cout << b;
}
}
当输入字符123和abc输入完毕时输入S就可直接输出结果,这里运用了可变长度大小的容器vector,
方便输入不受长度的限制。
运行结果: