⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pex7_3.cpp

📁 数据结构C++代码,经典代码,受益多多,希望大家多多支持
💻 CPP
字号:
#include <iostream.h>
#include <string.h>
#pragma hdrstop

#include "wex7_2.h"

// stream output operator for a DataStore object
template <class T>
ostream& operator<< (ostream& ostr, const DataStore<T>& item)
{
	// cycle through the data values and output each one.
	// it is assumed that "<<" is defined for data type T
	for(int i=0;i < item.NumElts();i++)
		ostr << item.GetData(i);
		
	return ostr;
}

struct Person
{
	char name[50];
	int age;
	int height;
};

// relational equal operator for Person. used by DataStore
// when the template type is Person
int operator== (const Person &a, const Person &b)
{
	// two Person records are equal if the name fields agree
	return strcmp(a.name, b.name) == 0;
}

// stream output for Person records. used when a
// DataStore<Person> object is output
ostream& operator<< (ostream &ostr, const Person &a)
{
	cout << a.name << "  " << a.age << " years old with height " 
		 << a.height << " in." << endl;
	return ostr;
}

void main(void)
{
	// five records inserted into DataStore object ds
	Person  p[5] = {{"Mike", 21, 76}, {"Dee ", 26, 64}, {"John", 25, 72},
					{"Flow", 40, 68}, {"Bill", 28, 70}}, q;
	DataStore<Person> ds;
	int i = 0;

	// insert records from p until Insert method returns 0.
	// 0 indicates that the object is full
	while (ds.Insert(p[i]))
		i++;
		
	// test the Find method by looking for record with name "John"
	q = p[2];
	if ((i = ds.Find(q)) != -1)
		cout << "Record with name " << q.name
			 << " found at index " << i;
	else
		cout << "Record with name " << q.name
			 << " not found";
	cout << endl << endl;
		
	cout << "List of entries:" << endl << ds << endl;
}
/*
<Run>

Record with name John found at index 2

List of entries:
Mike  21 years old with height 76 in.
Dee   26 years old with height 64 in.
John  25 years old with height 72 in.
Flow  40 years old with height 68 in.
Bill  28 years old with height 70 in.
*/

⌨️ 快捷键说明

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