Generally, LP represents a pointer, C represents a const, T represents a wide character, and STR represents a string. This corresponds to the following:
LPCTSTR = const TCHAR *
Here we have to explain the relationship between TCHAR wideResult [MaxResultLen] and const TCHAR * wideResult.
The former is a character array, and the latter is a character pointer. Everyone knows the character array and stores the characters. Here we will talk about the character pointer next to it, here, the entire character Pointer Modified by const is not simply the wideResult pointer, so the content pointed by wideResult here is constant and cannot be changed, but the pointer pointing to wideResult can be changed, that is, you cannot try to change the content through wideResult [I] = 'a'; but you can change wideResult, for example, wideResult ++;
After explaining the relationship between the two, you can clearly understand the following operations:
TCHAR wideResult [MaxResultLen;
Next, let's start with the question, focusing on how to convert between the LPCTSTR and std: wstring,
typedef basic_string<wchar_t, char_traits<wchar_t>,allocator<wchar_t> > wstring;
This is a template class that everyone knows. There are many methods in it. You can call them at will. These methods are very helpful for string processing. What is the LPCTSTR, pointer, it is very troublesome for pointers to perform complex operations on strings. Here we should all want to convert the LPCTSTR into a wstring for processing. The following describes how to convert the string.
std::wstring wsPath = sPath;
It is so simple that the data in the wsPath is stored in the content pointed to by the sPath.
However, if you define many such objects, there will be a problem, that is, a waste of memory. All the members in the class must allocate the memory, which will definitely affect the running speed, therefore, it is best to use LPCTSTR to represent the string when not processing it, and convert it to std: wstring when processing it. After processing, it will be converted back. How can this problem be solved?
LPCTSTR sPath = wsPath.c_str();
It is also so simple that we can solve the battle. Std: wstring contains many common operations. c_str () is the operation for retrieving content!
This article from the "selling cute programmers" blog, please be sure to keep this source http://7677869.blog.51cto.com/7667869/1266955