C++ string类实现

自学c++一个月了,在VS2017上写了一下string类的实现,中间还是遇到了一些麻烦,还好差不多解决了,也学习到了空指针在特例中单独讨论的情况.
//MString.h
#pragma once
#ifndef _MSTRING_
#define _MSTRING_

#include <iostream>

class MString
{
private:
	char * _str;
	int _len;
public:
	MString(const char * str = NULL);
	MString(const MString & ms);
	MString(int n);
	int length() const;

	MString operator+(const MString & ms) const;
	MString operator+(const char * str) const;
	MString & operator=(const MString & ms);

	friend int MStringLen(const MString & ms);
	friend MString operator+(const char * str, const MString & ms);
	friend std::ostream & operator<<(std::ostream & os, MString & ms);
	friend std::istream & operator>>(std::istream & is, MString & ms);

	bool operator>(const MString & ms) const;
	bool operator>=(const MString & ms) const;
	bool operator<(const MString & ms) const;
	bool operator<=(const MString & ms) const;
	bool operator!=(const MString & ms) const;
	bool operator==(const MString & ms) const;

	~MString();
};

#endif

//MString.cpp
#include "stdafx.h"
#include "MString.h"


MString::MString(const char * str)
{
	if (str == nullptr || str == "")
	{
		_len = 0;
		_str = new char[1];
		strcpy_s(_str, _len + 1, "");
	}
	else
	{
		_len = strlen(str);
		_str = new char[_len + 1];
		strcpy_s(_str, _len + 1, str);
	}
//	std::cout << _str << " at " << (int *)_str << " created~" << std::endl;
}

MString::MString(const MString & ms)
{
	_len = ms._len;
	_str = new char[_len + 1];
	strcpy_s(_str, _len + 1, ms._str);
	//	std::cout << _str << " at " << (int *)_str << " copied~" << std::endl;
}


MString::~MString()
{
//	std::cout << _str << " at " << (int *)_str << " deleted~" << std::endl;
	_len = 0;
	delete[] _str;
}

MString::MString(int n)
{
	delete[] _str;
	_len = n;
	_str = new char[_len + 1];
	for (int i = 0; i < _len; i++)
		_str[i] = '0';
	_str[_len] = '\0';
}

int MString::length() const
{
	return _len;
}

MString MString::operator+(const MString & ms) const
{
	MString Temp;
	delete[] Temp._str;
	Temp._len = _len + ms._len;
	Temp._str = new char[Temp._len + 1];
	strcpy_s(Temp._str, Temp._len + 1, _str);
	strcat_s(Temp._str, Temp._len + 1, ms._str);
	return Temp;
}

MString MString::operator+(const char * str) const
{
	if (str)
	{
		MString Temp;
		delete[] Temp._str;
		Temp._len = _len + strlen(str);
		Temp._str = new char[Temp._len + 1];
		strcpy_s(Temp._str, Temp._len + 1, _str);
		strcat_s(Temp._str, Temp._len + 1, str);
		return Temp;
	}
	else
		return *this;
}

MString & MString::operator=(const MString & ms)
{
	delete[] _str;
	_len = ms._len;
	_str = new char[_len + 1];
	strcpy_s(_str, _len + 1, ms._str);
	return *this;
}

int MStringLen(const MString & ms)
{
	return ms._len;
}

MString operator+(const char * str, const MString & ms)
{
	if (str)
	{
		MString Temp;
		delete[] Temp._str;
		Temp._len = strlen(str) + ms._len;
		Temp._str = new char[Temp._len + 1];
		strcpy_s(Temp._str, Temp._len + 1, str);
		strcat_s(Temp._str, Temp._len + 1, ms._str);
		return Temp;
	}
	else
		return ms;
}

std::ostream & operator<<(std::ostream & os, MString & ms)
{
	os << ms._str;
	return os;
}

std::istream & operator>>(std::istream & is, MString & ms)
{
	delete[] ms._str;
	ms._str = new char[20];
	is.getline(ms._str, 20);
	ms._len = strlen(ms._str);
	return is;
}

