STL string and wstring

Source: Internet
Author: User

STL has a string processing class-stirng and wstring, but it is not very convenient to use it, because it cannot be converted in the char type string and wchar_t according to the defined macro like tchar, it cannot be becauseProgramCan I convert all types to Unicode? Is there a good way?

The answer is of course yes. Let's take a look at how Ms's tchar is made. The following is an excerpt from MS Platform's tchar. H, which is slightly deleted.

# Ifdef _ Unicode

# Ifdef _ cplusplus
}/*... Extern "C "*/
# Endif

/* ++ Unicode ++ ++ */

# Include <wchar. h>

# Ifdef _ cplusplus
Extern "C "{
# Endif

# If! _ Stdc __
Typedef wchar_t tchar;
# Endif
...

# Ifdef _ MBCS

/* ++ MBCS ++ ++ */

# Ifdef _ cplusplus
}/*... Extern "C "*/
# Endif

# Include <mbstring. h>

# Ifdef _ cplusplus
Extern "C "{
# Endif

# Ifndef _ tchar_defined
Typedef char _ tchar;
Typedef signed Char _ tschar;

# If! _ Stdc __
Typedef char tchar;
# Endif

As you can see, tchar uses _ MBCS and _ Unicode macros as typedef of char and wchar_t.

Let's take a look at two classes: string and wstring:

Typedef basic_string <char, char_traits <char>, Allocator <char>
String;
Typedef basic_string <wchar_t, char_traits <wchar_t>,
Allocator <wchar_t> wstring;
The original string and wstring are also typedef, both of which are template basic_string. Since only one template is available, its implementation does not depend on the specific type, this is the meaning of the template-Abstract The implementation from the specific type.

Then we can make a tstring:

Typedef basic_string <tchar, char_traits <tchar>,
Allocator <tchar> tstring;

In this way, tstring can be a string or wstring based on different macros. When using tstring, you only need to define the expected macro without modifying it in a large area.Code.

The template gives STL powerful functions. A general library certainly cannot accommodate all requirements, but a good library should have good scalability, such as string, wstring, since it cannot meet the flexible conversion requirements in daily development, we can start to use a tstring, all the member functions of stirng,AlgorithmYou do not need to implement it unless you have special needs, because the template has already implemented these functions and algorithms, and we only need to implement it now.

In fact, not only string and wstring, fstream and wfstream can also be the same as string and wstring, using the basic_fstream template to present a tfstream

This is the powerful power of the template, and only C ++ has such powerful capabilities.

Here, I would like to thank senior fat Chan for his ideas.

 

More convenient usage:

 # ifdef _ unicodetypedef wstring tstring;  # else   typedef   string   tstring;  # endif  

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.