std::string wrapper class?

Started by
24 comments, last by shurcool 20 years, 4 months ago
I found this debate on the subject of private inheritance vs. containment, especially when it comes to inheriting from std::string... I thought I''d post it here, as some of you may be interested.

Here.

I would do containment, like many of them recommended, but like I said earlier, it''s too much work having to copy all the constructors/member functions of std::string interface into mine, and I simply object to this kind of approach. It''s not right. Why should I rewrite the entire interface of a class, just to add a few of my own functions?

So I tried ironpoint''s code, and it''s far from working. First of all, what in the world is binary_function? I''ve searched through all my VC.net includes, and there isn''t such thing. Where did you get it from?

Please help me get it to work as a global function for now... Thanks.

PS. Ready4Dis, thanks a lot for that code, but I''d rather stick with the standard string for now, as this is just a very minor aspect of my project (it''s for a config file parser; to get a boolean statement, I need to compare a string parameter in the config file to "Yes", ignoring case).

---
shurcooL`
Advertisement
Never mind now, I've figured it out.

If anyone ever needs something like this, I'll post it for your reference.

This goes in a CPP file.

template <class T>struct equal_to_ignore_case : public std::binary_function<T, T, bool> {	result_type operator()(const T& first_argument_type, const T& second_argument_type) const	{		return std::toupper(first_argument_type) == std::toupper(second_argument_type);	}}; bool StringIgnoreCaseCompare(String sString1, String sString2){	if (sString1.length() == sString2.length())		return std::equal(sString1.begin(), sString1.end(), sString2.begin(), equal_to_ignore_case<std::string::value_type>());	else return false;}
Keep in mind this uses a basic <cctype> toupper function, so if you need support for wide characters and what not, use toupper(charT, locale) from <locale>.

---
shurcooL`

[edited by - shurcooL on December 14, 2003 4:51:55 PM]
Am I completely missing something? Why didn''t you just do:

if( string1.toUpper() == string2.toUpper() );

For your case insensitive match? Why bother with templates and extra global funcs and all that fun stuff?
_buu_, std::string doesn't have a toupper() method. You'd have to do it manually, like so.

#include <algorithm>#include <cctype>std::transform(str1.begin(), str1.end(), str1.begin(), std::toupper);   
What I'm doing is the exact same thing, only I'm comparing it char by char. So if the first char is different in the two strings, it'll break out right away. Real handy if the strings are very long.

---
shurcooL`

[edited by - shurcooL on December 14, 2003 9:44:32 PM]
shurcool: Woah. It doesn''t. I wonder why I thought it did..
quote:Original post by shurcool
PS. Ready4Dis, thanks a lot for that code, but I''d rather stick with the standard string for now, as this is just a very minor aspect of my project (it''s for a config file parser; to get a boolean statement, I need to compare a string parameter in the config file to "Yes", ignoring case).



Yeah, I know it was a bit much for your purposes, but hey, it''s relatively efficient for compares and such, since it knows the exact length of each string, it can compare that before it does any sort of string compare, even if you don''t use the hash (which you can''t unless both strings are in the same case). Implementing the functionality that you need into my string class would be rather simple, and possibly even speed-up your code a bit, which probably isn''t a major concern at this time, but it''s available if required .

This topic is closed to new replies.

Advertisement