| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1157 人关注过本帖
标题:[求助]有关vc++类的习题
只看楼主 加入收藏
tate
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2006-1-10
收藏
 问题点数:0 回复次数:2 
[求助]有关vc++类的习题

Assignment 10 Template

数组是一种十分常用的数据结构,通过数组,我们可以得到一块连续的内存空间,可以

方便地通过下标操作(”[]”)获取数组中的任何一个元素。但是,数组的大小不能动态增长;

数组也不能让你方便地在数组最后插入一个元素,你必须保存当前的最后一个元素所在数组

中的位置才能完成上述的功能。

在此,我们设计了一个类似数组的新类,使之能够成为一个更加易用的数据结构,能够

方便地对整型元素进行操作,我们称它为整型向量,定义如下:

class IntVector

{

public:

// Constructor

enum{ INITIAL_SIZE = 20 }; // The default initial capacity of IntVector

explicit IntVector( size_t size = INITIAL_SIZE );

~IntVector();

public:

// Member functions

void push_back(int); // Add an item to the end of IntVector

void pop_back() const; // Delete an item at the end of IntVector

int& back(); // Return the reference to the last item of IntVector

size_t size() const; // Return the number of item in IntVector

bool empty() const; // Test if IntVector is empty

// Operator

int& operator [] ( size_t index ); // Return the reference to the IntVector

// item at a specified position

protected:

int* m_pIntArr; // The point to the integer array

size_t m_uiSize; // The number of item in IntVector

size_t m_uiCapacity; // The number of items that IntVector could

// contain without allocating more storage

};

通过以上定义的IntVector 类,我们可以方便地定义一个整型向量,并有以下功能:

1. 可以定义一个整型向量(基于数组实现,其中的元素在内存中是连续分布的),

并可以指定其初始的容量大小,默认的初始容量为20

2. 可以将一个新的整型元素添加到整型向量中最后一个元素的后面。

3. 添加元素时,当向量中的元素个数已达向量容量的大小时,可以分配更大的内

存空间,并完成新元素的添加。

4. 可以删除位于整型向量中的最后一个元素。

5. 可以获得位于整型向量中的最后一个元素的引用。

6. 可以获得整型向量中现有元素的个数。

7. 可以判断整型向量中是否没有元素。

8. 可以通过下标操作(”[]”),直接获得整型向量中指定位置元素的引用。

9. 不考虑整型向量的最大容量。

基于IntVector 类,可以通过以下代码并得到相应的输出。

void main()

{

IntVector intVec;

for ( int i = 0; i <30; ++i )

intVec.push_back(i+1);

for ( i = 0; i < 10; ++i )

{

intVec[i] = intVec.back();

intVec.pop_back();

}

int intTemp = 0;

for ( i = 0; i < 10; ++i )

{

intVec.push_back(i+1);

intTemp = intVec.back();

intVec.back() = intVec[i];

intVec[i] = intTemp;

}

cout << “The size of IntVector is: ” << intVec.size() << endl

<< “The items in IntVector are listed as follows:” << endl;

for ( i = 0; i < intVec.size(); ++i )

cout << intVec[i] << “; ”;

}

得到的屏幕输出为:

The size of IntVector is: 30

The items in IntVector are listed as follows:

1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20; 30; 29; 28; 27; 26; 25; 24;

23; 22; 21;

上述的IntVector 类要比整型数组更易用,而且功能也更加强大。但是它只能用于整型

元素的存储,若是需要在向量中存储字符型、浮点型等内部类型的元素,亦或是自定义类型

的元素,则必须分别定义相应的字符向量类、浮点数向量类、亦或是自定义类型的向量类。

所以,若是能够设计一个模板向量类,就能够“一劳永逸”了。

现请设计出一个模板类(包括类的定义与其成员函数的实现),用于实现任何类型

元素的向量存储。

具体要求如下:

1. 将模板类命名为TemplateVector,其成员函数的函数名称、参数列表和IntVector

中的相同(push_back()的参数自然应该不同);并使TemplateVector 类具有以下功能:

1) 可以定义任意类型元素的向量(基于数组实现,其中的元素在内存中是连续分

布的),并可以指定其初始的容量大小,默认的初始容量为20

