string.cpp

来自「《c++ 实践之路》光盘中的源代码」· C++ 代码 · 共 50 行

CPP
50
字号
// Bartosz Milewski (c) 2000
#include "String.h"
#include <iostream>
#include <cctype>

int main ()
{
	StringCow strOriginal ("text");
	StringCow strCopy (strOriginal);
	strCopy.Upcase ();
	std::cout << "The copy: " << strCopy.c_str () << std::endl;
	// The original will not be upper-cased!
	std::cout << "The original: " << strOriginal.c_str () << std::endl;
}

StringVal & StringVal::operator= (StringVal const & str)
{
	if (this != &str)
	{
		char const * cstr = str.c_str ();
		if (cstr == 0)
		{
			delete _buf;
			_buf = 0;
		}
		else
		{
			unsigned len = std::strlen (cstr);
			if (_buf == 0 || std::strlen (_buf) < len)
			{
				delete _buf;
				Init (cstr);
			}
			else
				std::strcpy (_buf, cstr);
		}
	}
	return *this;
}

void StringVal::Upcase ()
{
	if (_buf)
	{
		int len = std::strlen (_buf);
		for (int i = 0; i < len; ++i)
			_buf [i] = std::toupper (_buf [i]);
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?