我派生了ctype
类来构建自己的构面,以便覆盖其虚拟函数do_is()
。我的目的是使流提取器忽略空格字符(并仍然在制表符上标记化)。此重载调用母类的实现。但是只能使用wchar_t
进行编译。 ctype::do_is()
模板值没有char
的实现。对于gcc和VS 2010来说确实如此。
这是我的代码;您只需要取消注释第五行即可在两个版本之间进行测试。
#include <iostream>
#include <locale>
#include <sstream>
// #define WIDE_CHARACTERS
#ifdef WIDE_CHARACTERS
typedef wchar_t CharacterType;
std::basic_string<CharacterType> in = L"string1\tstring2 string3";
std::basic_ostream<CharacterType>& consoleOut = std::wcout;
#else
typedef char CharacterType;
std::basic_string<CharacterType> in = "string1\tstring2 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 = "string1\tstring2 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';
}
}
关于c++ - 方面ctype,do_is()和特化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22873106/