C++ 字符串和数字拼接

时间:2025-03-20 08:20:32

方法一: 

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
    int a = 2;
    string b = "abc";
    stringstream ss;
    ss << a << b;
    cout << () << endl;
    return 0;
}

方法二:

#include <iostream>
using namespace std;
int main()
{
int a = 6;
char b[10];
sprintf_s(b, "abc%d", a);
cout << b << endl;
system("pause");
return 0;
}

方法三:

std::stirng和int互转

#include <string>
#include <iostream>
 
int main(int argc, char *argv[])
{
    std::string str = "1";
    int num = std::stoi(str);
    std::cout<<"num: "<<num<<std::endl;
    std::string str2 = std::to_string(num);
    std::cout<<"str2: "<<str2<<std::endl;
    system("pause");
    return 0;
}

原文链接:/caoshangpa/article/details/52794257