codecvt不是std头文件
Is codecvt not a std header?
此代码在Visual c++ 11下编译,并在Windows 7上正常运行,但在Windows 7上使用MinGW 4.7.0或Linux上使用gcc 4.8.0无法编译。使用-std=c++11
标志符编译
#include <codecvt>
#include <string>
// convert UTF-8 string to wstring
std::wstring utf8_to_wstring (const std::string& str)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
return myconv.from_bytes(str);
}
// convert wstring to UTF-8 string
std::string wstring_to_utf8 (const std::wstring& str)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
return myconv.to_bytes(str);
}
错误:codecvt: No such file or directory.
GCC拒绝此代码的原因很简单:libstdc++还不支持<codecvt>
。
c++ 11支持状态页确认了这一点:
22.5标准代码转换面N
这个问题是三年前问的,我很惊讶我在使用Ubuntu 14.04的新更新时也遇到了同样的问题。
第二个惊喜,@Fanael提供的链接现在显示:
22.5标准代码转换面Y
所以我搜索了GCC的哪个版本可以完全实现c++ 11。原来在GCC 5中添加了完整的支持:
https://gcc.gnu.org/gcc-5/changes.html完全支持c++ 11,包括以下新特性:
…
locale facet用于Unicode转换;
…
如果我有足够的声望,我很乐意对这个答案发表评论:)
使用Boost的解决方案。地区:
#include <boost/locale/encoding_utf.hpp>
#include <string>
using boost::locale::conv::utf_to_utf;
std::wstring utf8_to_wstring(const std::string& str)
{
return utf_to_utf<wchar_t>(str.c_str(), str.c_str() + str.size());
}
std::string wstring_to_utf8(const std::wstring& str)
{
return utf_to_utf<char>(str.c_str(), str.c_str() + str.size());
}
在Ubuntu 14.04和gcc 5.3上运行
相关文章:
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 如何传递多个 std::文件系统选项?
- 遍历顺序由 std::文件系统directory_iterator给出
- 使 std::fstream 写入文件末尾,但从头开始读取
- 是否可以使用 Python csv 阅读器读取使用 C++ std::setw 生成的文件?
- std::ifstream 在读取文件中最后一项时设置 eofbit,但仅在读取数值类型时发生
- C++:std::ofstream 方法 open() 在第二次迭代时擦除打开的 ifstream 文件
- 'std::wstring_convert'尽可能多地转换(从 UTF8 文件读取块)
- C++ VS 错误:<实验/文件系统>提供 std::experimental::文件系统的标头已被Microsoft弃用,将被删除
- 'generate'不是窗口头文件中'std::random_device'的成员<random>
- Windows std::fstream 修改文件后打开()?
- 是否可以使用 std::文件系统获取根名称列表?
- 如何将文件的一部分读取到std::list缓冲区?
- llvm 9在10.15之前的mac版本上支持std::文件系统吗
- 从带有 std::ifstream::read() 的文件中读取 int 遍历 char * 二进制数据
- 当 ofstream 指针位于 std::map 中时,文件写入不会发生 C++.
- std(boost)::文件系统::p ath 组件数
- 变量 std::fstream 文件具有初始值设定项,但类型不完整 编译错误
- std::文件系统根路径如何将自身作为父路径?
- 为什么我打开一个没有std::ios::binary的文件(std::ifstream)