Reading in C + +--problems encountered in Chinese characters reading

Source: Internet
Author: User

Well, the subject is related to Chinese processing, so it requires C + + to read Chinese characters for processing.

First use the wchar_t character, which is the wide character, and then define:

1#include <cstdio>2#include <cwchar>3 4 using namespacestd;5 6 intMain () {7wchar_t*ch;8scanf"%s", ch);9printf"%s", ch);Ten     return 0; One}
View Code

And what's the result? No output 、、、

So we changed:

1#include <cstdio>2#include <cwchar>3 4 using namespacestd;5 6 intMain () {7 wchar_t ch;8CH =Getwchar ();9printf"%c", ch);Ten     return 0; One}
View Code

There is still no output. I tried to change it to "printf ("%d ", ch);" and then output-1

This shows that even a wide character is read in the width of char, that is, wchar_t is a character type at least to the present.

What do we do? Czr told me can actually write:

1#include <iostream>2#include <string>3 4 using namespacestd;5 6 intMain () {7     stringSt;8CIN >>St;9cout << St <<Endl;Ten     return 0; One}
View Code

The string class implements the writing high-end thing, anyway is can read into the output Chinese.

And then I said I was upset. Because Cin and cout were so slow, we thought of something strange:

1#include <cstdio>2#include <iostream>3#include <string>4 5 using namespacestd;6 7 intMain () {8Ios::sync_with_stdio (false);9     stringSt;TenCIN >>St; Onecout << St <<Endl; A     return 0; -}
View Code

Line 8 of the above program is a new addition to the sentence, "Std::ios::sync_with_stdio (false);", as long as this sentence, CIN and scanf speed is similar.
"Cin is slow for a reason, in fact, the default time, CIN and stdin always keep in sync, that is to say, these two methods can be mixed, without worrying about the file pointer confusion, and cout and stdout are the same, the two mixed will not output order confusion. Because of this compatibility feature, CIN has a lot of extra overhead, how do I disable this feature? Just one statement Std::ios::sync_with_stdio (false), so you can cancel the synchronization of CIN to stdin. "-by Byvoid

So in C + + read into the Chinese this thing is finally done. By the way, it means I've never used char* 's nest to learn about the over-encapsulated string class operation 2333

Reading in C + +--problems encountered in Chinese characters reading

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.