c++使用指针记录更新位置

本文详细介绍了C++中String类的构造、赋值、运算符重载及静态成员函数,展示了如何使用new和delete操作动态内存,并通过示例展示了字符串的比较、输入输出等。涉及了字符串处理和内存管理的基础知识。
摘要由CSDN通过智能技术生成

1. string1.h

#pragma once
#include<iostream>
using std::ostream;
using std::istream;

class String
{
private:
	char* str; // pointer to string
	int len; // length of string
	static int num_strings; // number of objects
	static const int CINLIM = 80; // cin input limit
public:
	// constructors and other methods
	String(const char* s); // constructor
	String(); // default constructor
	String(const String& st); // copy constructor
	~String(); // destructor
	int length() const { return len; }

	// overload operator methods
	String& operator=(const String& st);
	String& operator=(const char* s);
	char& operator[](int i);
	// read only char access for const string
	const char& operator[](int i) const;
	// overload operator friends
	friend bool operator<(const String& st, const String& st2);
	friend bool operator>(const String& st1, const String& st2);
	friend bool operator==(const String& st, const String& st2);
	friend ostream& operator<<(ostream& os, const String& st);
	friend istream& operator>>(istream& is, String& st);

	// static function
	static int HowMany();
};

2. string1.cpp

#include "string1.h"
#include<cstring>

using std::cin;
using std::cout;

int String::num_strings = 0;

String::String(const char* s)
{
	len = std::strlen(s);
	str = new char[len + 1];
	std::strcpy(str, s);
	num_strings++;
}

String::String()
{
	len = 4;
	str = new char[1];
	str[0] = '\0';
	num_strings++;
}

String::String(const String& st)
{
	num_strings++; // handle static member update
	len = st.len; // same length
	str = new char[len + 1]; // allot space
	std::strcpy(str, st.str); // copy string to new location
}

String::~String()
{
	--num_strings;
	delete[] str;
}

String& String::operator=(const String& st)
{
	// TODO: 在此处插入 return 语句
	if (this == &st) return *this;
	delete[] str;
	len = st.len;
	str = new char[len + 1];
	std::strcpy(str, st.str);
	return *this;
}

String& String::operator=(const char* s)
{
	// TODO: 在此处插入 return 语句
	delete[] str;
	len = std::strlen(s);
	str = new char[len + 1];
	std::strcpy(str, s);
	return *this;
}

char& String::operator[](int i)
{
	// TODO: 在此处插入 return 语句
	return str[i];
}

const char& String::operator[](int i) const
{
	// TODO: 在此处插入 return 语句
	return str[i];
}

int String::HowMany()
{
	return num_strings;
}

bool operator<(const String& st, const String& st2)
{
	return (std::strcmp(st.str,st2.str)<0);
}

bool operator>(const String& st1, const String& st2)
{
	return st2 < st1;
}

bool operator==(const String& st, const String& st2)
{
	return (std::strcmp(st.str, st2.str) == 0);
}

ostream& operator<<(ostream& os, const String& st)
{
	// TODO: 在此处插入 return 语句
	os << st.str;
	return os;
}

istream& operator>>(istream& is, String& st)
{
	// TODO: 在此处插入 return 语句
	char temp[String::CINLIM];
	is.get(temp, String::CINLIM);
	if (is) {
		st = temp;
	}
	while (is && is.get() != '\n') {
		continue;
	}
	return is;
}

3. sayings2.cpp

#include<iostream>
#include<stdlib.h> // for rand(),srand()
#include<ctime> // for time
#include"string1.h"

const int ArSize = 10;
const int MaxLen = 81;

int main()
{
	using namespace std;
	String name;
	cout << "hi, what's your name?\n>> ";
	cin >> name;

	cout << name << ", please enter up to " << ArSize
		<< " short sayings <empty line to quit>:\n";
	String sayings[ArSize];
	char temp[MaxLen];
	int i;
	for (i = 0; i < ArSize; i++) {
		cout << i + 1 << ": ";
		cin.get(temp, MaxLen);
		while (cin and cin.get()!='\n') continue;
		if (!cin || temp[0] == '\0') break;
		else sayings[i] = temp;
	}
	int total = i;
	if (total > 0) {
		cout << "here are your sayings:\n";

		// use pointers to keep track of shortest
		// first strings
		String* shortest = &sayings[0];
		String* first = &sayings[0];
		for (i = 1; i < total; i++) {
			if (sayings[i].length() < shortest->length()) {
				shortest = &sayings[i];
			}
			if (sayings[i] < *first) {
				first = &sayings[i];
			}
		}
		cout << "shortest saying:\n";
		cout << *shortest << endl;
		cout << "first alphabetically:\n" <<
			*first << endl;
		srand(time(0));
		int choice = rand() % total; // pick index at random
		// use new to create, initialize new string object
		String* favorite = new String(sayings[choice]);
		cout << "my favorite saying:\n" <<
			*favorite << endl;
		delete favorite;
	}
	else {
		cout << "not much to say, eh?\n";
	}
	cout << "bye.\n";

	return 0;
}

4. 结果

hi, what's your name?
>> hablee
hablee, please enter up to 10 short sayings <empty line to quit>:
1: yuki and hablee
2: hablee and yuki
3: what's wrong with their relationship?
4:
here are your sayings:
shortest saying:
yuki and hablee
first alphabetically:
hablee and yuki
my favorite saying:
what's wrong with their relationship?
bye.

E:\projectFiles\CPPcodes\learn_cpp\DynamicMemory002\x64\Release\DynamicMemory002.exe (进程 39596)已退出,代码为 0。
按任意键关闭此窗口. . .
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值