String can also be wonderful

Source: Internet
Author: User

// ================================================ ========================================

// Title:

// String can also be wonderful

// Author:

// Norains

// Date:

// Moday 03-November-2008

// Environment:

// None

// ================================================ ========================================

For friends who are used to the MFC architecture, it is estimated that the most painful way to write code directly using the API is to have no cstring. In fact, in STL, we still have STD: String available. Although it is not as convenient as cstring in some ways, we can barely win over it.





1. macro definition



In wince programming, wchar_t is rarely used directly, as is Char. Instead, we prefer Microsoft's suggestion: tchar ---- No way. Who makes wince a Microsoft product? However, since all of them have been on this thief ship, we may wish to follow the same conventions and make a similar definition of STD: String:



# Ifdef Unicode

# Ifndef tstring

# Define tstring STD: wstring

# Endif

# Else

# Ifndef tstring

# Define tstring STD: String

# Endif

# Endif



Another benefit is that we can beat the keyboard four times less. From the perspective of extending the lifetime of the keyboard, this is also a major contribution. Well, in order for my keyboard to serve longer, I will directly use tstring instead of the following content.





2. Initialization



There are various initialization methods, generally as follows:

Tstring stra = text ("");

Tstring strb (text ("B "));



If you are dealing with containers, it is actually very simple:

STD: vector <tchar> VTC (max_path, 0 );

Tstring STRC (VTC. Begin (), VTC. End ());





3. assign values



The initialization is simple, and the assignment value is no different:



Stra = text ("A2 ");

Tchar SZB [] = text ("B2 ");

Strb = SZB;



Dealing with containers is also simple:

STRC = & VTC [0];





4. append



Let's recall the days when an array is used purely. If we want to append a string to the end of a fixed array, We need to allocate a sufficient space and assign a value to the old value, add a new value. Many may remember the following pseudocode:



Tchar * pbuf;

...

Tchar * pnew = new tchar [_ tcslen (pbuf) + text ("APPEND") + 1];

_ Tcscpy (pnew, pbuf );

_ Tcscat (pnew, text ("APPEND "));

Delete [] pbuf;

Pbuf = pnew;



Well, that's true. If we need to append a new value to the end of an array, We have to experience these troubles. Fortunately, we can finally talk about goodbye with this kind of torture. What is the append value for string? Very simple, just like Java:

Stra + = text ("APPEND ");



Yes, you are not mistaken. It is a statement and there is no other place.



For arrays and containers, the same is true:

Strb + = SZB;

STRC ++ = & VTC [0];





5. Dealing with API functions



The API functions of windows are naturally used by users of C. Therefore, we can say that most of the API functions, in order to be able to be called in C, basically use the C interface. Tstring, to be exact, is a class rather than a value type, so it cannot be directly assigned a value.



Take createfile as an example. Its declaration is as follows:

Handle createfile (

Lptstr lpfilename,

DWORD dwdesiredaccess,

DWORD dw1_mode,

Lpsecurity_attributes lpsecurityattributes,

DWORD dwcreationdisposition,

DWORD dwflagsandattributes,

Handle htemplatefile

);



The first parameter is the lpctstr, which is a pointer. Therefore, it is not possible to assign a value directly to the iron:

Createfile (stra ,...);



Fortunately, the great STL builder has considered this issue for us. We only need to call the c_str function:

Createfile (stra. c_str (),...);



Similarly, c_str can also be used in pure string functions such as _ tcslen and _ tcscat.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.