我正在尝试编写将为POJ编译的代码.POJ不使用C++ 11,所以我不能用很基本的STL功能,如std::to_string,std::begin或std::end.我环顾四周,发现其他的StackOverflow问题询问std::to_string.为了std::to_string使用裸g++ myfile.cpp命令编译代码,用户建议使用此补丁,它可以很好地工作:
namespace patch
{
template < typename T > std::string to_string( const T& n )
{
std::ostringstream stm ;
stm << n ;
return stm.str() ;
}
}
Run Code Online (Sandbox Code Playgroud)
我想要做同样的事情std::begin,std::end和std::stoi,但我不知道该怎么做.我对STL很不熟悉.我只是希望我的C++ 11代码能够使用MS-VC++ 6.0或G ++进行编译而不需要任何标志等.我该怎么做?
非常直截了当.例如,这里是std :: begin:
template <typename C>
typename C::iterator my_begin(C& ctr) { return ctr.begin(); }
template <typename C>
typename C::const_iterator my_begin(const C& ctr) { return ctr.begin(); }
template <typename C, size_t sz>
C* my_begin(C (&ctr)[sz]) { return &ctr[0]; }
template <typename C, size_t sz>
const C* my_begin(const C (&ctr)[sz]) { return &ctr[0]; }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
510 次 |
| 最近记录: |