Home > Back-end >  The STL Map custom structure, find problems
The STL Map custom structure, find problems

Time:09-22

 struct SortS which 
{
Int nNum;
String strName;

Bool bFlag;//false - & gt; According to the num is true - & gt; According to the Name is

SortS () : nNum (0), strName (" "), bFlag (false)
{}

Bool operator<(const SortS & amp; Const stSortS)
{
Bool bRet=false;
if(! BFlag & amp; & ! StSortS. BFlag)
{
BRet=nNum & lt; StSortS. NNum;
}
The else
{
BRet=strName & lt; StSortS. StrName;//???
}

Return bRet.
}
};


Int main ()
{
Map MapSortTest;

SortS stSort1;
StSort1. NNum=1;
StSort1. StrName="111";

SortS stSort2;
StSort2. NNum=2;
StSort2. StrName="222";

SortS stSort3;
StSort3. NNum=3;
StSort3. StrName="333";

SortS stSort4;
StSort4. NNum=4;
StSort4. StrName="444";

MapSortTest. Insert (pair (stSort1, "liu111"));
MapSortTest. Insert (pair (stSort2, "liu222"));
MapSortTest. Insert (pair (stSort3, "liu333"));
MapSortTest. Insert (pair (stSort4, "liu444"));

Map : : iterator it=mapSortTest. The begin ();
For (; It!=mapSortTest. End (); + + it)
{
Printf (" it. First: second: % d % s % s \ n ", it - & gt; First, nNum, it - & gt; First, strName c_str (),
It - & gt; Second. C_str ());
}
Printf (" \ n \ n ");

SortS stSort5;
StSort5. BFlag=true;
StSort5. StrName="222";
It=mapSortTest. Find (stSort5);

If (it==mapSortTest. End ())
{
Printf (" 66666666666666666666666666666 \ n ");
}
The else
{
Printf (" 77777777777777777777777777777 \ n ");
}

It=mapSortTest. The begin ();
For (; It!=mapSortTest. End (); + + it)
{
Printf (" it. First: second: % d % s % s \ n ", it - & gt; First, nNum, it - & gt; First, strName c_str (),
It - & gt; Second. C_str ());
}

system("pause");
return 0;
}



In the above code??? , if my collation using & gt; The find of the map will be can not find, but if the collation use & lt; Num (and above), the find of the map. Then you will find,,, now can't understand, ask god to explain,,,
  • Related