您的位置:首页 > 编程语言 > C语言/C++

C++连接两个字符串

2017-12-10 19:47 176 查看

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,

方便输入不受长度的限制。

运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: