C ++ string full guide (2)

xiaoxiao2021-03-06  54

MFC class cstring

The MFC's cstring contains Tchar, which depends on the setting of the pre-process tag. Typically, the CString is like a STL string is an opaque object, and can only be modified by a cstring method. CString is more superior to the STL string that its constructor accepts MBCS and Unicode strings. And can be converted to LPCTSTR, so the CString object can be directly delivered to the function of receiving LPCTSTR, and the c_str () method is not required.

// Construct CString S1 = "CHAR STRING"; // Structure CString S2 = L "Wide Char String" from LPCSTR; // Constructs CString S3 ('', 100) from LPCWSTR; // Prepare 100 bytes, fill space CSTRING S4 = "New window text"; // You can use CString: setWindowText (HWNDSOMEWINDOW, S4); // or, Explicitly Mandatory Type Conversion: SetWindowText (HWNDSOMEWINDOW, (LPCTSTSTSTSTSTSTSTS);

You can also load strings from a string table. CString constructs objects through LoadString (). Use the format () method to selectively read a string of a certain format from a string table.

// Structure / Load CString S5 ((LPCTSTR) IDS_SOME_STR) from the string table; // load the CString S6, S7; // from the string table (IDS_SOME_STR); // From the string table Load the string S7.Format (IDS_SOME_FORMAT, "BOB", NSOMestuff, ...);

The first constructor looks a bit strange, but it is indeed a character string load method.

Note that CString only allows for a mandatory type conversion, that is, forced to convert to LPCTSTR. Forced to convert to LPTSTR (very measured pointer) is wrong. According to old habits, convert CString to lPTSTR can only hurt themselves. Sometimes didn't find an error in the program, it just happened. The correct way to convert to a very quantity pointer is to call the getBuffer () method.

The following previous queue added elements Take an example how to use CString correctly:

CString str = _t ("new text"); lvitem item = {0}; item.mask = lvif_text; item.iitem = 1; item.psztext = (lpctstr) Str; // is wrong! Item.psztext = str.getBuffer (0); // correct listView_setitem (& item); str.releaseBuffer (); // Return the queue to STR

PSZText members are LPTSTR, a very quantitative pointer, so use STR's getBuffer (). The parameters of GetBuffer () are the minimum buffers allocated by CString. If you want to assign a 1K TCHAR, call GetBuffer (1024). The parameter is 0, and only the pointer to the string is returned.

The error statement on the above example can be compiled, even can work properly, if it is this type. But this does not prove the syntax correct. Perform a very quite compulsory type conversion, break the object-oriented package principle and pass over the internal operation of CString. If you habits such a forced type conversion, I will eventually encounter an error, but you may not know where you are, because you are doing such a conversion, and the code can run. Know why people always complain about defective software? Incorrect code is breed in the bug. However, you are willing to prepare a known code to make the bug organically multiplied? Still spend some time to learn CString's correct usage makes your code 100% correct. CString has two functions to get BSTR from CString and convert to Unicode if necessary. That is allocsystring () and setsystring (). In addition to setsysString (), both use the BSTR * parameters.

// Convert to BSTRCSTRING S5 = "Bob!"; BSTR BS1 = NULL, BS2 = NULL; BS1 = S5.Allocsystring (); S5.Setsystring (& BS2); // ... sysfreestring (BS1); sysfreestring (BS2) ;

Colevariant is very similar to CComvariant. Colevariant inherits to Variant, which can be passed to a function that needs Variant. However, unlike CComvariant, Colevariant has only one LPCTSTR constructor that does not provide a separate LPCSTR and LPCWSTR constructor. In most cases, there is no problem, because it is always willing to treat the string to LPCTSTR. But you have to know this. Colevariant also has a constructor that accepts CString.

// Construct CString S1 = _T ("Tchar String"); ColeVariant V1 = _T ("Bob"); // Construct Colevariant V2 = S1; // from the LPCTSTR V2 = S1; // Copy from CString

For CCOMVARIANT, you must directly process the Variant member, and convert it to a string when necessary when necessary, using the CHANGETYPE () method. However, Colevariant :: ChangeType () will throw an exception when the conversion fails, not the error code to return HRESULT.

// Data extraction Colevariant V3 = ...; // Constructs V3BSTR BS = NULL from some type of type; try {v3.changtype (vt_bstr); BS = v3.bstrval;} catch (ColeException * e) {// error, Unable to convert} sysfreeestring (bs);

WTL class

Cstring

WTL's CSTRING is identical to the MFC's CString behavior, see the description of MFC CString above.

CLR and VC 7 SYSTEM :: String are string classes of .NET. In its interior, the String object is a constant character sequence. Any String method for any operation String object returns a new String object because the original String object remains the same. The String class has a property that when multiple String points to the same set of character sets, they actually point to the same object. The string of Managed Extensions C has a new prefix S. is used to indicate a managed string string.

// Construct String * ms = s "this is a nice managed string"; you can use the unmanaged string string to construct the String object, but it is better to construct the String object with the Managed String. The reason is that all the same strings with S prefix points to the same object, and unmanaged string does not have this feature. The following example can be explained more clearly:

String * ms1 = s "this is nice"; string * ms2 = s "this is nice"; string * ms3 = l "this is nice"; console :: WriteLine (ms1 == ms2); // Output TrueConsole :: WriteLine (ms1 == ms3); // Output FALSE

To make comparisons with strings without S prefix, use the string :: compareto () method, such as:

Console :: WriteLine (MS1-> Compareto (MS2)); Console :: WriteLine (MS1-> Compareto (MS3));

Both output 0, indicating that the string is equal.

The conversion between String and MFC 7 cstring is easy. CString can be converted to LPCTSTR, String has two constructors that accept char * and wchar_t *. Therefore, you can directly pass the cstring to the String constructor:

CSTRING S1 ("Hello World"); String * S2 (S1); // Copy from CSTRING

The method of reverse conversion is also similar:

String * S1 = s "Three Cats"; CSTRING S2 (S1);

May be a bit confused. Starting with VS.NET, CString has a constructor to accept String objects, so it is correct.

CStringt; In order to accelerate operation, it is sometimes available to underwater strings (Underlying String):

String * S1 = s "Three cats"; console :: WriteLine (S1); const __wchar_t __pin * pstr = ptrtostringchars (S1); for (int i = 0; i (PSTR I)) ; console :: WriteLine (S1);

PTRTOStringChars () Returns the const __wchar_t * pointer to the underlying string, prevent the garbage collector from removing the string when the string is operated.

转载请注明原文地址:https://www.9cbs.com/read-54171.html

New Post(0)