开发者

String comparison with changed character ordering

开发者 https://www.devze.com 2023-02-16 06:43 出处:网络
How would you compare strings when the characters ( and ) should come after the alphanumeric characters. Do I have to write a function or is there any library function avai开发者_Go百科lable?

How would you compare strings when the characters ( and ) should come after the alphanumeric characters. Do I have to write a function or is there any library function avai开发者_Go百科lable?

Thank you for your answers!


You can use std::lexicographical_compare with a custom predicate. That predicate should take 2 chars, and return false if the first should come before the second.


There's no built in way to do this. You'll have to write your own comparison function/functor. I believe you can however implement this with character traits so that operator< still works, but you won't be using std::string anymore.


  1. Copy both strings that are to be compared to temporary strings
  2. In both strings, replace ( and ) with characters that come after alphanumeric characters in the particular encoding you're using
  3. Compare the manipulated results with standard library comparing function
  4. Use the result of function and dispose of the manipulated strings

For instance, ( and ) come before alphanumeric characters in ASCII, but { and } come after!


some environments have provision for custom collation sequences, but generally speaking you have to write your own.

0

精彩评论

暂无评论...
验证码 换一张
取 消