字符串的加法其实是一个拼接生成新的一个字符串,
#include <iostream> #include <Windows.h> #include <string> using namespace std; int main(void) { string s1 = "武当派"; string s2 = "张三丰"; string s3 = "太极拳"; string s4; s4 = s1 + s2; //武当派张三丰 cout << "s4=" << s4 << endl; s4 = s4 + s3; //武当派张三丰太极拳 //相当于 s4 += s3; cout << "s4=" << s4 << endl; s4 += "第一式"; //等效于:s4 = s4 + "第一式"; cout << "s4=" << s4 << endl; system("pause"); return 0; } |
总结:在C++中string字符串的相加,不是数字相加而是排序组合起来。