C ++ exercise note _ 1

Source: Internet
Author: User
Document directory
  • The code is very simple and I will not explain it. It is purely for experimentation.
C ++ exercise note _ 1 Testing... Testing... CopyThat. Hey hey, today I got a LiveWriter, which is quite convenient. This Paste from Visual Studio seems to be not very good at Chinese support (ps: Save it as utf8 in vs, and solve the problem )... The Exploration of C ++ is really endless. I still thought it was a piece of cake for this String. I did not expect to encounter a problem of heavy load. I wrote a cString class on the plane and encountered a lot of trouble, keep in mind that you have to get a glimpse of the last word on the paper. You must be sure to do it! Also, you must write blogs diligently in the future to benefit your body and mind! CString. h
# Pragma once // cString operator heavy-duty experiment // provides a variety of operators, and supports ANSI and Unicode. // you can get a glimpse of this article! // 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 # endifclass 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 ??? Dumb, you dumb, 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 <"string initialization:" <pChar <endl; // int size = sizeof (TCHAR); memcpy (m_pStr, pChar, (m_iLength + 1) * sizeof (TCHAR); // wcout <"string initialization:" <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 = pNe WStr;} 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 does not change int size2 = sizeof (TCHAR) under ANSI and Unicode; std :: COUT. imbue (std: locale ("chs"); TCHAR * wStr = TEXT ("I Got it what about you? "); COUT <wStr <endl; cString p1; cString p2 (TEXT (" hello, I will come! "); P1 = p2; // COUT <" P2 Vaule: "<p2.GetPtr () <endl; // COUT <" P1 Vaule: "<p1.GetPtr () <endl; cString p3 (TEXT (" Yong 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; // you have no choice but to put p2.Show (); temp. show (); if (temp = p2) COUT <TEXT ("our same ~ ") <Endl; system (" pause ");}
The code is very simple and I will not explain it. It is purely for experimentation.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.