c++primer3版中的问题

zhangyu21101213 2008-10-23 05:55:14
/*******************************array.h***************************/

#ifndef __ARRAY_H__
#define __ARRAY_H__

#include <iostream>

using namespace std;

//template <class elemType> void try_array(Array<elemType> &);
template <class elemType> class Array;
template <class elemType> ostream&
operator<< (ostream &,const Array<elemType> &);

template <class elemType>
class Array {
public:
explicit Array(int sz=DefaultArraySize)
{init(0,sz);}
Array(const elemType *ar,int sz)
{init(ar,sz);}
Array(const Array &iA)
{init(iA._ia,iA._size);}
~Array(){delete[] _ia;}

Array & operator=(const Array &);
int size() const
{return _size;}

elemType& operator[](int ix) const
{return _ia[ix];}

ostream &print(ostream &os=cout) const;
void grow();
void sort(int,int);
int find(elemType);
elemType min();
elemType max();
private:
void init(const elemType *,int);
void swap(int,int);

static const int DefaultArraySize=12;
int _size;
elemType *_ia;
};

#endif //__ARRAY_H__
/***********************************end****************************/

/***************************array.cpp********************/
#include "array.h"

template <class elemType>
void Array<elemType>::init(const elemType *array,int sz)
{
_size=sz;
_ia=new elemType[_size];
for(int ix=0;ix<_size;++ix)
if(!array)
_ia[ix]=0;
else _ia[ix]=array[ix];
}

template <class elemType> Array<elemType> &
Array<elemType>::operator=(const Array<elemType> &iA)
{
if(this!=&iA)
{
delete[] _ia;
init(iA._ia,iA._size);
}
return *this;
}

template <class elemType> ostream&
operator<<(ostream &os,const Array<elemType> &ar)
{
return ar.print(os);
}

template <class elemType> ostream&
Array<elemType>::print(ostream &os) const
{
const int lineLength=12;
os<<"( "<<_size<<" )<";

for(int ix=0;ix<_size;++ix)
{
if(ix%lineLength==0 &&ix)
os<<"\n\t";
os<<_ia[ix];
if(ix%lineLength !=lineLength-1 && ix!=_size-1)
os<<", ";
}
os<<" >\n";
return os;
}

template <class elemType>
void Array<elemType>::grow()
{
elemType *oldia=_ia;
int oldSize=_size;

_size=oldSize+oldSize/2+1;
_ia=new elemType[_size];

int ix;
for(ix=0;ix<oldSize;++ix)
_ia[ix]=oldia[ix];
for(;ix<_size;++ix)
_ia[ix]=elemType();
delete[] oldia;
}

template <class elemType>
elemType Array<elemType>::min()
{
assert(_ia!=0);
elemType min_val=_ia[0];
for(int ix=1;ix<_size;++ix)
if(_ia[ix]<min_val)
min_val=_ia[ix];
return min_val;
}

template <class elemType>
elemType Array<elemType>::max()
{
assert(_ia!=0);
elemType max_val=_ia[0];
for(int ix=1;ix<_size;++ix)
if(_ia[ix]>max_val)
max_val=_ia[ix];
return max_val;
}

template<class elemType>
int Array<elemType>::find(elemType val)
{
for(int ix=0;ix<_size;++ix)
if(val==_ia[ix])
return ix;
return -1;
}

template<class elemType>
void Array<elemType>::swap(int i,int j)
{
elemType tmp=_ia[i];
_ia[i]=_ia[j];
_ia[j]=tmp;
}

template<class elemType>
void Array<elemType>::sort(int low,int high)
{
if(low>=high) return;
int lo=low;
int hi=high+1;
elemType elem=_ia[low];
for(;;)
{
while(_ia[++lo]<elem && lo<high);
while(_ia[--hi]>elem && hi>low);
if(lo<hi)
swap(lo,hi);
else break;
}
swap(low,hi);
sort(low,hi-1);
sort(hi+1,high);
}
///*
//not class function,test the class Array
template<class elemType>
void try_array(Array<elemType> &iA)
{
cout << "try_array: initial array values:\n";
cout << iA << endl;
elemType find_val = iA [ iA.size()-1 ];
iA[ iA.size()-1 ] = iA.min();
int mid = iA.size()/2;
iA[0] = iA.max();
iA[mid] = iA[0];
cout << "try_array: after assignments:\n";
cout << iA << endl;

Array<elemType> iA2 = iA;
iA2[mid/2] = iA2[mid];
cout << "try_array: memberwise initialization\n";
cout << iA << endl;

iA = iA2;
cout << "try_array: after memberwise copy\n";
cout << iA << endl;
iA.grow();
cout << "try_array: after grow\n";
cout << iA << endl;

int index = iA.find( find_val );
cout << "value to find: " << find_val;
cout << "\tindex returned: " << index << endl;
elemType value = iA[index];
cout << "value found at index: ";
cout << value << endl;

}
//*/
/*************************end***********************/
...全文
72 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
snow_haoxue 2008-10-24
  • 打赏
  • 举报
