C++ 如何将两个变量连接到另一个变量中

C++ 如何将两个变量连接到另一个变量中,c++,C++,请参见示例以了解 int rnd = rand() %10; string Folder = "c://foldername"; string final_name = Folder + rnd; // here the target /* I want the result like that (random folder name) foldername5 foldername10 foldername3 foldername20 foldername17 */ 使用std::strin

请参见示例以了解

int rnd = rand() %10;
string Folder = "c://foldername";
string final_name = Folder + rnd; // here the target

/* I want the result like that (random folder name)
foldername5
foldername10
foldername3
foldername20
foldername17
*/

使用
std::stringstream
作为:

#include <sstream> //include this

std::stringstream ss;
ss << Folder  << rnd;
string final_name = ss.str();

在C++中,最好的方法是使用<代码> StrugSu水流:

#include<sstream>

...

std::stringstream stream;
stream << "c://foldername" << rand() %10;
stream.str(); // now contains both path and number
#包括
...
std::stringstream;

C++中的流使用StrueScript将整数转换为字符串。

int rnd = rand() %10;
string Folder = "c://foldername";
stringstream ss;
ss << Folder << rnd;
string final_name = ss.str(); // here the target
int rnd=rand()%10;
string Folder=“c://foldername”;
细流ss;
ss说:

std::string final_name = Folder + std::to_string(rnd);
如果您有一个不支持C++11的旧编译器,您可以使用
boost::lexicalcast
,或
std::snprintf
,或字符串流。

rnd
(它是整数类型)转换为
string
类型,然后执行相同的操作

string final_name = Folder + rnd;

peeve:
rand()/(rand\u MAX/10+1)
请参见使用数据类型(long-long,long-double)但不使用(int,float)的字符串函数@LionKing:我怀疑,因为标准(21.5.7)明确要求使用
int
重载。。。也许你的编译器坏了?您可以将该值转换为
long int
long-long int
,如果这样有帮助的话,我想。
int rnd = rand() %10;
string Folder = "c://foldername";
stringstream ss;
ss << Folder << rnd;
string final_name = ss.str(); // here the target
std::string final_name = Folder + std::to_string(rnd);
string final_name = Folder + rnd;