C++;如何获取字符串的第一个字母 这听起来像是一个简单的问题,但是C++使它变得困难(至少对我来说):我有一个WString,我想把第一个字母作为WHARGHT对象,然后从字符串中删除第一个字母。
这不适用于非ASCII字符:C++;如何获取字符串的第一个字母 这听起来像是一个简单的问题,但是C++使它变得困难(至少对我来说):我有一个WString,我想把第一个字母作为WHARGHT对象,然后从字符串中删除第一个字母。,c++,wstring,C++,Wstring,这不适用于非ASCII字符: wchar_t currentLetter = word.at(0); 因为它返回两个字符(在循环中)作为字符,例如德语Umlauts 这在这里也不起作用: wchar_t currentLetter = word.substr(0,1); error: no viable conversion from 'std::basic_string<wchar_t>' to 'wchar_t' 还有其他想法吗 干杯 马丁 ----更新----- 下面
wchar_t currentLetter = word.at(0);
因为它返回两个字符(在循环中)作为字符,例如德语Umlauts
这在这里也不起作用:
wchar_t currentLetter = word.substr(0,1);
error: no viable conversion from 'std::basic_string<wchar_t>' to 'wchar_t'
还有其他想法吗
干杯
马丁
----更新-----
下面是一些可执行代码,可以演示这个问题。该程序将循环所有字母并逐个输出:
#include <iostream>
using namespace std;
int main() {
wstring word = L"für";
wcout << word << endl;
wcout << word.at(1) << " " << word[1] << " " << word.substr(1,1) << endl;
wchar_t currentLetter;
bool isLastLetter;
do {
isLastLetter = ( word.length() == 1 );
currentLetter = word.at(0);
wcout << L"Letter: " << currentLetter << endl;
word = word.substr(1, word.length()); // remove first letter
} while (word.length() > 0);
return EXIT_SUCCESS;
}
#包括
使用名称空间std;
int main(){
wstring word=L“für”;
wcout以下是由以下人员提供的解决方案:
是的,您需要Boost,但似乎您无论如何都需要一个外部库
C++不知道Unicode。请使用外部库,如ICU
(Unicode解构类)或Qt(QString类),都支持Unicode,
包括UTF-8
由于UTF-8具有可变长度,因此各种索引都可以
以代码单位而不是代码点进行索引。这是不可能的
UTF-8序列中的码点随机访问,因为它是
可变长度特性。如果您想要随机访问,则需要使用
固定长度编码,如UTF-32。为此,可以使用U前缀
在弦上
<> P> C++语言标准没有显式编码的概念。
包含“系统编码”的不透明概念,wchar\u t是
“足够大”的类型
将不透明系统编码转换为显式外部编码
编码时,必须使用外部库。选择的库
将是iconv()(从WCHAR_T到UTF-8),它是Posix和
在许多平台上可用,但在Windows上可用
WideChart多字节函数保证生成UTF8
C++11以std::string s=u8“Hello”的形式添加新的UTF8文本
世界:\U0010FFFF“。这些已经在UTF8中,但它们不能
与不透明wstring接口,而不是通过I
描述
C++中的编码相当复杂,下面是我的理解。
当然
每个实现都必须支持来自基本源的字符
字符集。包括§2.2/1中列出的常用字符
(§C++11中的§2.3/1)。这些字符应全部放入一个字符中
添加实现必须支持命名其他实现的方法
使用一种称为通用字符名的方式的字符
\uffff或\Uffffffff和可用于引用unicode字符。A
它们的子集在标识符中可用(见附录E)
这很好,但是从文件中的字符到
源字符(在编译时使用)由实现定义。
这构成了所使用的编码
第一个版本的问题是什么?你能为你的UMLUAT问题发布代码吗?C++的字符串函数不是天生的Unicode感知的。不要期望它们知道UMLUUT和字母之间的区别。<代码> WString::< /Cord>返回一个新的<代码> WScord,不是一个字符。非常感谢帮助。我更喜欢一个没有依赖于外部库的解决方案。虽然我无法想象像C++这样简单的东西“不在盒子里”。@ MARW其实不是那么简单,而且(从我引用的来源),标准对此并不明确。如果您想要一致性,请使用库。
#include <iostream>
using namespace std;
int main() {
wstring word = L"für";
wcout << word << endl;
wcout << word.at(1) << " " << word[1] << " " << word.substr(1,1) << endl;
wchar_t currentLetter;
bool isLastLetter;
do {
isLastLetter = ( word.length() == 1 );
currentLetter = word.at(0);
wcout << L"Letter: " << currentLetter << endl;
word = word.substr(1, word.length()); // remove first letter
} while (word.length() > 0);
return EXIT_SUCCESS;
}
#include <iostream>
#include <string>
#include <boost/regex/pending/unicode_iterator.hpp>
using namespace std;
template <typename C>
std::string to_utf8(C const& in)
{
std::string result;
auto out = std::back_inserter(result);
auto utf8out = boost::utf8_output_iterator<decltype(out)>(out);
std::copy(begin(in), end(in), utf8out);
return result;
}
int main() {
wstring word = L"für";
bool isLastLetter;
do {
isLastLetter = ( word.length() == 1 );
auto currentLetter = to_utf8(word.substr(0, 1));
cout << "Letter: " << currentLetter << endl;
word = word.substr(1, word.length()); // remove first letter
} while (word.length() > 0);
return EXIT_SUCCESS;
}
Letter: f
Letter: ü
Letter: r