string::operator<=

在 Visual C++ 演示如何使用 字符串:: operatorAMP_LT= 标准 (STL)模板库函数。

template<class _E, class _TYPE, class _A> inline
   bool operator<=(const basic_string<_E, _TYPE, _A>& LString,
   const _E *RCharArray);
template<class _E, class _TYPE, class _A> inline
   bool operator<=(const _E * LCharArray,
   const basic_string<_E, _TYPE, _A>& RString);

备注

备注

类/参数名在原型不匹配版本在头文件。修改某些提高可读性。

具有 字符串:: operatorAMP_LT= 函数的两个版本。 两个版本用于与 basic_string 比较一个 null 终止的字符数组。 通过对结果执行此操作 (RStringAMP_LTLCharArray  RCharArrayAMP_LTLString)。 有关此比较的更多信息,请参见 字符串:: operatorAMP_LT 功能。 注意该运算符不与字符数组的 NULL 指针一起使用。 您需要确保,字符数组不为空,则通过将运算符之前。

备注

// StringLessEqual.cpp
// compile with: /EHsc
// Illustrates how to use the operator<= to compare
// a basic_string variable to a null-terminated
// string.
//
// Functions:
//
//    operator<=   Returns true if the second parameter is not less
//                 than the first.
/////////////////////////////////////////////////////////////////////////

#pragma warning(disable:4786)
#include <string>
#include <iostream>

using namespace std ;

void trueFalse(int x)
{
  cout << (x? "True": "False") << endl;
}

int main()
{
   string S1="DEF";
   char CP1[]="ABC";
   char CP2[]="DEF";
   char CP3[]="DEFG";
   char CP4[]="def";

   cout << "S1 is " << S1 << endl;
   cout << "CP1 is " << CP1 << endl;
   cout << "CP2 is " << CP2 << endl;
   cout << "CP3 is " << CP3 << endl;
   cout << "CP4 is " << CP4 << endl;

   cout << "S1<=CP1 returned ";
   trueFalse(S1<=CP1);  // False  (calls function 1)

   cout << "S1<=CP2 returned ";
   trueFalse(S1<=CP2);  // True   (calls function 1)

   cout << "S1<=CP3 returned ";
   trueFalse(S1<=CP3);  // True   (calls function 1)

   cout << "CP1<=S1 returned ";
   trueFalse(CP1<=S1);  // True   (calls function 2)

   cout << "CP2<=S1 returned ";
   trueFalse(CP2<=S1);  // True   (calls function 2)

   cout << "CP4<=S1 returned ";
   trueFalse(CP4<=S1);  // False   (calls function 2)
}

Output

S1 is DEF
CP1 is ABC
CP2 is DEF
CP3 is DEFG
CP4 is def
S1<=CP1 returned False
S1<=CP2 returned True
S1<=CP3 returned True
CP1<=S1 returned True
CP2<=S1 returned True
CP4<=S1 returned False

要求

标题: string

请参见

概念

标准模板库示例