如何从type (Class)转换为std:string
How to convert from type (Class) to std:string?
我有一个名为blog[]的字符串数组我试图使用数组中位置I的字符串,如:
outfile << blog[i];
但是问题是blog[i]是微博类型(微博是我正在使用的一个类)
所以我的问题是我如何从类型微博转换到类型字符串?
这是我试图使用blog[I]的方法:
bool MicroBlog::SaveBlog(const string filename, const MicroBlog &AnotherMicroBlog)
{
ofstream outfile;
outfile.open(filename.c_str());
num_tweets = AnotherMicroBlog.num_tweets;
for (int i=0; i < num_tweets; i++)
{
outfile << blog[i];
outfile.close();
}
}
您必须编写自己的操作符,例如toString()
或重载<<
:
class Microblog {
....
std::string toString() const { //public
string ret = all_the_data;
return ret;
}
};
然后outfile << blog[i].toString();
相关文章:
- 将无符号char*转换为std::istream*C++
- 可以有效地转换 std::any 与 std::any_cast
- 尝试转换 std::chrono::d uration 会导致"rep cannot be a duration"编译错误
- 封送处理 - 转换 std::向量<char>到字符串^ 反之亦然
- 在 C++ 中转换 std::string,使其可以是 C# 中的 byte[]
- 转换 std::vector<T> 为 char *
- 转换std ::绑定到功能指针
- 强制转换 std::vector 的所有成员
- 无法转换'std::string{又名std::basic_string<char>}'到&
- 转换std::string到cv::Mat对象
- 转换std::chrono::system_clock::time_point::min()为字符串时,无效空指针错误
- 简单的模板化函数,用于转换 std::vectors - "illegal use of this type as an expression"
- Const转换std容器
- 如何强制转换std::make_unique,以便可以使用在类中声明的函数
- 如何向下转换std::shared_ptr
- 转换 std::vector<uint8_t> 为 QImage
- 使用std::dynamic_pointer_cast向上转换std::shared_ptr
- 如何转换std::vector<无符号字符>到向量<char>而无需复制
- 正在强制转换std::pair
const&T1 const, T2>const&安全的 - 如何在boost.python中转换std::string*