C++专题之string和char *的简单比较使用
来源:互联网 发布:mysql安装配置 编辑:程序博客网 时间:2024/04/27 17:51
转载请注明出处:http://blog.csdn.net/feng1790291543
当编写C++类,使用string时,我们注意到,在头文件处,我们要#include<string> (不加.h,如果加了,就成了给C语言加头文件了),别忘了,还有using namespace std;不然就会出现很多错误,这可能是初学者容易犯的错误,下面附上代码。
Student.h
// Student.h: interface for the CStudent class.////////////////////////////////////////////////////////////////////////#if !defined(AFX_STUDENT_H__96794BEB_7938_416F_A48A_1E028AE6E7D4__INCLUDED_)#define AFX_STUDENT_H__96794BEB_7938_416F_A48A_1E028AE6E7D4__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000#include <string>using namespace std;class CStudent {public:int number;string name; //避免使用* 去分配内存,此时的name的长度是无限的,克服*name的缺陷 //char *name; CStudent();virtual ~CStudent();CStudent(int number,string name);void print_student(); CStudent(CStudent &stu);CStudent &operator=(CStudent &stu);};#endif // !defined(AFX_STUDENT_H__96794BEB_7938_416F_A48A_1E028AE6E7D4__INCLUDED_)
Student.cpp
// Student.cpp: implementation of the CStudent class.////////////////////////////////////////////////////////////////////////#include "Student.h"#include <iostream>#include <string>#include <stdlib.h>using namespace std;//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////CStudent::CStudent(){}CStudent::~CStudent(){}CStudent::CStudent(int number,string name){ this->number=number;this->name=name;return ;}void CStudent::print_student(){cout<<"number is "<<this->number<<" name is "<<this->name<<endl;return ;}CStudent::CStudent(CStudent &stu) //拷贝构造函数{this->number=stu.number;this->name=stu.name; //深复制 ----->string name; 本身就是相当于数组,故直接将字符串复制了 //this->name=stu.name; //浅拷贝 --->char *name; 只是复制指针的本身,而没有复制指针指向的目标 //this->name=new char[strlen(stu.name)+1]; //char *name; ---->解决浅拷贝,将指针的目标复制了//strcpy(this->name,stu.name); return ;}CStudent &CStudent::operator=(CStudent &stu) //当string name;的时候的情况分析------->赋值函数,运算符重载 { if(&stu==this){ return *this; }this->name=stu.name; //明显不用分配内存,而直接进行深复制 this->number=stu.number; return *this;}/*CStudent &CStudent::operator=(CStudent &stu) //当char *name;的时候的情况分许------->赋值函数,运算符重载 { if(&stu==this){ return *this;}this->number=stu.number; char *name_bak=new char[strlen(stu.name)+1]; //分配内存,还要备份name的值,避免空间分配失败 if(name_bak==NULL){return *this;}name_bak=this->name;free(this->name); //释放原有的空间,使其指向新的内存区域this->name=name_bak;strcpy(this->name,stu.name); //深拷贝,解决复制问题,return *this;}*//********************************************* * 比较上述的string 和char *,可以知道 ** string 比较方便实用,减少了很多繁琐的问题 ** * *********************************************/
main.cpp
#include "Student.h" #include <iostream>#include <string>#include <stdlib.h>//带.h的是C语言中的头文件using namespace std;int main(){ string nu="快跑,草泥马!";cout<<nu<<endl;CStudent stu(1001,"孙权");stu.print_student();CStudent stu2(1002,"草泥马"); stu2.print_student();stu.name=stu.name+"——"+stu2.name;//字符串连接stu.print_student(); CStudent stu3(stu2); //拷贝构造函数使用stu3.print_student();CStudent stu4; stu4.operator =(stu); //赋值构造函数使用stu4.print_student();return 0;}
0 0
- C++专题之string和char *的简单比较使用
- char,string和CString 之间的比较
- string 和 const char* 的性能比较
- [C/C++] char[]和char*变量的==比较
- char 和 string 的简单区别
- char*数组(c类型字符串)和c++中的string的转换、字符串比较、char*数组等问题
- char*数组(c类型字符串)和c++中的string的转换、字符串比较、char*数组等问题
- CString,string 和 Char*比较
- CString,string 和 Char*比较
- CString string 和 Char* 比较
- CString string 和 Char* 比较
- CString,string 和 Char*比较
- CString,string 和 Char*比较
- char*、char[]、string 之间的 转换、比较
- CString,String,Char*的比较
- string,CString,char *的比较
- string字符串和char字符串的使用
- C++ char和string的使用
- Oracle中随机抽取N条记录
- asa 防火墙基本配置管理
- IIS32位,64位模式下切换
- C++中 内联函数(关键字:inline)
- Chapter3 Sharing data between threads
- C++专题之string和char *的简单比较使用
- 在GNU/Linux中查看端口占用情况的命令-lsof、netstat
- 2-sat 问题学习小记 Poj 3207 Ikki's Story IV - Panda's Trick (可行性判定)
- 从决策树学习谈到贝叶斯分类算法、EM、HMM
- 变量的存储方式与生存期
- 前序 开始写博客
- 讲故事学设计模式-状态(State)模式
- C++ map的基本操作和使用
- SQLite数据库的增删改查