C++将两个字符串连接起来

参考https://blog.csdn.net/u010925447/article/details/79145767

参考2https://www.cnblogs.com/qlwy/archive/2012/03/25/2416937.html

#include<iostream>
#include<string>
#include<cstdlib>
#include<sstream>
#include<stdio.h>
#include <cstring>
#include <typeinfo>
using namespace std;

//c语言风格 sprintf函数
void test1()
{
     char* name = "suyunzzz";
     int age_number = 22;
     char* be = "is";
     string age = "age";
     cout<<"name: "<<name<<"\n"
         <<"age_number: "<<age_number<<"\n"
         <<"be: "<<be<<"\n"
         <<"age: "<<age<<endl;
//         char sum[100];
         char* sum = new char;
         sprintf(sum , "%s %s %s %d",name,age.c_str(),be,age_number);
         cout<<sum<<endl;
}

//ostringstream类型组合数据
void test2()
{
    string s = "苏云征";
    int a = 520;
    double  b = .1314;
    ostringstream oss;
    oss<<s<<" "<<a<<" "<<b<<endl;
    cout<<oss.str()<<endl;
}

//C++中的string类型
void test3()
{
    string a = "a";
    string b = "b";
    string c = "c";
    string sum;
    sum  = a+b+c;
    char d;
    cout<<"变量d的类型:" <<typeid(d).name()<<endl;
    cout<<sum<<endl;
    cout<<sum.c_str()<<endl;
}
int main()
{
    test1();
    test2();
    test3();
}

结果: 

name: suyunzzz
age_number: 22
be: is
age: age

suyunzzz age is 22

苏云征 520 0.1314

变量d的类型:c
abc
abc