美文网首页C/C艹
CPP字符串连接

CPP字符串连接

作者: 会长__ | 来源:发表于2019-02-14 00:07 被阅读16次

php一个 . 就连接了,再次说明了php是世界上最好的语言

#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>
#include <cstring> 
using namespace std;

//第一种C风格的转化
void test()
{
    char *s = "dong";
    int a = 52;
    float b = .1314;
    char *buf = new char[strlen(s) + sizeof(a) + 1];
    sprintf(buf, "%s%d%.4f", s, a, b);
    printf("%s\n", buf);
}
// //半C半C++风格,
// void test1()
// {
//     string s = "dong";
//     int a = 520;
//     char *buf = new char[10];//2147483647 int最大值
//     _itoa(a, buf, 10);      //itoa虽然可以转化为各种进制,但是注意不能是float或者double
//     cout << s + buf << " | ";
//     _itoa(a, buf, 16);
//     cout << s + buf << endl;
// }

//纯C++风格
void test2()
{
    string s = "陈明东";
    int a = 52;
    double b = .1314;
    ostringstream oss;
    oss << s << a << b;
    cout << oss.str() << endl;
}
//C++11新特性
void test3()
{
    int a = 520;
    float b = 5.20;
    string str = "dong";
    string res = str + to_string(a);
    cout << res << endl;
    res = str + to_string(b);
    res.resize(8);
    cout << res << endl;
}
int main()
{
    test();
    // test1(); 这种方式下我的ide会提示_itoa 方法不存在,有知道的可以下面评论下
    test2();
    test3();
    return 0;
}
image.png

转载地址

https://blog.csdn.net/program_anywhere/article/details/63720261

相关文章

  • CPP字符串连接

    php一个 . 就连接了,再次说明了php是世界上最好的语言 转载地址 https://blog.csdn.ne...

  • 数据库连接字符串

    SQLServer 连接字符串 MySql 连接字符串

  • Python Day195(字符串的连接)复盘

    字符串的连接 直接使用“+”连接字符串 代码

  • socket编程

    http://c.biancheng.net/cpp/socket/三次握手建立连接四次握手断开连接close函数...

  • lua使用的一点建议

    string 拼接大量字符串 少用默认连接符号当需要拼接大量字符串的时候,少用默认的字符串连接,如果大量字符串连接...

  • 比特币源码阅读(获得余额-解析参数"钱包名称")

    src/wallet/rpcwallet.cpp 从JSONRPCRequest当前URI(字符串)里面获得携带的...

  • 随记

    PHP中的字符串连接运算符 字符串连接运算符是为了将两个字符串进行连接,PHP中提供的字符串连接运算符有: (1)...

  • 字符串连接

    字符串连接 (+ 号连接) 可以使用 + 号连接任意两个字符串,连接字符串时,无论是使用单引号还是双引号创建的都可...

  • 数据类型之字符串

    String是一个类,表示字符串。 字符串都得用双引号(''')括起来,连接字符串使用“+”(连接)符号; 字符串...

  • 【ADO.NET】数据库查询SQLCommand方法

    1.创建连接字符串 2.使用Connection通过连接字符串进行连接 创建对象使用Command对象.Comma...

网友评论

    本文标题:CPP字符串连接

    本文链接:https://www.haomeiwen.com/subject/xekcsqtx.html