Facet ctype, do_is() and specializations
Facet ctype, do_is() and specializations
我派生了ctype
类来构建我自己的分面,以便覆盖其虚函数do_is()
。我的目的是使流提取器忽略空格字符(并且仍然在制表字符上标记)。此重写调用母类的实现。但它只能用wchar_t
编译.没有实现char
模板值的ctype::do_is()
。对于 gcc 和 VS 2010 来说也是如此。
这是我的代码;你只需要取消注释第5行就可以在两个版本之间进行测试。
#include <iostream>
#include <locale>
#include <sstream>
// #define WIDE_CHARACTERS
#ifdef WIDE_CHARACTERS
typedef wchar_t CharacterType;
std::basic_string<CharacterType> in = L"string1tstring2 string3";
std::basic_ostream<CharacterType>& consoleOut = std::wcout;
#else
typedef char CharacterType;
std::basic_string<CharacterType> in = "string1tstring2 string3";
std::basic_ostream<CharacterType>& consoleOut = std::cout;
#endif
struct csv_whitespace : std::ctype<CharacterType>
{
bool do_is(mask m, char_type c) const
{
if ((m & space) && c == ' ')
{
return false; // space will NOT be classified as whitespace
}
return ctype::do_is(m, c); // leave the rest to the parent class
}
};
int main()
{
std::basic_string<CharacterType> token;
consoleOut << "locale with modified ctype:n";
std::basic_istringstream<CharacterType> s2(in);
s2.imbue(std::locale(s2.getloc(), new csv_whitespace()));
while (s2 >> token)
{
consoleOut << " " << token << 'n';
}
}
谢谢 !
我从您提供的链接中执行了以下代码,这确实有效。
#include <iostream>
#include <vector>
#include <locale>
#include <sstream>
// This ctype facet declassifies spaces as whitespace
struct CSV_whitespace : std::ctype<char>
{
static const mask* make_table()
{
// make a copy of the "C" locale table
static std::vector<mask> v(classic_table(), classic_table() + table_size);
// space will not be classified as whitespace
v[' '] &= ~space;
return &v[0];
}
CSV_whitespace(std::size_t refs = 0) : ctype(make_table(), false, refs) {}
};
int main()
{
std::string token;
std::string in = "string1tstring2 string3";
std::cout << "locale with modified ctype:n";
std::istringstream s(in);
s.imbue(std::locale(s.getloc(), new CSV_whitespace()));
while (s >> token)
{
std::cout << " " << token << 'n';
}
}
窄
字符流使用表查找进行分类(我认为这是一种优化优势)。您的实现仅适用于 char
以外的字符类型。您可以在C++参考页面上看到他们如何使用该表对字符进行分类。
相关文章:
- C++核心准则 C35 对于接口类"A base class destructor should be either public and virtual, or protected and nonv
- 为什么C++逐位AND运算符在不同大小的操作数中表现为这样
- 为什么 Clang 不允许"and"作为函数名称?
- 位阵列上的快速AND运算
- 是否可以在 C++03 中定义'move-and-swap idiom'等效项
- BoostPython and CMake
- OpenSSL BIO and SSL_read
- Gurobi GRBModel and GRBmodel in C++
- std::visit and std::variant usage
- SHBrowseForFolder with BIF_BROWSEFORCOMPUTER and SHGetPathFr
- Directx12 and keystrokes
- different between int **arr =new int [ n]; and int a[i][j]?
- C++ getenv and setenv
- Inference pytorch C++ with alexnet and cv::imread image
- Visual Studio 2019 C++ and std::filesystem
- 保证逻辑 AND 表达式中的函数调用
- python ctypes and C++ pointers
- C++ const char with .begin() and .end()
- Threads with Classes and std::packaged_task
- Facet ctype, do_is() and specializations