代码改变世界

随笔分类 -  C++

转: isspace 函数中文报错

2012-06-15 13:23 by youxin, 1087 阅读, 收藏, 编辑
摘要: 当用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, 7744 阅读, 收藏, 编辑
摘要: 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, 367 阅读, 收藏, 编辑
摘要: 新手经常写这样的代码:#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, 2560 阅读, 收藏, 编辑
摘要: 可以用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, 4407 阅读, 收藏, 编辑
摘要: 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, 5170 阅读, 收藏, 编辑
摘要: 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, 1844 阅读, 收藏, 编辑
摘要: 看一下自定义类型如何在函数头返回类定义的类型: 一个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, 408 阅读, 收藏, 编辑
摘要: 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, 934 阅读, 收藏, 编辑
摘要: 在函数参数中数组已经退化为指针。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, 2986 阅读, 收藏, 编辑
摘要: 先从简单说起,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 阅读全文

c++ string c_str() 和data()区别

2012-06-12 21:44 by youxin, 8354 阅读, 收藏, 编辑
摘要: 看下面的英文解释:const char* c_str ( ) const;Get C string equivalentGenerates a null-terminated sequence of characters (c-string) with the same content as the string object and returns it as a pointer to an array of characters.A terminating null character is automatically appended.const char* data() const;. 阅读全文

转:C++ 数组引用

2012-06-12 21:40 by youxin, 927 阅读, 收藏, 编辑
摘要: 如下定义就得到一个数组的引用 类型名 (&变量明)[N]; 实例 int int_array[10]; int (&int_ref)[10] = int_array; 这样就得到了一个数组的应用 在函数的声明中用数组的引用定义,就不怕数组退化了。如下 for_each( int (&int_ref)[10] ) { for( int i=0; i#includetypedef char Char10[10];void func(Char10 &a)//char (&a)[10]){ printf("%d\n",sizeof(a)); 阅读全文

C++函数默认参数

2012-06-12 21:35 by youxin, 288 阅读, 收藏, 编辑
摘要: 在定义参数的时候同时给它一个初始值。void Func(int i = 1, float f = 2.0f, double d = 3.0){ cout << i << ", " << f << ", " << d << endl ;}int main(void){ Func() ; // 1, 2, 3 Func(10) ; // 10, 2, 3 Func(10, 20.0f) ; // 10, 20, 3 Func(10, 20.0f, 30.0) ; // 10, 20, 3 阅读全文

c++ 复制+赋值+析构

2012-06-08 21:51 by youxin, 433 阅读, 收藏, 编辑
摘要: 对于需要管理资源(譬如内存)的类,一般均需要自行定义复制、赋值、析构函数,以确保正确、合理的语义和对资源的妥善管理。T::T(); //one or more constructors, perhaps with arguments T::~T() // the destructor T::T(const T&) // the copy constructor T::operator=(const T&) // the assignment operator构造函数、析构函数与赋值函数是每个类最基本的函数。每个类只有一个析构函数,但可以有多个构造函数(包含一个拷贝构造函数,其它 阅读全文

编写Vec类

2012-06-08 21:15 by youxin, 474 阅读, 收藏, 编辑
摘要: 上面的解释了为什么两个函数仅仅返回值不同为什么还能重载。对象的复制隐式地复制double d;vector<int> vi;d = median(vi); // copy vi into the parameter in median string line;vector<string> words = split(line); // copy the return value from split into words 显式的复制vector<Student_info> vs;vector<Student_info> v2 = vs; // c 阅读全文

c++ 操作符重载

2012-06-08 21:02 by youxin, 962 阅读, 收藏, 编辑
摘要: 操作符函数的名字: operator 后跟着操作符的符号(其间可以有空格)操作符函数的参数的个数: 如果操作符函数是非成员函数,参数个数就是操作数的个数;对于二元操作符,第一个参数为左操作数,第二个参数为右操作数。如果操作符函数为成员函数,参数个数比操作数个数少1;因为调用此操作符函数的对象默认... 阅读全文

c++3种内存管理方式

2012-06-08 20:08 by youxin, 270 阅读, 收藏, 编辑
摘要: 自动内存管理: 系统在运行时遇到局部变量的定义则为此变量分配内存空间,在退出包含此变量定义的语句块时释放对应于此变量的内存空间。注意:严禁使用指向被销毁对象的指针,同理,也不可使用被销毁对象的引用。静态内存分配: 静态对象被分配且仅被分配一次内存空间,并且此对象的生存期与程序的运行期相同。动态内存分配: 自行掌控对象的生成和销毁T* ptr = new T;为T类型的对象分配内存,并进行默认初始化,生成指向此无名对象的指针。new T(initializer); //使用initilizer初始化新生成的对象此对象会一直存在,直至程序结束(内存泄露!)或被主动销毁 delete ptr;例:i 阅读全文

c++内联函数

2012-06-08 19:57 by youxin, 1061 阅读, 收藏, 编辑
摘要: 内联函数定义 内联函数使用inline关键字定义, 并且函数体和申明必须结合在一起, 否则编译器将他作为普通函数对待。inline void function(int x); //仅仅是申明函数,没有任何效果inline void function(int x) //正确 { return ++x; }在类内部定义的函数自动的为内联函数, 不需要加关键字inline。class point { int i; public: void SetValue(int x) //内联函数 { i = x; ... 阅读全文

c++ allocator类

2012-06-07 13:25 by youxin, 1095 阅读, 收藏, 编辑
摘要: 头文件提供了一个类allocator,它可以分配用来保存T类型的对象的整块内存,而不需要初始化,并且他会返回一个指针,指向这块内存的首元素。这样的指针是危险的,因为他们的类型表面指向的是对象,但是这些内存却并不包含真正的对象。标准库还提供了一种方式在这些内存中构造对象,销毁对象--但是并没有释放内... 阅读全文

转:c/c++里的 堆区 栈区 静态区 文字常量区 程序代码区

2012-05-28 00:06 by youxin, 530 阅读, 收藏, 编辑
摘要: 任何一个运行中的程序,在内存中都被分为代码区和数据区2部分,而数据区又被分为静态存储区,栈区等等。一个由C/C++编译的程序占用的内存分为以下几个部分 1、栈区(stack)—由编译器自动分配释放 ,存放函数的参数值,局部变量的值等。其操作方式类似于数据结构中的栈。 栈(stack):是自动分配变量,以及函数调用所使用的一些空间(所谓的局部变量),地址由高向低减少; 堆(heap):由malloc,new等分配的空间的地址,地址由低向高增长(程序员释放)。2、堆区(heap) —一般由程序员分配释放, 若程序员不释放,程序结束时可能由OS回收。注意它与数据结构中的堆是两回事,分配方式... 阅读全文
上一页 1 ··· 5 6 7 8 9 10 11 下一页