赞
踩
在Java中,要把一个整型和字符串拼接起来只需要用+操作符直接拼接起来就可以了:String str = 1 + ".txt",得到str的值就是1.txt。
在C++中则比较麻烦一些,可以通过以下2中方式实现:
①通过使用头文件<sstream>中的stringstream,核心代码如下:
#include<string>
int i = 1;
std::stringstream ss << i;
std::string str = ss.str();
str.append(".txt");
②调用swprintf_s函数,“组装”出对应的文件名称
#include <tchar.h>
wchar_t filename[20];
int i = 1;
swprintf_s(filename,L"%d.txt",i);
通过以上两种方式都可以拼接出一个值为“1.txt”的字符串。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。