Namespaces
Variants
Views
Actions

Talk:cpp/string/basic string/compare

From cppreference.com

[edit] Comparison description incorrect?

This text from the page:

The first one (data) is a substring of the current string, the second one (arg) is a substring of a supplied character sequence. If sizes of the sequences do not match, the shorter sequence is considered being less than the longer sequence. If the sizes (size) of the sequences match, character values are compared. The comparison is done by calling Traits::compare(data, arg, size). For default strings this function does the comparison lexicographically.
Condition Result Return value
size(data) < size(arg) data is less than arg <0
size(data) == size(arg) Traits::compare(data, arg, size) < 0 data is less than arg <0
Traits::compare(data, arg, size) == 0 data is equal to arg 0
Traits::compare(data, arg, size) > 0 data is greater than arg >0
size(data) > size(arg) data is greater than arg >0

Seems completely backwards to me. According to the standard (and all three implementations I have looked at),

int compare(const basic_string<charT,traits,Allocator>& str)

Effects:
  Determines the effective length rlen of the strings  to  compare  as
  the  smallest  of size() and str.size().  The function then compares
  the  two  strings  by  calling  traits::compare(data(),  str.data(),
  rlen).
Returns:
  the nonzero result if the result of the comparison is nonzero.  Oth-
  erwise, returns a value as indicated in Table 10:

                      Table 10--compare() results

                 +------------------------------------+
                 |     Condition         Return Value |
                 +------------------------------------+
                 |size() <  str.size()   < 0          |
                 |size() == str.size()     0          |
                 |size() >  str.size()   > 0          |
                 +------------------------------------+

This means that the function first calls traits::compare on the first rlen characters of each string, where rlen is the smaller of the two string sizes. If traits::compare returns non-zero, that value is returned immediately. If and only if traits::compare returns zero (indicating that first rlen characters compare equal) do the relative sizes of the string come into play. 130.20.187.25 11:13, 10 February 2012 (PST)

Yes, it is incorrect. Thank you for pointing that out! --Cubbi 12:10, 10 February 2012 (PST)

[edit] noexcept of compare(const charT *) [4]

According to 21.4.7.9-4 of the standard, overload 4 has no noexcept specification. It's declared as int compare(const charT *s) const;.

yes, it can certainly throw as defined in the standard. Updated, thank you for bringing this up. --Cubbi (talk) 06:47, 5 December 2014 (PST)

[edit] pos1? count1?

Any particular reason why overloads that take only one parameter of a given name (2, 5, and 6) have a number to that parameter's name? --DevSolar (talk) 11:46, 9 February 2016 (PST)

my guess is to make the Parameters table shorter. --Cubbi (talk) 12:05, 9 February 2016 (PST)