Christian Ammer
Christian Ammer

Reputation: 7542

String comparison with changed character ordering

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 available?

Thank you for your answers!

Upvotes: 1

Views: 235

Answers (4)

MSalters
MSalters

Reputation: 180050

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.

Upvotes: 2

otto
otto

Reputation: 1158

  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!

Upvotes: 1

Mark B
Mark B

Reputation: 96291

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.

Upvotes: 1

ddyer
ddyer

Reputation: 1786

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

Upvotes: 0

Related Questions