Implementation of 2:string class based on algorithm and data structure

Source: Internet
Author: User

Say less nonsense, reject the prelude, direct orgasm, and look at the code:

String.h

#include <iostream> #include <cstring> #pragma warning (disable:4996) using namespace Std;class string{ public://**************************************************************************//class four functions: constructors, copy constructors, Overloaded assignment operator, destructor//**************************************************************************string (const char* str = NULL); String (const string &str); string& operator= (const String &AMP;STR); ~string ();//******************************************************** Heavy Duty//**************************************************************************string operator+ (const String &str) const; string& operator+= (const string &str), bool operator== (const string &str);char& operator[] (unsigned int index); friend ostream& operator<< (ostream& os, string& str); friend istream& Operator>> ( istream& is, string& str), unsigned short size (), const char* C_STR () const;private:private:char* m_string;};/ / ***********Four functions of a class: constructor, copy constructor, overloaded assignment operator, destructor//************ string::string (const char* str) {if (!str) {m_string = NULL;} else{m_string = new Char[strlen (str) + 1];strcpy (m_string, str);}} string::string (const String &AMP;STR) {if (!str.m_string) {m_string = NULL;} else{m_string = new Char[strlen (str.m_string) + 1];strcpy (m_string, str.m_string);}} string& string::operator= (const String &AMP;STR) {if (This! = &str) {delete[] m_string;if (!str.m_string) {m_ string = NULL;} else{m_string = new Char[strlen (str.m_string) + 1];strcpy (m_string, str.m_string);}} return *this;} String::~string () {delete[] m_string;m_string = NULL;} //heavy-duty//***************************** String string::operator+ (const string &str) const{string newstring ; if (!str.m_string) {NewSTring = *this;} else if (!m_string) {newstring = str;} else{newstring.m_string = new Char[strlen (m_string) + strlen (str.m_string) + 1];strcpy (newstring.m_string, m_string); strcat (newstring.m_string, str.m_string);} return newstring;} string& string::operator+= (const string &str) {String newstring;if (!str.m_string) {newstring = *this;} else if (!m_string) {newstring = str;} else{newstring.m_string = new Char[strlen (m_string) + strlen (m_string) + 1];strcpy (newstring.m_string, m_string); strcat (newstring.m_string, str.m_string);} strcpy (m_string, newstring.m_string); return *this;} BOOL string::operator== (const String &AMP;STR) {if (strlen (m_string)! = strlen (str.m_string)) {return false;} Return strcmp (m_string, str.m_string)? False:true;} char& string::operator[] (unsigned int index) {if (Index >=0 && index <=strlen (m_string)) {return m_ String[index];} Else{cout << "Invalid index:" << index << endl;exit (0);} ostream& operator<< (ostream& OS, string&Amp STR) {OS << str.m_string;return os;} istream& operator>> (istream& is, string& str) {char tmp[1024];is >> tmp;str.m_string = new char[ STRLEN (TMP) + 1];strcpy (str.m_string, TMP); return is;} unsigned short string::size () {return strlen (m_string);} Const char* STRING::C_STR () Const{return m_string;}

Main.cpp

#include "String.h" #include <cstdlib>int main () {String s;cin >> s;cout << s << ":" << s.size () << Endl;char a[] = "Hello", b[] = "World"; String S1 (a), S2 (b), cout << S1 << "+" << s2 << "=" << S1 + s2 << Endl; String s3 = S1 + s2;if (S1 = = S3) {cout << "first:s1 = S3" << Endl;} S1 + = s2;if (S1 = = S3) {cout << "second:s1 = = s3" << Endl;} System ("pause"); return 0;}

Output Result:




Report:

strcpy error in vs2013, solution reference:

http://blog.csdn.net/u010273652/article/details/21320431








Implementation of 2:string class based on algorithm and data structure

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.