In Delphi, The tstringlist contains a function called find. It searches for a string in a stringlist and returns the index of this string in the stringlist. Descriptions in Delphi help are as follows:
Locates the index for a string in a sorted list and indicates whether a string with that value already exists in the list.
Delphi Syntax:
Function find (const S: string; var index: integer): Boolean; virtual;
C ++ Syntax:
Virtual bool _ fastcall find (const ansistring S, Int & Index );
Description
Use Find to obtain the index in a sorted list where the string s shocould be added. if the string S, or a string that differs from S only in case when casesensitive is false, already exists in the list, find returns true. if the list does not contain a string that matches S, find returns false. the index where s shocould go is returned in the index parameter. the value of index is zero-based, where the first string has the index 0, the second string has the Index 1, and so on.
Note: Only Use Find with sorted lists. for unsorted lists, use the indexof method instead.
As mentioned above, if this function is to be correctly executed, there is a premise that stringlist must be sorted first.
In this case, why not sort the function internally? In this case, this function will not be incorrectly used because you do not know the prerequisite.
Or you can change the function name more intuitively and easily, such as findsort.
Of course, if the stringlist is simply sorted and then searched, the integrity of the original data will be damaged. Therefore, if you put the sorting in the function, you can copy the stringlist before performing the operation.
The following is my pseudocode:
Strlsttmp: tstringlist;
Self. copyto (strlsttmp );
Strlsttmp. Sort ();
Strlsttmp. Find (strtofind); // strtofind: string to be searched
Index: = self. indexof (strtofind); // index: The index position of the stringlist in which the string is located.