嗨,我想在字符串中保存多行。我有一个字符串日志字符串,我想保存多播错误日志,以后我可以将其打印在 txt 文件中或作为控制台输出。是否有可能使用 endl 来格式化字符串变量?我搜索了互联网,但我只找到
cout << "" << endl;
Run Code Online (Sandbox Code Playgroud)
现在我的想法是:
std::string logstring;
logstring = logstring + "Error Message" + "Number" + "Time + Date";
logstring += endl;
Run Code Online (Sandbox Code Playgroud)
这样的事情是可能的还是没有办法格式化字符串变量?后来我想将它们打印到 log.txt 文件中?是否可以使用这样的字符串?
std::ofstream logfile;
logfile.open(log.txt);
logfile << logstring;
Run Code Online (Sandbox Code Playgroud)
文本文件应如下所示
Error Message 1 Date
Error Message 2 Date
Error Message 3 Date
...
Run Code Online (Sandbox Code Playgroud)
有可能像这样得到它还是我必须单独打印所有行?
#include <unordered_map>
#include <memory>
#include <vector>
template<> // Voxel has voxel.position which is a IVec2 containing 2 values, it also has a bool value
struct hash<Voxel> {
size_t operator()(const Voxel & k) const
{
return Math::hashFunc(k.position);
}
};
template<typename T> // This was already given
inline size_t hashFunc(const Vector<T, 2>& _key)
{
std::hash<T> hashfunc;
size_t h = 0xbd73a0fb;
h += hashfunc(_key[0]) * 0xf445f0a9;
h += hashfunc(_key[1]) * 0x5c23b2e1;
return h;
}
Run Code Online (Sandbox Code Playgroud)
我的主要
int main()
{
Voxel t{ 16,0,true };
std::hash(t); …Run Code Online (Sandbox Code Playgroud)