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

 

  • 6
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值