📄 stl_patch.h
字号:
//stl_patch.h
#ifndef _H_STL_PATCH
#define _H_STL_PATCH
#include <functional>
#include <algorithm>
#include <string>
#include <vector>
#include <exception>
using namespace std;
//unary_compose
template <class UnaryFunction1,class UnaryFunction2>
class unary_compose
:public unary_function<typename UnaryFunction2::argument_type,
typename UnaryFunction1::result_type>
{
public:
unary_compose(UnaryFunction1 uf1,UnaryFunction2 uf2):m_uf1(uf1),m_uf2(uf2){};
result_type operator () (typename argument_type x)
{
return m_uf1(m_uf2(x));
}
protected:
UnaryFunction1 m_uf1;
UnaryFunction2 m_uf2;
};
template <class UnaryFunction1,class UnaryFunction2>
inline unary_compose<UnaryFunction1,UnaryFunction2>
compose1(UnaryFunction1 uf1,UnaryFunction2 uf2)
{
return unary_compose<UnaryFunction1,UnaryFunction2>(uf1,uf2);
}
//binary_compose
template <class BinaryFunction,class UnaryFunction1,class UnaryFunction2>
class binary_compose:
public unary_function<typename UnaryFunction1::argument_type,
typename BinaryFunction::result_type>
{
public:
binary_compose(BinaryFunction bf,UnaryFunction1 uf1,UnaryFunction2 uf2):m_bf(bf),m_uf1(uf1),m_uf2(uf2){};
result_type operator () (argument_type x){return m_bf(m_uf1(x),m_uf2(x));}
protected:
BinaryFunction m_bf;
UnaryFunction1 m_uf1;
UnaryFunction2 m_uf2;
};
template <class BinaryFunction,class UnaryFunction1,class UnaryFunction2>
inline binary_compose<BinaryFunction,UnaryFunction1,UnaryFunction2>
compose2(BinaryFunction bf,UnaryFunction1 uf1,UnaryFunction2 uf2)
{
return binary_compose<BinaryFunction,UnaryFunction1,UnaryFunction2>(bf,uf1,uf2);
}
string itos(int value,int radix=10)
{//max avail radix is 36 and min is 2
//0
if (value==0)
return string("0");
//build table and check legal input
const char table[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const int N=sizeof(table)/sizeof(char)-1;
if (radix<2 || radix>N)
return string("error radix!");
string vs;
//negative
if (value<0)
{
vs.push_back('-');
value = -value;
}
//positive
while (value)
{
vs.push_back(table[value%radix]);
value /= radix;
}
//reverse
if (*vs.begin()=='-')
reverse(vs.begin()+1,vs.end());
else
reverse(vs.begin(),vs.end());
return vs;
}
int stoi(string str,int radix=10)
{//legal characters: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ(abcdefghijklmnopqrstuvwxyz),ignore radix
//build search table
int c,size=max(max('Z','z'),'9')+1;
vector<int> table(size,-1);
for (c='0'; c<='9'; c++) table[c] = c-'0';
for (c='A'; c<='Z'; c++) table[c] = c-'A'+10;
for (c='a'; c<='z'; c++) table[c] = c-'a'+10;
//decode
string::iterator scan=str.begin();
int value=0;
if (*scan=='-') ++scan;
while(scan!=str.end())
{
value *= radix; //the only space using radix
if (*scan>=0 && *scan<size && table[*scan]!=-1) //check legal
value += table[*scan];
else
return 0x80000000; //illegal input
++scan;
}
//return result
return (*str.begin()=='-') ? -value : value ;
}
class stl_exception:public exception
{
public:
stl_exception(const string& message):m_msg(message){}
virtual const char* what() const {return m_msg.c_str();}
protected:
const string m_msg;
};
#endif //_H_STL_PATCH
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -