Is string::compare reliable to determine alphabetical order?

前端 未结 5 1606
太阳男子
太阳男子 2021-02-14 06:59

Simply put, if the input is always in the same case (here, lower case), and if the characters are always ASCII, can one use string::compare to determine reliably the alphabetica

5条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-14 07:52

    In C++, string is the instantiation of the template class basic_string with the default parameters: basic_string, allocator >. The compare function in the basic_string template will use the char_traits::compare function to determine the result value.

    For std::string the ordering will be that of the default character code for the implementation (compiler) and that is usually ASCII order. If you require a different ordering (say you want to consider { a, á, à, â } as equivalent), you can instantiate a basic_string with your own char_traits<> implementation. providing a different compare function pointer.

提交回复
热议问题