C++: set of C-strings

Posted by Nicholas on Stack Overflow See other posts from Stack Overflow or by Nicholas
Published on 2010-12-25T14:47:45Z Indexed on 2010/12/25 14:54 UTC
Read the original article Hit count: 337

Filed under:
|
|

I want to create one so that I could check whether a certain word is in the set using set::find

However, C-strings are pointers, so the set would compare them by the pointer values by default. To function correctly, it would have to dereference them and compare the strings.

I could just pass the constructor a pointer to the strcmp() function as a comparator, but this is not exactly how I want it to work. The word I might want to check could be part of a longer string, and I don't want to create a new string due to performance concerns. If there weren't for the set, I would use strncmp(a1, a2, 3) to check the first 3 letters. In fact, 3 is probably the longest it could go, so I'm fine with having the third argument constant.

Is there a way to construct a set that would compare its elements by calling strncmp()? Code samples would be greatly appreciated.

© Stack Overflow or respective owner

Related posts about c++

Related posts about string