2) 可以将一个新的元素添加到向量中最后一个元素的后面。

3) 添加元素时,当向量中的元素个数已达向量容量的大小时,可以分配更大的内

存空间,并完成新元素的添加。

4) 可以删除位于向量中的最后一个元素。

5) 可以获得位于向量中的最后一个元素的引用。

6) 可以获得向量中现有元素的个数。

7) 可以判断向量中是否没有元素。

8) 可以通过下标操作(”[]”),直接获得向量中指定位置元素的引用。

9) 可以不考虑向量的最大容量。

2. 需要考虑TemplateVector 类的健壮性。

3. 不能有内存泄露。

4. 程序必须有一个main()函数,其中可以有一些同学用于测试的代码,也可以为空。

5. 一律使用Win32 Console 完成此次作业。

6. 去掉Debug 文件夹和.exe 可执行文件,打包上传。

搜索更多相关主题的帖子: 习题 内存 left align FONT 
2006-01-10 17:52
踏魔狼
Rank: 6Rank: 6
等 级:贵宾
威 望:24
帖 子:1322
专家分:33
注 册:2005-9-22
收藏
得分:0 

那有什么.看看这个:

#ifndef _PVECTOR_H_
#define _PVECTOR_H_

#include <iostream>
#include <stdlib.h>

template <class T>
class pvector
{

public:
pvector(); //default constructor
pvector(int size); //constructor with specific dimension
pvector(int size, const T &fill_val); //create a pvector with a default fill value
pvector(const pvector<T> &); //copy constructor
virtual ~pvector(); //destructor

void resize(int new_size); //resize the vector

inline int length() const; //returns number of elements in pvector

T & operator [] (int index); //access a particular array element (mutable)
const T & operator [] (int index) const; //access a particular array element (immutable)

const pvector<T> & operator = (const pvector<T> &); //assignment operator

protected:
T *array;
int len;

};

template <class T>
pvector<T>::pvector() :array(0), len(0)
{}

template <class T>
pvector<T>::pvector(int size)
{
if(size <= 0)
{
cerr << "\nError: invalid pvector dimension: " << size << endl;
exit(1);
}
array = new T[size];
len = size;
}

template <class T>
pvector<T>::pvector(int size, const T &fill_val)
{
array = new T[size];
len = size;

for(int i=0; i<size; i++)
array[i] = fill_val;
}

template <class T>
pvector<T>::pvector(const pvector<T> &vec)
{
array = new T[vec.length()];
for(int i=0; i<vec.length(); i++)
array[i] = vec[i];
len = vec.length();
}

template <class T>
pvector<T>::~pvector()
{
delete[] array;
}

template <class T>
int pvector<T>::length() const
{
return len;
}

template <class T>
T & pvector<T>::operator [] (int index)
{
if(index < 0 || index >= length())
{
cerr << "\nError: index out of range: " << index
<< " in pvector of length " << length() << endl;
exit(1);
}
return array[index];
}

template <class T>
const T & pvector<T>::operator [] (int index) const
{
if(index < 0 || index >= length())
{
cerr << "\nError: index out of range: " << index
<< " in pvector of length " << length() << endl;
exit(1);
}
return array[index];
}

template <class T>
const pvector<T> & pvector<T>::operator = (const pvector<T> & vec)
{
delete[] array;
array = new T[vec.length()];
for(int i=0; i<vec.length(); i++)
array[i] = vec[i];
len = vec.length();
return *this;
}

template <class T>
void pvector<T>::resize(int new_size)
{
if(new_size <= 0)
{
cerr << "\nError: invalid pvector dimension: " << new_size << endl;
exit(1);
}

T *newarray = new T[new_size];

int minsize = (new_size<len)?new_size:len;
for(int i=0; i<minsize; i++)
newarray[i] = array[i];

delete[] array;
array = newarray;
len = new_size;
}

#endif


=×&D o I p R e E n C g T l X&×=
2006-01-10 19:39
tate
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2006-1-10
收藏
得分:0 

呵呵,我知道比这好的方法很多,但我现在要完成作业啊~~~~

2006-01-11 09:17
快速回复:[求助]有关vc++类的习题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016072 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved