我最近从本地图书馆租了一本关于 c++ 的书,书名是:Joseph Bergin 的“Data Structure Programming with the standard template library in c++”。问题是当我检查它的出版日期时,它是 1998 年,考虑到今天计算机的发展速度,这是一个相当大的时间框架。这本书会伤害我还是帮助我?这是我书中算法的摘录:
// THIS IS AN ALGORITHM EXPLAINED AS SELECTION SORT
template <class T>
void selectionSort(T* start, T* end) //first off, i notice an excessive use of pointers, when usually
{ for(T* where = start ; where < end ; where++) // im told to work around pointers if i dont need to use them
{ T* loc = where; //weird bracket formatting(old practice i would guess?)
T small = *loc;
for //very strange method of organizing for loops used in the book
( T* inner = where + 1;
inner < end;
inner++;
)
if(*inner < *loc)
{ loc = inner;
small = *loc;
}
*loc = *where;
*where = small;
}
}
此代码使用奇怪的方法来进行选择排序。指针可以很容易地用普通值替换,但本书喜欢使用指针和奇怪的 for 循环格式。这段代码与现代 C++11 方法与数组选择排序相差多远?
*他们还用 _ 前缀声明所有变量,这对变量有什么特别的作用吗?声明变量的例子:
int _Num1;
float _Select;