C++ 考虑到此类编译器可能应用的优化的复杂性以及决定其可以应用和不能应用的情况的许多微妙之处,不太可能对一种构造的速度与另一种构造的速度进行如此广泛的陈述。谢谢。回答得好,谢谢。回答得很好。 void Utility::TrimToWhiteSpace( std
C++ 考虑到此类编译器可能应用的优化的复杂性以及决定其可以应用和不能应用的情况的许多微妙之处,不太可能对一种构造的速度与另一种构造的速度进行如此广泛的陈述。谢谢。回答得好,谢谢。回答得很好。 void Utility::TrimToWhiteSpace( std,c++,string,loops,C++,String,Loops,考虑到此类编译器可能应用的优化的复杂性以及决定其可以应用和不能应用的情况的许多微妙之处,不太可能对一种构造的速度与另一种构造的速度进行如此广泛的陈述。谢谢。回答得好,谢谢。回答得很好。 void Utility::TrimToWhiteSpace( std::string& str ) { size_t i = 0; for( ; i < str.size() && !std::isspace( str[i], "C" ); i++ ){}
考虑到此类编译器可能应用的优化的复杂性以及决定其可以应用和不能应用的情况的许多微妙之处,不太可能对一种构造的速度与另一种构造的速度进行如此广泛的陈述。谢谢。回答得好,谢谢。回答得很好。
void Utility::TrimToWhiteSpace( std::string& str )
{
size_t i = 0;
for( ; i < str.size() && !std::isspace( str[i], "C" ); i++ ){}
str.erase( 0, i );
}
str.remove(
std::find_if( str.begin(), str.end(), []( char ch ) { return !std::isspace( ch, "C" ); } ),
str.end() );
static std::locale const cLocale( "C" ); // Define locale to ensure lifetime.
static std::ctype<char> const& cType( std::use_facet<std::codecvt<char>>( cLocale ) );
str.remove(
std::find_if( str.begin(), str.end(), [&]( char ch) { return !cType.is( std::ctype_base::space, ch ); } ),
str.end() );
template <std::ctype_base::mask mask>
class Is
{
std::locale myCopyToEnsureLifetime;
std::ctype<char> const* myCType;
public:
Is( std::locale const& locale = std::locale() )
: myCopyToEnsureLifetime( locale )
, myCType( &std::use_facet<std::codecvt<char>>( myCopyToEnsureLifetime )
{
}
bool operator()( char ch ) const
{
return myCType->is( mask, ch );
}
};
template <std::ctype_base::mask mask>
class IsNot
{
std::locale myCopyToEnsureLifetime;
std::ctype<char> const* myCType;
public:
Is( std::locale const& locale = std::locale() )
: myCopyToEnsureLifetime( locale )
, myCType( &std::use_facet<std::codecvt<char>>( myCopyToEnsureLifetime )
{
}
bool operator()( char ch ) const
{
return !myCType->is( mask, ch );
}
};
typedef Is<std::ctype_base::space> IsSpace;
typedef IsNot<std::ctype_base::space> IsNotSpace;
// And so on...