StackWithMinTesting1007.cpp
来源:互联网 发布:java怎么学 编辑:程序博客网 时间:2024/06/12 21:55
// StackWithMinTesting1007.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <deque>
#include <assert.h>
#include<iostream>
#include<vector>
using namespace std;
template <typename T> class StackWithMin
{
public:
StackWithMin(void) {};
virtual ~StackWithMin(void) {};
T& top(void);
const T& top(void) const;
void push(const T& value);
void pop(void);
const T& min(void) const;
private:
T m_data; // the elements of stack
T m_minIndex; // the indices of minimum elements
};
template<typename T>T& StackWithMin<T>::top()
{
return m_data.back();
}
template<typename T>const T& StackWithMin<T>::top() const
{
return m_data.back();
}
template<typename T> void StackWithMin<T>::push(const T& value)
{
m_data.push_back(value);
if(m_minIndex.size()==0)
m_minIndex.push_back(0);
else
{
if(value<m_minIndex.back())
m_minIndex.push_back(value);
else
m_minIndex.push_back(m_minIndex.back());
}
}
template<typename T> void StackWithMin<T>::pop(void )
{
m_data.pop_back();
m_minIndex.pop_back();
}
template<typename T>const T& StackWithMin<T>::min(void)const
{
assert(m_minIndex.size()>0);
return m_minIndex.back();
}
int main()//如果这个模版程序在vector的参数没有隐式转换 而是explicite的 那怎么解决?
{
class StackWithMin<deque<int> > mystack;
mystack.push(9);
mystack.push(10);
mystack.push(5);
mystack.push(6);
mystack.push(1);
mystack.push(2);
return 0;
}
//
#include "stdafx.h"
#include <deque>
#include <assert.h>
#include<iostream>
#include<vector>
using namespace std;
template <typename T> class StackWithMin
{
public:
StackWithMin(void) {};
virtual ~StackWithMin(void) {};
T& top(void);
const T& top(void) const;
void push(const T& value);
void pop(void);
const T& min(void) const;
private:
T m_data; // the elements of stack
T m_minIndex; // the indices of minimum elements
};
template<typename T>T& StackWithMin<T>::top()
{
return m_data.back();
}
template<typename T>const T& StackWithMin<T>::top() const
{
return m_data.back();
}
template<typename T> void StackWithMin<T>::push(const T& value)
{
m_data.push_back(value);
if(m_minIndex.size()==0)
m_minIndex.push_back(0);
else
{
if(value<m_minIndex.back())
m_minIndex.push_back(value);
else
m_minIndex.push_back(m_minIndex.back());
}
}
template<typename T> void StackWithMin<T>::pop(void )
{
m_data.pop_back();
m_minIndex.pop_back();
}
template<typename T>const T& StackWithMin<T>::min(void)const
{
assert(m_minIndex.size()>0);
return m_minIndex.back();
}
int main()//如果这个模版程序在vector的参数没有隐式转换 而是explicite的 那怎么解决?
{
class StackWithMin<deque<int> > mystack;
mystack.push(9);
mystack.push(10);
mystack.push(5);
mystack.push(6);
mystack.push(1);
mystack.push(2);
return 0;
}
- StackWithMinTesting1007.cpp
- cpp
- CPP
- CPP
- cpp
- cpp
- CPP
- CPP
- ClientSockThread.cpp&&ClientUrlThread.cpp
- cpp调用其他cpp
- ex34a.cpp&&ex34aDoc.cpp&&ex34aView.cpp
- MainFrm.cpp&&ServerThread.cpp&&SheetConfig.cpp
- CPP thread
- cpp->html
- CFaderWnd.cpp
- PasswordReminder.cpp
- object.cpp
- CmdTarget.cpp
- 写了一个简单实用的PHP模板引擎
- iPhone tableview分批显示数据
- InsertSortTesting1004.cpp
- MergeSortTesting1004.cpp
- eclipse创建user library
- StackWithMinTesting1007.cpp
- NSInvocation简单使用
- 如何使用免费敏捷工具放飞你的创造力?
- 最老程序员创业札记:全文检索、数据挖掘、推荐引擎应用27
- 策略模式testing1002.cpp
- 工厂模式testing0927.cpp
- linux下学习OpenGL
- 装饰模式testing0927.cpp
- iPhone_开发_基本术语_Serialization