C++ C++;将对象添加到字符串

C++ C++;将对象添加到字符串,c++,string,type-conversion,C++,String,Type Conversion,我试图将几个值转换成一个字符串,用作文件名,但是在尝试了几个不同的方法后,我有点不知所措 string reportfile = myarray[0][2] + myarray[0][3] + "report.txt"; cout << reportfile << endl; ofstream outfile(reportfile); 那么,如何将两个int数组项和文本转换为单个字符串。试试以下方法: #include <sstream> //

我试图将几个值转换成一个字符串,用作文件名,但是在尝试了几个不同的方法后,我有点不知所措

  string reportfile = myarray[0][2] + myarray[0][3] + "report.txt";
  cout << reportfile << endl;
  ofstream outfile(reportfile);
那么,如何将两个int数组项和文本转换为单个字符串。

试试以下方法:

#include <sstream> // ^ top of the file

std::ostringstream reportfile;
reportfile << myarray[0][2] << myarray[0][3] << "report.txt";
std::string reportfile_str = reportfile.str();
std::cout << reportfile_str << std::endl;
std::ofstream outfile(reportfile_str.c_str()); // in c++11, ommit the ".c_str()"
#包括//^文件顶部
std::ostringstream报告文件;
reportfile在C++11中,可用于int到string的转换:

string reportfile = to_string(myarray[0][2]) + to_string(myarray[0][3]) + "report.txt";

你要告诉我们什么是
myarray
吗?对不起,虽然描述已经足够了,但myarray都是整数,包含年份和月份值。
string reportfile = to_string(myarray[0][2]) + to_string(myarray[0][3]) + "report.txt";