d_orderl.h

来自「这是数据结构和算法的国外经典书籍.清华大学出版社出版的<数据结构C++语言」· C头文件 代码 · 共 56 行

H
56
字号
#ifndef ORDEREDLIST_CLASS
#define ORDEREDLIST_CLASS

#include <list>

using namespace std;

template <typename T>
class orderedList: public list<T>
{
   public:
     	// constructor. implemented with inline code
      orderedList(): list<T>() 
		{}

		orderedList(T *first, T *last);
			// constructor. build an ordered list whose data comes from the
			// pointer range [first, last)

      void insert(const T& item);
			// insert item into the ordered list
   private:
      // disallow automatic access to insert functions in base class
      void push_front(const T& item);
      void push_back(const T& item);
      void insert(list<T>::iterator& pos, const T& item);
};


template <typename T>
orderedList<T>::orderedList(T *first, T *last)
{
	T *curr = first;

	// use insert() to create the ordered list
	while (curr != last)
		insert(*curr++);
}

template <typename T>
void orderedList<T>::insert(const T& item)
{
   // curr starts at first list element, stop marks end. the begin()
   // and end() functions belong to the base class
   list<T>::iterator curr = begin(), stop = end();

   // find the insertion point, which may be at end of list
   while ((curr != stop) && (*curr < item))
      curr++;

   // do the insertion using base class insert() function
   list<T>::insert(curr, item);
}

#endif   // ORDEREDLIST_CLASS

⌨️ 快捷键说明

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