许多 Windows API,例如 GetModuleFileName 等...将输出写入 char* 缓冲区。但使用std::string更方便。有没有办法让它们直接写入 std::string (或 std::wstring)的缓冲区?
抱歉我的英语不好。我的母语不是英语。 -_-
塔沃恩·T。
最佳答案
如果您使用 C++0x,则保证以下内容有效:
std::string s;
s.resize(max_length);
size_t actual_length = SomeApiCall(&s[0], max_length);
s.resize(actual_length);
在C++0x之前,std::string内容不保证在内存中是连续的,因此理论上代码不可靠;实际上,它适用于流行的 STL 实现。
关于c++ - 在 std::string 中是否有等效的方法来执行 CString::GetBuffer ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4533546/