我想拿出一个区分大小写字符串,我在网上找到了以下无法从不区分大小写的字符串中提取double?
http://www.gotw.ca/gotw/029.htm
所以基本上我的代码拿出一个区分大小写字符串如下
struct ci_char_traits : public std::char_traits<char> {
static bool eq(char c1, char c2)
{ return toupper(c1) == toupper(c2); }
static bool ne(char c1, char c2)
{ return toupper(c1) != toupper(c2); }
static bool lt(char c1, char c2)
{ return toupper(c1) < toupper(c2); }
static int compare(const char* s1, const char* s2, size_t n)
{ return memicmp(s1, s2, n); }
private:
static int memicmp(const void *s1, const void *s2, size_t n) {
if (n != 0) {
const unsigned char *p1 = (const unsigned char *)s1, *p2 = (const unsigned char *)s2;
do {
if (toupper(*p1) != toupper(*p2))
return (*p1 - *p2);
p1++;
p2++;
} while (--n != 0);
}
return 0;
}
};
// case insensitive string type definition
typedef std::basic_string<char, ci_char_traits> ci_string;
// provide standard output for case insensitive string
template<typename char_type, typename traits_type, typename allocator_type>
inline std::basic_ostream<char_type, traits_type>&
operator<<(std::basic_ostream<char_type, traits_type>& os,
const std::basic_string<char_type, ci_char_traits, allocator_type>& str) {
return std::__ostream_insert(os, str.data(), str.size());
}
所以类型定义是字符串。现在我遇到的问题是,我无法使用此自定义字符串来处理函数,因为它使用常规字符串。下面的模板函数从字符串值,但
template <typename T, class string_type, typename counter_type>
T stream_cast(const string_type& s) {
typedef typename string_type::value_type char_type;
typedef typename string_type::traits_type traits_type;
typename std::basic_istringstream<char_type, traits_type> iss(s);
T x;
char c;
if (!(iss >> x) || iss.get(c))
cout<<"*** ERROR *** Bad Conversion!"<<endl;
return x;
}
我调用此函数从字符串中获得双重如下:
ci_string str("2.0");
double test = stream_cast<double>(str);
可是,我是错了我的情况下定义不敏感的字符串,因为通过运算符评估流对象!总是失败(行!(iss >> x)对于此字符串类型始终为真)。
有人知道我为什么会遇到这个问题吗?提前感谢您花时间阅读这篇长篇文章。
AA
我想所有的基本格式化操作只是为'char_traits'定义的,所以那些重载根本不存在。你可以通过使用普通的字符串来处理I/O,并从另一个字符串构造一个字符串(例如'ci_string is(s.begin(),s.end());'等等)。 –
有趣的事情:用VC++ 2010编译的代码按预期运行,但用gcc 4.5.3编译时遇到上述问题。 – Eugene
我只花了2个小时试图找出为什么会发生这种情况。 badbit是为这种类型的字符串设置的,但我不知道为什么。我试图编写我自己的函数basic_istream <_CharT,_Traits> :: _ M_extract,但是在某些时候我停止了,因为我无法访问basic_istream类的受保护成员。 这让我发疯...... =/ – aaragon