我如何在没有 C++11 的情况下使用 std::begin 和 std::end?
How can I use std::begin and std::end without C++11?
我正在尝试编写将为 POJ. POJ doesn't use C++11 so I can't use really basic STL functions like std::to_string
, std::begin
, or std::end
. I looked around and found another Whosebug question 查询 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() ;
}
}
我想为 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]; }
boost::lexical_cast 可以完成与 to_string 相同的工作,并且它不需要 C++11。下面是一个简单的例子:
std::string s = boost::lexical_cast<std::string>(12345)
我正在尝试编写将为 POJ. POJ doesn't use C++11 so I can't use really basic STL functions like std::to_string
, std::begin
, or std::end
. I looked around and found another Whosebug question 查询 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() ;
}
}
我想为 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]; }
boost::lexical_cast 可以完成与 to_string 相同的工作,并且它不需要 C++11。下面是一个简单的例子:
std::string s = boost::lexical_cast<std::string>(12345)