如何从std :: string获得一个可写的C缓冲区?

我试图将我的代码从使用MFC的CString到Microsoft Windows平台的std::string 。 我对某事很好奇。 在下面的例子中说:

 CString MakeLowerString(LPCTSTR pStr) { CString strLower = pStr ? pStr : L""; CharLower(strLower.GetBuffer()); //Use WinAPI strLower.ReleaseBuffer(); return strLower; } 

我使用strLower。 GetBuffer ()获得一个可写入的缓冲区以传递给CharLower API。 但是我没有在std::string看到类似的方法。

我错过了什么吗? 如果是这样,你将如何使用std::string覆盖上面的方法?

在我的新工作中,我们不使用MFC–但是幸运的是lib和C ++ 11–所以我得出了和c00000fd一样的问题。 感谢BitTickler的回答,我想出了通过&s[0]和resp使用字符串的内部缓冲区的Win32-API的想法。 &s.front() catch。

使用收缩内部字符串缓冲区的Win32-API函数

假设你有一个字符串可以被Win32-API函数缩短 – 例如::PathRemoveFileSpec(path) – 你可以按照这个方法:

 std::string path( R("?(C:\TESTING\toBeCutOff)?") ); ::PathRemoveFileSpec( &path.front() ); // Using the Win32-API // and the the string's internal buffer path.resize( strlen( path.data() ) ); // adjust the string's length // to the first \0 character path.shrink_to_fit(); // optional to adjust the string's // capacity - useful if you // do not plan to modify the string again 

Unicode版本:

 std::wstring path( LR("?(C:\TESTING\toBeCutOff)?") ); ::PathRemoveFileSpec( &path.front() ); // Using the Win32-API // and the the string's internal buffer path.resize( wcslen( path.data() ) ); // adjust the string's length // to the first \0 character path.shrink_to_fit(); // optional to adjust the string's // capacity - useful if you // do not plan to modify the string again 

使用扩展内部字符串缓冲区的Win32-API函数

假设你有一个字符串,它将被Win32-API函数扩展或填充 – 例如::GetmoduleeFileName(NULL, path, cPath)来检索你的可执行文件的路径 – 你可以按照下面的方法:

 std::string path; path.resize(MAX_PATH); // adjust the internal buffer's size // to the expected (max) size of the // output-buffer of the Win32-API function ::GetmoduleeFileName( NULL, &path.front(), static_cast<DWORD>( path.size() ) ); // Using the Win32-API // and the the string's internal buffer path.resize( strlen( path.data() ) ); // adjust the string's length // to the first \0 character path.shrink_to_fit(); // optional to adjust the string's // capacity - useful if you // do not plan to modify the string again 

Unicode版本:

 std::wstring path; path.resize(MAX_PATH); // adjust the internal buffer's size // to the expected (max) size of the // output-buffer of the Win32-API function ::GetmoduleeFileName( NULL, &path.front(), static_cast<DWORD>( path.size() ) ); // Using the Win32-API // and the the string's internal buffer path.resize( wcslen( path.data() ) ); // adjust the string's length // to the first \0 character path.shrink_to_fit(); // optional to adjust the string's // capacity - useful if you // do not plan to modify the string again 

当你最终缩小到适合字符串,那么当扩展字符串的内部缓冲区时,与MFC替代方法相比,只需要再多一行代码,缩小字符串时,它的开销几乎相同。

CString方法相比, std::string方法的优点是您不必声明额外的C-String指针变量,只需使用官方的std::string方法和一个strlen / wcslen函数即可。 上面显示的方法只适用于收到的Win32-API缓冲区以空字符结尾的缩小变体,但对于Win32-API返回未终止字符串的特殊情况,则类似于CString::ReleaseBuffer方法,你必须通过path.resize( newLength )显式地知道和指定新的字符串/缓冲区长度 – 就像path.ReleaseBuffer( newLength )CString替代。

 void GetString(char * s, size_t capacity) { if (nullptr != s && capacity > 5) { strcpy_s(s,capacity, "Hello"); } } void FooBar() { std::string ss; ss.resize(6); GetString(&ss[0], ss.size()); std::cout << "The message is:" << ss.c_str() << std::endl; } 

正如你所看到的,你可以使用“老派c指针”来将字符串提供给传统函数,并将其用作OUT参数。 当然,你需要确保它有足够的容量来工作。

小写std::string是:

 #include <algorithm> #include <string> std::string data = "Abc"; std::transform(data.begin(), data.end(), data.begin(), ::tolower); 

你真的无法绕过每个角色。 原始的Windows API调用将在内部执行相同的字符迭代。

如果您需要为标准的“C”语言环境以外的其他语言环境获取toLower() ,则可以使用:

 std::string str = "Locale-specific string"; std::locale loc("en_US.UTF8"); // desired locale goes here const ctype<char>& ct = use_facet<ctype<char> >(loc); std::transform(str.begin(), str.end(), str.begin(), std::bind1st(std::mem_fun(&ctype<char>::tolower), &ct)); 

要直接回答你的问题并减去任何上下文,你可以调用str.c_str() )从std::string获得一个const char * (LPCSTR)。 您不能直接将std::string转换为char * (LPTSTR); 这是通过设计,并会破坏使用std::string一些动机。

根据您的要求,您可以使用以下一项或多项:

  1. std::string::operator[]() 。 这个函数返回给定索引处的一个字符而没有边界检查。

  2. std::string::at() 。 此函数返回给定索引处的字符并进行边界检查。

  3. std::string::data() 。 这个函数返回一个指向原始数据的const指针。

  4. std::string::c_str() 。 这个函数返回与std::string::data()相同的值