如何在没有C++ 11的情况下使用std :: begin和std :: end?

3 c++ stl g++ std c++11

我正在尝试编写将为POJ编译的代码.POJ不使用C++ 11,所以我不能用很基本的STL功能,如std::to_string,std::beginstd::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::endstd::stoi,但我不知道该怎么做.我对STL很不熟悉.我只是希望我的C++ 11代码能够使用MS-VC++ 6.0或G ++进行编译而不需要任何标志等.我该怎么做?

Ser*_*eyA 5

非常直截了当.例如,这里是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)

  • 最后一次重载不是必需的.对于const数组,`C`将在第三个重载中推导为`const`,并且它比其他两个更专业. (4认同)