C++练习笔记_1
Testing。。。Testing。。。
CopyThat。
嘿嘿,今天下了个LiveWriter ,挺方便的,
就是这个Paste from Visual Studio貌似对中文支持不是太好(ps:在vs下另存为为utf8,问题解决)。。。
对C++的探索真的是永无止境,
对这个String自己还一直以为是小菜一碟,
没想到遇到个重载的问题,乘机写了个cString类,
遇到了不少麻烦,还得谨记,纸上得来终觉浅,绝知此事要躬行!
还有,以后要勤加写博,有益身心!
cString.h
#pragma once
//cString运算符重载实验
//提供多种运算符,及对ANSI和Unicode的支持
//纸上得来终觉浅,绝知此事要躬行!
//Zephyr 2010/11/18
#include "assert.h"
#include <Windows.h>
#define SafeDelete(p) if(p!=NULL){delete p;p=NULL;}
#ifdef UNICODE
#define COUT wcout
#else
#define COUT cout
#endif
class cString
{
public:
cString(TCHAR *pChar=NULL);
cString(cString &str);
~cString();
void operator =(cString &str);
void operator +=(cString &str);
cString operator +(cString &str);
bool operator >(cString &str);
bool operator <(cString &str);
bool operator ==(cString &str);
TCHAR operator [](int i);
int GetLength(){return m_iLength;}
const TCHAR* GetPtr(){return m_pStr;}
void Show();
friend ostream& operator<< (ostream&,cString&);
private:
int m_iLength;
TCHAR *m_pStr;
};
cString::cString(TCHAR *pChar)
{
m_iLength=0;//int ???笨蛋你个笨蛋
m_pStr=NULL;
if(pChar==NULL)
return;
TCHAR *p=pChar;//Bug Nothing!!!!!
while((*p++)!=NULL)
m_iLength++;
m_pStr=new TCHAR[m_iLength+1];
assert(m_pStr!=NULL);
//wcout<<"字符串初始化:"<<pChar<<endl;
//int size=sizeof(TCHAR);
memcpy(m_pStr,pChar,(m_iLength+1)*sizeof(TCHAR));
//wcout<<"串初始化化:"<<m_pStr<<endl;
}
cString::cString(cString &str)
{
if(this!=&str)
{
// if(m_iLength>0)
// SafeDelete(m_pStr);
m_iLength=str.GetLength();
m_pStr=new TCHAR[m_iLength+1];
assert(m_pStr!=NULL);
memcpy(m_pStr,str.m_pStr,(m_iLength+1)*sizeof(TCHAR));
}
}
cString::~cString()
{
SafeDelete(m_pStr);
}
//cString&
void cString::operator =(cString &str)
{
if(this!=&str)
{
SafeDelete(m_pStr);
m_iLength=str.GetLength();
m_pStr=new TCHAR[m_iLength+1];
assert(m_pStr!=NULL);
memcpy(m_pStr,str.m_pStr,(m_iLength+1)*sizeof(TCHAR));
}
//return *this;
}
void cString::operator +=(cString &str)
{
int iOldLength=m_iLength;
int iIter=0;
m_iLength+=str.GetLength();
TCHAR *pNewStr=new TCHAR[m_iLength+1];
while(iIter<iOldLength)
pNewStr[iIter++]=m_pStr[iIter];
int i=0;
while(iIter<m_iLength)
pNewStr[iIter++]=str.m_pStr[i++];
pNewStr[iIter]=NULL;
SafeDelete(m_pStr);
m_pStr=pNewStr;
}
cString cString::operator+(cString &str)
{
cString newStr;
int iIter=0;
newStr. m_iLength=m_iLength+str.GetLength();
TCHAR *pNewStr=new TCHAR[newStr. m_iLength+1];
while(iIter<m_iLength)
pNewStr[iIter++]=m_pStr[iIter];
int i=0;
while(iIter<newStr. m_iLength)
pNewStr[iIter++]=str.m_pStr[i++];
pNewStr[iIter]=NULL;
newStr.m_pStr=pNewStr;
return newStr;
}
bool cString::operator >(cString &str)
{
if(m_iLength>=str.GetLength())
return true;
else
return false;
}
bool cString::operator <(cString &str)
{
if(m_iLength<str.GetLength())
return true;
else
return false;
}
bool cString::operator ==(cString &str)
{
if(m_iLength!=str.GetLength())
return false;
int i=0;
while ((i++)<m_iLength)
{
if(m_pStr[i]!=str.m_pStr[i])
return false;
}
return true;
}
TCHAR cString::operator [](int i)
{
if(i<0||i>m_iLength)
return NULL;
return m_pStr[i];
}
void cString::Show()
{
COUT<<m_pStr<<endl;
}
ostream& operator<<(ostream& os,cString& str)
{
os << str.m_pStr;
return os;
}
Main.cpp
#include <iostream>
using namespace std;
#include "cString.h"
#include <string>
void main()
{
int size=sizeof(char);//char在ANSI和Unicode下不变
int size2=sizeof(TCHAR);
std::COUT.imbue(std::locale("chs"));
TCHAR *wStr=TEXT("I Got it 你呢?");
COUT<<wStr<<endl;
cString p1;
cString p2(TEXT("你们好,我将降临!"));
p1=p2;
// COUT<<"P2 Vaule: "<<p2.GetPtr()<<endl;
// COUT<<"P1 Vaule: "<<p1.GetPtr()<<endl;
cString p3(TEXT("勇Nani"));
TCHAR cStr;
if(p1>p3)
cStr=p3[0];
COUT<<cStr<<endl;
p2+=p3;
cString p4(p2);
//p4(p3);
cString temp=p1+p3;
cout<<p2<<endl;//这个 有点无奈 先放下
p2.Show();
temp.Show();
if(temp==p2)
COUT<<TEXT("我们一样的~")<<endl;
system("pause");
}
代码很简单,就不解释了,纯粹为了试验,呵呵