bool MString::operator>(const MString & ms) const
{
	return strcmp(_str, ms._str) > 0;
}

bool MString::operator>=(const MString & ms) const
{
	return strcmp(_str, ms._str) >= 0;
}

bool MString::operator<(const MString & ms) const
{
	return strcmp(_str, ms._str) < 0;
}

bool MString::operator<=(const MString & ms) const
{
	return strcmp(_str, ms._str) <= 0;
}

bool MString::operator!=(const MString & ms) const
{
	return strcmp(_str, ms._str) != 0;
}

bool MString::operator==(const MString & ms) const
{
	return strcmp(_str, ms._str) == 0;
}
#include <stdafx>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
#include <cstring>
#include <conio.h>
#include <windows.h>
#include <time.h>
#include <stdlib.h>

using namespace std;

/* ----main函数不准动---- */
int main()
{
	if (1) {
		MString s1;                  //s1为NULL
		MString s2("teststr2");      //s2为"teststr2"
		MString s3 = "teststr3";       //s3为"teststr3"
		MString s4 = nullptr;             //s4为NULL
		MString s5 = "";               //s5为NULL

		cout << "定义对象并初始化测试(NULL及字符串常量初始化)" << endl;

		cout << "s1应该是<NULL>,  实际输出:" << s1 << endl;
		cout << "s2应该是teststr2,实际输出:" << s2 << endl;
		cout << "s3应该是teststr3,实际输出:" << s3 << endl;
		cout << "s4应该是<NULL>,  实际输出:" << s4 << endl;
		cout << "s5应该是<NULL>,  实际输出:" << s5 << endl;

		cout << endl << "任意键继续..." << endl;
		_getch();
		cout << endl;
	}

	if (1) {
		MString s0 = "teststr1";
		char *str1 = "teststr2";
		char  str2[] = "teststr3";
		char *str3 = nullptr;
		char  str4[] = "";
		MString s1 = s0, s2 = str1, s3 = str2, s4 = str3, s5 = str4;

		cout << "定义对象并初始化测试(MString对象及字符指针、字符数组)" << endl;

		cout << "s1应该是teststr1,实际输出:" << s1 << endl;
		cout << "s2应该是teststr2,实际输出:" << s2 << endl;
		cout << "s3应该是teststr3,实际输出:" << s3 << endl;
		cout << "s4应该是<NULL>,  实际输出:" << s4 << endl;
		cout << "s5应该是<NULL>,  实际输出:" << s5 << endl;

		cout << endl << "任意键继续..." << endl;
		_getch();
		cout << endl;
	}

	if (1) {
		MString s1;

		cout << "重载cin测试" << endl;

		cout << "请在键盘上输入Hello" << endl;
		cin >> s1;
		cout << "s1应该是Hello,实际输出:" << s1 << endl;

		cout << "请在键盘上输入Hello 123" << endl;
		cin >> s1;
		cout << "s1应该是Hello,实际输出:" << s1 << endl;

		cout << endl << "任意键继续..." << endl;
		_getch();
		cout << endl;
	}

	if (1) {
		MString s1("Hello"), s2;

		cout << "赋值操作测试" << endl;

		cout << "s1应该是Hello, 实际输出:" << s1 << endl;

		s2 = s1;
		cout << "s2应该是Hello, 实际输出:" << s2 << endl;

		s1 = "Hi";
		cout << "s1应该是Hi,    实际输出:" << s1 << endl;

		s2 = "";
		cout << "s2应该是<NULL>,实际输出:" << s2 << endl;

		s1 = nullptr;
		cout << "s1应该是<NULL>,实际输出:" << s1 << endl;

		cout << endl << "任意键继续..." << endl;
		_getch();
		cout << endl;
	}

	if (1) {
		MString s0, s1("tong"), s2("ji"), s3;

		cout << "连接(+)测试(两个MString类)" << endl;

		s3 = s1 + s2;
		cout << "s3应为tongji,实际输出:" << s3 << endl;

		s3 = s2 + s1;
		cout << "s3应为jitong,实际输出:" << s3 << endl;

		s3 = s1 + s0;
		cout << "s3应为tong,  实际输出:" << s3 << endl;

		s3 = s0 + s2;
		cout << "s3应为ji,    实际输出:" << s3 << endl;

		cout << endl << "任意键继续..." << endl;
		_getch();
		cout << endl;
	}

	if (1) {
		MString s1("tong"), s2, s3;

		cout << "连接(+)测试(MString类和字符串常量)" << endl;

		s3 = s1 + "ji";
		cout << "s3应为tongji,实际输出:" << s3 << endl;

		s3 = "ji" + s1;
		cout << "s3应为jitong,实际输出:" << s3 << endl;

		s3 = s1 + "";
		cout << "s3应为tong,  实际输出:" << s3 << endl;

		s3 = s1 + nullptr;
		cout << "s3应为tong,  实际输出:" << s3 << endl;

		s3 = "" + s1;
		cout << "s3应为tong,  实际输出:" << s3 << endl;

		s3 = nullptr + s1;
		cout << "s3应为tong,  实际输出:" << s3 << endl;

		s3 = s2 + nullptr;
		cout << "s3应为<NULL>,实际输出:" << s3 << endl;

		s3 = nullptr + s2;
		cout << "s3应为<NULL>,实际输出:" << s3 << endl;

		s3 = s2 + "";
		cout << "s3应为<NULL>,实际输出:" << s3 << endl;

		s3 = "" + s2;
		cout << "s3应为<NULL>,实际输出:" << s3 << endl;

		cout << endl << "任意键继续..." << endl;
		_getch();
		cout << endl;
	}

	if (1) {
		MString s1("tong"), s3;
		char *str1 = "ji", *str2 = "", *str3 = nullptr;

		cout << "连接(+)测试(MString类和字符指针)" << endl;

		s3 = s1 + str1;
		cout << "s3应为tongji,实际输出:" << s3 << endl;

		s3 = str1 + s1;
		cout << "s3应为jitong,实际输出:" << s3 << endl;

		s3 = s1 + str2;
		cout << "s3应为tong,  实际输出:" << s3 << endl;

		s3 = str2 + s1;
		cout << "s3应为tong,  实际输出:" << s3 << endl;

		s3 = s1 + str3;
		cout << "s3应为tong,  实际输出:" << s3 << endl;

		s3 = str3 + s1;
		cout << "s3应为tong,  实际输出:" << s3 << endl;

		cout << endl << "任意键继续..." << endl;
		_getch();
		cout << endl;
	}

	if (1) {
		MString s1("tong"), s3;
		char str1[] = "ji", str2[] = "";

		cout << "连接(+)测试(MString类和字符数组)" << endl;

		s3 = s1 + str1;
		cout << "s3应为tongji,实际输出:" << s3 << endl;

		s3 = str1 + s1;
		cout << "s3应为jitong,实际输出:" << s3 << endl;

		s3 = s1 + str2;
		cout << "s3应为tong,  实际输出:" << s3 << endl;

		s3 = str2 + s1;
		cout << "s3应为tong,  实际输出:" << s3 << endl;

		cout << endl << "任意键继续..." << endl;
		_getch();
		cout << endl;
	}

	if (1) {
		MString s1 = "house", s2 = "horse", s3 = "house", s4 = "", s5 = nullptr;

		cout << "比较运算测试(两个MString类)" << endl;

		cout << "串1=" << s1 << " 串2=" << s2 << " > 结果应为1,实际:" << (s1 >  s2) << endl;
		cout << "串1=" << s1 << " 串2=" << s2 << " >=结果应为1,实际:" << (s1 >= s2) << endl;
		cout << "串1=" << s1 << " 串2=" << s2 << " < 结果应为0,实际:" << (s1 <  s2) << endl;
		cout << "串1=" << s1 << " 串2=" << s2 << " <=结果应为0,实际:" << (s1 <= s2) << endl;
		cout << "串1=" << s1 << " 串2=" << s2 << " ==结果应为0,实际:" << (s1 == s2) << endl;
		cout << "串1=" << s1 << " 串2=" << s2 << " !=结果应为1,实际:" << (s1 != s2) << endl;

		cout << "串1=" << s1 << " 串3=" << s3 << " > 结果应为0,实际:" << (s1 >  s3) << endl;
		cout << "串1=" << s1 << " 串3=" << s3 << " >=结果应为1,实际:" << (s1 >= s3) << endl;
		cout << "串1=" << s1 << " 串3=" << s3 << " < 结果应为0,实际:" << (s1 <  s3) << endl;
		cout << "串1=" << s1 << " 串3=" << s3 << " <=结果应为1,实际:" << (s1 <= s3) << endl;
		cout << "串1=" << s1 << " 串3=" << s3 << " ==结果应为1,实际:" << (s1 == s3) << endl;
		cout << "串1=" << s1 << " 串3=" << s3 << " !=结果应为0,实际:" << (s1 != s3) << endl;

		cout << "串1=" << s1 << " 串4=" << s4 << " > 结果应为1,实际:" << (s1 >  s4) << endl;
		cout << "串1=" << s1 << " 串4=" << s4 << " >=结果应为1,实际:" << (s1 >= s4) << endl;
		cout << "串1=" << s1 << " 串4=" << s4 << " < 结果应为0,实际:" << (s1 <  s4) << endl;
		cout << "串1=" << s1 << " 串4=" << s4 << " <=结果应为0,实际:" << (s1 <= s4) << endl;
		cout << "串1=" << s1 << " 串4=" << s4 << " ==结果应为0,实际:" << (s1 == s4) << endl;
		cout << "串1=" << s1 << " 串4=" << s4 << " !=结果应为1,实际:" << (s1 != s4) << endl;

		cout << "串1=" << s1 << " 串5=" << s5 << " > 结果应为1,实际:" << (s1 >  s5) << endl;
		cout << "串1=" << s1 << " 串5=" << s5 << " >=结果应为1,实际:" << (s1 >= s5) << endl;
		cout << "串1=" << s1 << " 串5=" << s5 << " < 结果应为0,实际:" << (s1 <  s5) << endl;
		cout << "串1=" << s1 << " 串5=" << s5 << " <=结果应为0,实际:" << (s1 <= s5) << endl;
		cout << "串1=" << s1 << " 串5=" << s5 << " ==结果应为0,实际:" << (s1 == s5) << endl;
		cout << "串1=" << s1 << " 串5=" << s5 << " !=结果应为1,实际:" << (s1 != s5) << endl;

		cout << endl << "任意键继续..." << endl;
		_getch();
		cout << endl;
	}

	if (1) {
		MString s1 = "house";

		cout << "比较运算测试(MString类和字符串常量)" << endl;

		cout << "串1=" << s1 << " 常量=horse" << " > 结果应为1,实际:" << (s1 >  "horse") << endl;
		cout << "串1=" << s1 << " 常量=horse" << " >=结果应为1,实际:" << (s1 >= "horse") << endl;
		cout << "串1=" << s1 << " 常量=horse" << " < 结果应为0,实际:" << (s1 <  "horse") << endl;
		cout << "串1=" << s1 << " 常量=horse" << " <=结果应为0,实际:" << (s1 <= "horse") << endl;
		cout << "串1=" << s1 << " 常量=horse" << " ==结果应为0,实际:" << (s1 == "horse") << endl;
		cout << "串1=" << s1 << " 常量=horse" << " !=结果应为1,实际:" << (s1 != "horse") << endl;

		cout << "串1=" << s1 << " 常量=house" << " > 结果应为0,实际:" << (s1 >  "house") << endl;
		cout << "串1=" << s1 << " 常量=house" << " >=结果应为1,实际:" << (s1 >= "house") << endl;
		cout << "串1=" << s1 << " 常量=house" << " < 结果应为0,实际:" << (s1 <  "house") << endl;
		cout << "串1=" << s1 << " 常量=house" << " <=结果应为1,实际:" << (s1 <= "house") << endl;
		cout << "串1=" << s1 << " 常量=house" << " ==结果应为1,实际:" << (s1 == "house") << endl;
		cout << "串1=" << s1 << " 常量=house" << " !=结果应为0,实际:" << (s1 != "house") << endl;

		cout << "串1=" << s1 << " 常量=" << " > 结果应为1,实际:" << (s1 >  "") << endl;
		cout << "串1=" << s1 << " 常量=" << " >=结果应为1,实际:" << (s1 >= "") << endl;
		cout << "串1=" << s1 << " 常量=" << " < 结果应为0,实际:" << (s1 <  "") << endl;
		cout << "串1=" << s1 << " 常量=" << " <=结果应为0,实际:" << (s1 <= "") << endl;
		cout << "串1=" << s1 << " 常量=" << " ==结果应为0,实际:" << (s1 == "") << endl;
		cout << "串1=" << s1 << " 常量=" << " !=结果应为1,实际:" << (s1 != "") << endl;

		cout << "串1=" << s1 << " 常量=<NULL>" << " > 结果应为1,实际:" << (s1 >  nullptr) << endl;
		cout << "串1=" << s1 << " 常量=<NULL>" << " >=结果应为1,实际:" << (s1 >= nullptr) << endl;
		cout << "串1=" << s1 << " 常量=<NULL>" << " < 结果应为0,实际:" << (s1 <  nullptr) << endl;
		cout << "串1=" << s1 << " 常量=<NULL>" << " <=结果应为0,实际:" << (s1 <= nullptr) << endl;
		cout << "串1=" << s1 << " 常量=<NULL>" << " ==结果应为0,实际:" << (s1 == nullptr) << endl;
		cout << "串1=" << s1 << " 常量=<NULL>" << " !=结果应为1,实际:" << (s1 != nullptr) << endl;

		cout << endl << "任意键继续..." << endl;
		_getch();
		cout << endl;
	}

	if (1) {
		MString s1 = "house";
		char *str2 = "horse", *str3 = "house", *str4 = "", *str5 = nullptr;

		cout << "比较运算测试(MString类和字符指针)" << endl;

		cout << "串1=" << s1 << " 串2=" << str2 << " > 结果应为1,实际:" << (s1 >  str2) << endl;
		cout << "串1=" << s1 << " 串2=" << str2 << " >=结果应为1,实际:" << (s1 >= str2) << endl;
		cout << "串1=" << s1 << " 串2=" << str2 << " < 结果应为0,实际:" << (s1 <  str2) << endl;
		cout << "串1=" << s1 << " 串2=" << str2 << " <=结果应为0,实际:" << (s1 <= str2) << endl;
		cout << "串1=" << s1 << " 串2=" << str2 << " ==结果应为0,实际:" << (s1 == str2) << endl;
		cout << "串1=" << s1 << " 串2=" << str2 << " !=结果应为1,实际:" << (s1 != str2) << endl;

		cout << "串1=" << s1 << " 串3=" << str3 << " > 结果应为0,实际:" << (s1 >  str3) << endl;
		cout << "串1=" << s1 << " 串3=" << str3 << " >=结果应为1,实际:" << (s1 >= str3) << endl;
		cout << "串1=" << s1 << " 串3=" << str3 << " < 结果应为0,实际:" << (s1 <  str3) << endl;
		cout << "串1=" << s1 << " 串3=" << str3 << " <=结果应为1,实际:" << (s1 <= str3) << endl;
		cout << "串1=" << s1 << " 串3=" << str3 << " ==结果应为1,实际:" << (s1 == str3) << endl;
		cout << "串1=" << s1 << " 串3=" << str3 << " !=结果应为0,实际:" << (s1 != str3) << endl;

		cout << "串1=" << s1 << " 串4=" << str4 << " > 结果应为1,实际:" << (s1 >  str4) << endl;
		cout << "串1=" << s1 << " 串4=" << str4 << " >=结果应为1,实际:" << (s1 >= str4) << endl;
		cout << "串1=" << s1 << " 串4=" << str4 << " < 结果应为0,实际:" << (s1 <  str4) << endl;
		cout << "串1=" << s1 << " 串4=" << str4 << " <=结果应为0,实际:" << (s1 <= str4) << endl;
		cout << "串1=" << s1 << " 串4=" << str4 << " ==结果应为0,实际:" << (s1 == str4) << endl;
		cout << "串1=" << s1 << " 串4=" << str4 << " !=结果应为1,实际:" << (s1 != str4) << endl;

		cout << "串1=" << s1 << " 串5=<NULL>" << " > 结果应为1,实际:" << (s1 >  str5) << endl;
		cout << "串1=" << s1 << " 串5=<NULL>" << " >=结果应为1,实际:" << (s1 >= str5) << endl;
		cout << "串1=" << s1 << " 串5=<NULL>" << " < 结果应为0,实际:" << (s1 <  str5) << endl;
		cout << "串1=" << s1 << " 串5=<NULL>" << " <=结果应为0,实际:" << (s1 <= str5) << endl;
		cout << "串1=" << s1 << " 串5=<NULL>" << " ==结果应为0,实际:" << (s1 == str5) << endl;
		cout << "串1=" << s1 << " 串5=<NULL>" << " !=结果应为1,实际:" << (s1 != str5) << endl;


		cout << endl << "任意键继续..." << endl;
		_getch();
		cout << endl;
	}

	if (1) {
		MString s1 = "house";
		char str2[] = "horse", str3[] = "house", str4[] = "";

		cout << "比较运算测试(MString类和字符指针)" << endl;

		cout << "串1=" << s1 << " 串2=" << str2 << " > 结果应为1,实际:" << (s1 >  str2) << endl;
		cout << "串1=" << s1 << " 串2=" << str2 << " >=结果应为1,实际:" << (s1 >= str2) << endl;
		cout << "串1=" << s1 << " 串2=" << str2 << " < 结果应为0,实际:" << (s1 <  str2) << endl;
		cout << "串1=" << s1 << " 串2=" << str2 << " <=结果应为0,实际:" << (s1 <= str2) << endl;
		cout << "串1=" << s1 << " 串2=" << str2 << " ==结果应为0,实际:" << (s1 == str2) << endl;
		cout << "串1=" << s1 << " 串2=" << str2 << " !=结果应为1,实际:" << (s1 != str2) << endl;

		cout << "串1=" << s1 << " 串3=" << str3 << " > 结果应为0,实际:" << (s1 >  str3) << endl;
		cout << "串1=" << s1 << " 串3=" << str3 << " >=结果应为1,实际:" << (s1 >= str3) << endl;
		cout << "串1=" << s1 << " 串3=" << str3 << " < 结果应为0,实际:" << (s1 <  str3) << endl;
		cout << "串1=" << s1 << " 串3=" << str3 << " <=结果应为1,实际:" << (s1 <= str3) << endl;
		cout << "串1=" << s1 << " 串3=" << str3 << " ==结果应为1,实际:" << (s1 == str3) << endl;
		cout << "串1=" << s1 << " 串3=" << str3 << " !=结果应为0,实际:" << (s1 != str3) << endl;

		cout << "串1=" << s1 << " 串4=" << str4 << " > 结果应为1,实际:" << (s1 >  str4) << endl;
		cout << "串1=" << s1 << " 串4=" << str4 << " >=结果应为1,实际:" << (s1 >= str4) << endl;
		cout << "串1=" << s1 << " 串4=" << str4 << " < 结果应为0,实际:" << (s1 <  str4) << endl;
		cout << "串1=" << s1 << " 串4=" << str4 << " <=结果应为0,实际:" << (s1 <= str4) << endl;
		cout << "串1=" << s1 << " 串4=" << str4 << " ==结果应为0,实际:" << (s1 == str4) << endl;
		cout << "串1=" << s1 << " 串4=" << str4 << " !=结果应为1,实际:" << (s1 != str4) << endl;

		cout << endl << "任意键继续..." << endl;
		_getch();
		cout << endl;
	}

	if (1) {
		MString s1 = "tong", s2;

		cout << "求长度测试(length()函数)" << endl;

		cout << "s1为tong,  长度应为4,实际:" << s1.length() << endl;
		cout << "s2为<NULL>,长度应为0,实际:" << s2.length() << endl;
		s2 = s1 + "ji";
		cout << "s2为tongji,长度应为6,实际:" << s2.length() << endl;

		cout << endl << "任意键继续..." << endl;
		_getch();
		cout << endl;
	}

	if (1) {
		MString s1 = "tong", s2;

		cout << "求长度测试(MStringLen()函数)" << endl;

		cout << "s1为tong,  长度应为4,实际:" << MStringLen(s1) << endl;
		cout << "s2为<NULL>,长度应为0,实际:" << MStringLen(s2) << endl;
		cout << "串为tongji,长度应为6,实际:" << MStringLen(s1 + (s2 = "ji")) << endl;
		cout << "串为tongji,长度应为6,实际:" << MStringLen(s1 + "ji") << endl;
		cout << "串为tongji,长度应为6,实际:" << MStringLen(s2 = s1 + "ji") << endl;
		cout << "串为tong,  长度应为4,实际:" << MStringLen(s1 + nullptr) << endl;

		cout << endl << "任意键继续..." << endl;
		_getch();
		cout << endl;
	}

	if (1) {
		const int MAX_BYTES = 100 * 1024 * 1024;
		const long teacher_time = 440000;

		MString s1;
		int     total, len, i;
		char    temp[65536];
		long    t_start, t_end;
		char   *crc_str;

		cout << "内存性能测试(大约需要6-10分钟),以内存耗尽或申请满100MB字节为结束条件,按任意键开始" << endl;
		_getch();

		crc_str = new char[MAX_BYTES + 65536]; //申请(100MB+64KB)空间
		if (crc_str == nullptr) {
			cout << "无法申请" << MAX_BYTES + 65536 << "字节的校验空间,程序终止,请检查后再次运行" << endl;
			goto END;
		}
		*crc_str = 0; //置为空串

		t_start = GetTickCount(); //取当前系统时间,单位毫秒
		srand(time(0));
		total = 0;
		while (1) {
			len = 32768 + rand() % 32768;

			cout << "s1已有长度:" << s1.length() / (1024.0 * 1024) << " MB字节,本次增加 " << len << " 字节" << endl;
			for (i = 0; i<len; i++)
				temp[i] = ' ' + rand() % 95;	//数组用随机字符填充
			temp[len] = 0;
			total += len;
			s1 = s1 + temp;
			strcat(crc_str, temp);
			if (s1.length() == 0 || s1.length() > MAX_BYTES)	//满100MB或内存耗尽则结束
				break;
		}
		t_end = GetTickCount();   //取当前系统时间

		cout << "time=" << (t_end - t_start) / 1000.0 << endl;

		if (s1.length() == 0)
			cout << "内存分配到达 " << total / (1024 * 1024) << " MB字节后,内存耗尽" << endl;
		else
			cout << "内存分配到达满100MB,测试结束" << endl;

		if (s1 != crc_str)
			cout << "s1累加验证出错,请检查程序的实现部分" << endl;
		else {
			cout << "    本次测试耗时 " << (t_end - t_start) / 1000.0 << "秒,大约是老师机器的" << (100.0*(t_end - t_start) / teacher_time) << "%" << endl;
			cout << "    【说明】:只有VC++6.0编译系统下的数据才有可比性" << endl;
			cout << "              老师的机器为Intel(R) Core(TM) i3-2310M CPU @2.10GHz" << endl;
			cout << "              如果时间相差太大,除CPU的性能差异外,还有可能是算法问题" << endl;

			//			cout << endl << "任意键继续..." << endl;
			//			_getch();
			cout << endl;
		}
		delete crc_str;
	}
END:
	system("pause");

	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值