回复
虽然正在从头开始认真学习c++ primer 3,可是看到了你的帖子内容就……晕那、东西太多了
太乙 2008-10-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zhangyu21101213 的回复:]
怎么没人理啊,各位有时间,看看好吗?谢谢了!
[/Quote]
兄弟,如果你能把里面你不懂的,重点抽象出来,那我看有很多人乐意回答你的问题!

这么长,这么乱,估计没几个有闲工夫!
太乙 2008-10-24
  • 打赏
  • 举报
回复
友情up一下~~
zhangyu21101213 2008-10-24
  • 打赏
  • 举报
回复
怎么没人理啊,各位有时间,看看好吗?谢谢了!
zhangyu21101213 2008-10-24
  • 打赏
  • 举报
回复
谢谢楼上的朋友,不过,我会把主要的东西抽出来的!
zhangyu21101213 2008-10-23
  • 打赏
  • 举报
回复
/***************************array_sort.H********************/
#ifndef ARRAY_S_H
#define ARRAY_S_H
#include "Array.h"
template <class Type>
class Array_Sort : public virtual Array<Type> {
protected:
void set_bit() { dirty_bit = true; }
void clear_bit() { dirty_bit = false; }
void check_bit() {
if ( dirty_bit ) {
sort( 0, Array<Type>::_size-1 );
clear_bit();
}
}
public:
Array_Sort( const Array_Sort&);
Array_Sort( int sz = Array<Type>::ArraySize )
: Array<Type>( sz )
{ clear_bit(); }
Array_Sort( const Type* arr, int sz )
: Array<Type>( arr, sz )
{ sort( 0,Array<Type>::_size-1 ); clear_bit(); }
Type&operator[]( int ix )
{ set_bit(); return ia[ ix ]; }
void print( ostream&os = cout )
{ check_bit(); Array<Type>::print( os ); }
Type min() { check_bit(); return ia[ 0 ]; }
Type max() { check_bit(); return ia[ Array<Type>::_size-1 ]; }
bool is_dirty() const { return dirty_bit; }
int find( Type );
void grow();
protected:
bool dirty_bit;
};
#endif

/*************************end***********************/

/***************************array_sort.CPP********************/
template <class Type>
Array_Sort<Type>::
Array_Sort( const Array_Sort<Type> &as )
: Array<Type>( as )
{
dirty_bit = as.dirty_bit;
check_bit();
}

template <class Type>
void Array_Sort<Type>::grow()
{
Array<Type>::grow();
sort( 0, Array<Type>::_size-1 );
clear_bit();
}

template <class Type>
int Array_Sort<Type>::find( Type val )
{
int low = 0;
int high = Array<Type>::_size-1;
check_bit();
while ( low <= high ) {
int mid = ( low + high )/2;
if ( val == ia[ mid ])
return mid;
if ( val < ia[ mid ] )
high = mid-1;
else low = mid+1;
}
return -1;
}

/************************end***********************/


/****************************main.cpp************************/
#include <string>
#include <iostream>
#include "array.h"

using namespace std;

template<class elemType> void try_array(Array<elemType> &);

int main()
{
static int ia[] = { 12,7,14,9,128,17,6,3,27,5 };
static string sa[] = { "Eeyore", "Pooh", "Tigger",
"Piglet", "Owl", "Gopher", "Heffalump" };

Array_sort<int> iA( ia, 10);
Array<string> sA( sa, 7 );

cout << "class template instantiation Array_Sort<int>" << endl;
try_array(iA);

cout << "template template instantiation Array_Sort<string>" << endl;
try_array(sA);

return 0;
}

/************************end***********************/

书上的结果是:

class template instantiation Array_Sort<string>
try_array: initial array values:
( 7 )< Eeyore, Gopher, Heffalump, Owl, Piglet, Pooh
Tigger >
try_array: after assignments:
( 7 )< Eeyore, Gopher, Owl, Piglet, Pooh, Pooh
Pooh >
try_array: memberwise initialization
( 7 )< Eeyore, Gopher, Owl, Piglet, Pooh, Pooh
Pooh >
try_array: after memberwise copy
( 7 )< Eeyore, Piglet, Owl, Piglet, Pooh, Pooh
Pooh >*****************************这一行没有排序,但我不知道为什么?书上说的结实不明白!            
try_array: after grow
( 11 )<<;empty>, <empty>, <empty>, <empty>, Eeyore, Owl
Piglet, Piglet, Pooh, Pooh, Pooh >
value to find: Tigger index returned: -1
Memory fault(coredump)

书上有说:

注意按成员拷贝的Array 类string 实例的显示并没有被排序为什么会这样这是因
为此处的虚拟函数是通过类的对象被调用的而不是通过指针或引用正如17.5 节说明的
当通过类对象调用时被调用的实例反映了该对象的类类型的活动虚拟函数而不是可能己
经被赋值给它的对象的类类型因此无法通过Array 类对象调用Sort 实.

问题,我看不出来是哪个对象调用,我的想法是他应该调用Array_Sort的排序函数!
为什么没有调用,请各位看看!说下你们的思路!谢谢!

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