摘要:当用isspace()判断中文时,会出现 (unsigned)(c + 1) <= 256);” 错误。解释: 试图用locale为ASCII的isspace来判断GBK即中文编码的空格,对么?如果我理解正确的话,那么这不是VC的问题,而是使用上的问题。 对于C++而言,应该使用isspace(ch, loc); 这个版本,loc是类型为std::locale的变量,如果你想判断GBK的空格,那么让loc是GBK的locale,然后这个函数就正常了。你现在使用的是C的isspace(ch)函数,这个函数使用的是默认的全局locale,你把这个全局的设为GBK,也应该可以解决这个问题。总之 阅读全文
c++ 错误:n个重载没有“this”指针的合法转换
2012-06-15 13:18 by youxin, 7826 阅读, 1 推荐, 收藏,
摘要:msdn解释:“function”: number 重载没有“this”指针的合法转换编译器未能将this转换为该成员函数的任何重载版本。此错误可能是由调用const对象的非const成员函数引起的。可能的解决方案:从对象声明中移除const。将const添加到成员函数重载之一。下面的示例生成 C2663:// C2663.cppstruct C { void f() volatile {} void f() {}};struct D { void f() volatile; void f() const {}};const C *pcc;const D *pcd;int ma... 阅读全文
c++ vector subscript of range
2012-06-15 13:11 by youxin, 377 阅读, 0 推荐, 收藏,
摘要:新手经常写这样的代码:#include<iostream>#include<string>#include<vector>using namespace std;void main(){vector<string> a;a[0]="aaaaaa";a[1]="xxxxxx";cout<<a.size()<<endl;} 错误很明显,没有开辟空间就直接放东西了。可以用push_back() 或resize()来解决。vector<string> s;s.erase(s.en 阅读全文
c++ string 替换
2012-06-15 00:55 by youxin, 2646 阅读, 0 推荐, 收藏,
摘要:可以用string的成员函数replace 或 算法库里的replace函数。string& replace ( size_t pos1, size_t n1, const string& str );还有很多的函数参数形式。用replace之前可以用string 成员函数find找到你想替换的字符串。string::findsize_t find ( const string& str, size_t pos = 0 ) const;size_t find ( const char* s, size_t pos, size_t n ) const;size_t fin 阅读全文
c++ string 转int,float
2012-06-15 00:16 by youxin, 4420 阅读, 0 推荐, 收藏,
摘要:std::string为library type,而int、double为built-in type,两者无法利用(int)或(double)的方式互转,法1:使用C的atoi()與atof()。先利用c_str()轉成C string,再用atoi()與atof()。int atoi ( const char * str ); //c++ 头文件cstdlib c stdlib.hConvert string to integerParses the C stringstrinterpreting its content as an integral number, which is ret 阅读全文
c++ 用setlocale 设置 cout 输出中文
2012-06-14 22:03 by youxin, 5372 阅读, 0 推荐, 收藏,
摘要:char * setlocale ( int category, const char * locale );Set or retrieve localeSets locale information to be used by the current program, either changing the entire locale or parts of it. The function can also be used to retrieve the current locale's name by passingNULLas the value forlocale.Local 阅读全文
C++ typename 用法
2012-06-13 09:33 by youxin, 1875 阅读, 1 推荐, 收藏,
摘要:看一下自定义类型如何在函数头返回类定义的类型: 一个vec初略模板:template <class T> class Vec {public: typedef T* iterator; typedef const T* const_iterator; typedef size_t size_type; typedef T value_type; typedef T& reference; typedef const T& const_reference; iterator erase(iterator it); ....... ..... 阅读全文
转:const char*, char const*, char*const的区别
2012-06-12 22:07 by youxin, 416 阅读, 0 推荐, 收藏,
摘要:const char*, char const*, char*const的区别问题几乎是C++面试中每次都会有的题目。事实上这个概念谁都有,只是三种声明方式非常相似很容易记混。Bjarne在他的The C++ Programming Language里面给出过一个助记的方法:把一个声明从右向左读。char * const cp; ( * 读成 pointer to )cp is a const pointer to charconst char * p;p is a pointer to const char;char const * p;同上因为C++里面没有const*的运算符,所以con 阅读全文
C++在函数中求数组大小
2012-06-12 22:02 by youxin, 951 阅读, 1 推荐, 收藏,
摘要:在函数参数中数组已经退化为指针。int size(int a[] ){ return sizeof(a)/sizeof(*a); 输出恒为1 }可以利用函数模板,看下面一个简单例子:#include <iostream>template <typename T, ::size_t N>void f (T (&) [N]){ std::cout << N << std::endl;}struct test { };int main (){ int i [5]; double d [10]; test t [20]; f(i); f(d); 阅读全文
c++ const char* 和char * 的相互转化
2012-06-12 21:57 by youxin, 3126 阅读, 0 推荐, 收藏,
摘要:先从简单说起,char *转 const char *char *a="hello";const char *b=a;可见,直接赋值即可。const char * 转换为 char *指向const的指针不能被赋给指向非const的指针,所以应该用strcpy,也就是另开一块内存,把字符一个个复制过去const char *expr = "goodidea";char *buf = new char[strlen(expr)+1];strcpy(buf, expr);strcpy 原型:extern char *strcpy(char *dest,cha 阅读全文
浙公网安备 33010602011771号