在C ++中获取拉丁字符 [英] Get Latin Character in C++
本文介绍了在C ++中获取拉丁字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 C ++
的新秀。我有一个字符串tỏa
,但是我找不到字符'ỏ'
,为什么长度如此字符串是5?如何获得该字符作为变量?
I am a rookie with C++
. I have a string "tỏa"
but I can't get the character 'ỏ'
and why the length of that string is 5? How can I get that character as a variable?
void test() {
std::string str ("tỏa");
for(int i=0; i<str.length(); ++i){
std::cout << str[i] << std::endl;
}
}
该代码的输出为:
t
�
�
�
a
有人可以帮助我吗?
推荐答案
结合使用 setlocale()
和 wstring
:
Use a combination of setlocale()
and wstring
:
#include <clocale>
#include <iostream>
void test() {
std::wstring str = L"tỏa";
for(int i=0; i<str.length(); ++i){
std::wcout << str[i] << std::endl;
}
std::wcout << "Size: " << str.size(); //the size of the string is 3 as it should
}
int main()
{
setlocale(LC_ALL, "");
test();
return 0;
}
编辑:
如果要将宽字符保存在变量中,则很简单:
If you want to save the wide char in a variable it's as simple as:
wchar_t ch = str[1];
您还可以使用ASCII代码:
You can also use the ASCII code:
wchar_t ch = 7887;
注意:
这可能不适用于以下版本的所有编译器所有SO,都不能保证100%可移植性。
Note: This may not work in all compilers in all SO's, 100% portability is not guaranteed.
这篇关于在C ++中获取拉丁字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文