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

📄 common.h

📁 vc++数字图像识别技术典型案例
💻 H
字号:
/****************************************************************************** * 光学字符识别程序 * 文件名:common.h * 功能  :共用函数定义 * modified by PRTsinghua@hotmail.com******************************************************************************/#ifndef COMMON_H#define COMMON_H#include <stdlib.h>#include <sys/time.h>#include <assert.h>#include <string>#include <fstream>#include <list>using std::istream;using std::list;using std::string;double seconds_passed(struct timeval &curtime);string get_next_token(istream &is);/****************************************************************************** * 获取下一个整数 * 参数:is   输入流 * 返回:整数******************************************************************************/inline long get_next_int(istream &is){	char *endptr;	return strtol(get_next_token(is).c_str(),&endptr,10);}/****************************************************************************** * 获取下一个实数 * 参数:is   输入流 * 返回:实数******************************************************************************/inline double get_next_float(istream &is){	char *endptr;	return strtod(get_next_token(is).c_str(),&endptr);}template<class T> class slist : public list<T> // "sorted" list{public:	// 排序list中的元素	typename slist<T>::iterator insert_sorted( const T& element, typename slist<T>::iterator guess_pos ); 	typename slist<T>::iterator insert_sorted( const T& element) { return insert_sorted( element, end() ); }};template<class T> class qlist : list<T> {	private:		size_t elements;	public:		list<T>::begin; 		list<T>::end;		list<T>::back;		list<T>::iterator;				qlist() : elements( 0 ) {}		void push_back(const T& e) { elements++; list<T>::push_back(e); }		size_t size() const { return elements; }};template<class T> class ringlist_iterator : public qlist<T>::iterator{	private:		qlist<T> *l;	public:		ringlist_iterator( qlist<T>& List) : qlist<T>::iterator( List.begin() ), l( &List )  {}		ringlist_iterator() : qlist<T>::iterator(), l( NULL )  {}		qlist<T> *get_corresponding_list() { assert( l != NULL ); return l; }		ringlist_iterator<T> end();		ringlist_iterator<T>& operator++();			ringlist_iterator<T> operator++( int );		ringlist_iterator<T>& operator--();		ringlist_iterator<T> operator--( int );}; template<class T> class guarded_obj{public:	guarded_obj( T* obj ) : refcount( 1 ), obj( obj ) {}	~guarded_obj() {} 	void addref() { refcount++; }	int delref() { return --refcount; }	T& operator*() { return *obj; }	private:	int refcount;	T* obj;};template<class T> class guarded_ptr{public:	guarded_ptr() : gobj( NULL ) {}	guarded_ptr( T* obj ) { gobj = new guarded_obj<T>( obj ); }	guarded_ptr( const guarded_ptr& ptr ) { gobj = ptr.gobj; if ( gobj ) gobj->addref(); }	~guarded_ptr() { if ( gobj && !gobj->delref() ) delete gobj; }	guarded_ptr& operator=( const guarded_ptr& ptr );	T& operator*() { return **gobj; }	T* operator->() { return &( **gobj ); }	private:	guarded_obj<T>* gobj;};/****************************************************************************** * 插入排序******************************************************************************/template<class T> typename slist<T>::iterator slist<T>::insert_sorted( const T& element, typename slist<T>::iterator guess_pos ){	typename slist<T>::iterator prev, next;			next = prev = guess_pos;		if ( guess_pos != begin() )		--prev;		if ( element < *prev )	// 搜索左边部分	{		while ( prev != begin() && element < *prev )			--prev;				return insert( ( prev == begin() && element < *prev ) ? prev : ++prev, element );	}	else 			// 搜索右边部分	{		while ( next != end() && *next < element )			++next;				return insert( next, element );	}}/****************************************************************************** * 获取最后一个位置******************************************************************************/template<class T> ringlist_iterator<T> ringlist_iterator<T>::end() {	ringlist_iterator<T> n;	n.l = l;	*static_cast<typename qlist<T>::iterator *>( &n ) = l->end();	return n;}/****************************************************************************** * ++操作符******************************************************************************/template<class T> ringlist_iterator<T>& ringlist_iterator<T>::operator++() {	static_cast<typename qlist<T>::iterator *>( this )->operator++();		if ( ( *static_cast<typename qlist<T>::iterator *>( this ) ) == l->end() )		*static_cast<typename qlist<T>::iterator *>( this ) = l->begin();		return *this;}/****************************************************************************** * ++操作符******************************************************************************/template<class T> ringlist_iterator<T> ringlist_iterator<T>::operator++( int ){	ringlist_iterator<T> temp = *this;			this->operator++();	return temp;}/****************************************************************************** * --操作符******************************************************************************/template<class T> ringlist_iterator<T>& ringlist_iterator<T>::operator--(){	if ( ( *static_cast<typename qlist<T>::iterator *>( this ) ) == l->begin() )		*static_cast<typename qlist<T>::iterator *>( this ) = l->end();		static_cast<typename qlist<T>::iterator *>( this )->operator--();		return *this;}/****************************************************************************** * --操作符******************************************************************************/template<class T> ringlist_iterator<T> ringlist_iterator<T>::operator--( int ){	ringlist_iterator<T> temp = *this;	this->operator--();	return temp;}/****************************************************************************** * 赋值操作符******************************************************************************/template<class T> inline guarded_ptr<T>& guarded_ptr<T>::operator=( const guarded_ptr<T>& ptr ) {	if ( this != &ptr )	{		if ( gobj && !gobj->delref() )			delete gobj;				gobj = ptr.gobj;		gobj->addref();	}	return *this;}#endif

⌨️ 快捷键说明

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