extending c++ string member functions

2020-08-25 05:45发布

I had a need to do a case insensitive find and found the following code which did the trick

bool ci_equal(char ch1, char ch2)
{
    return toupper((unsigned char)ch1) == toupper((unsigned char)ch2);
}

size_t ci_find(const string& str1, const string& str2)
{
    string::const_iterator pos = std::search(str1. begin ( ), str1. end ( ), str2.
    begin ( ), str2. end ( ), ci_equal);
    if (pos == str1. end ( ))
        return string::npos;
    else
        return pos - str1. begin ( );
}

That got me to wondering what it would take to make this a member function of 'string' so it could be called like this:

string S="abcdefghijklmnopqrstuv";
string F="GHI";

S.ci_find(F);

I realize that there are many problems with case conversions in non-English languages but that's not the question I'm interested in.

Being a neophyte, I quickly got lost among containers and templates.

Is there anyway to do this? Could someone point to me an example of something similar?

标签: c++ string
4条回答
老娘就宠你
2楼-- · 2020-08-25 05:52

Perhaps following the Standard Library Algorithms <algorithm> methodology could be beneficial. It wouldn't surprise users as much. :-)

Algorithm Functions as an example.

查看更多
我只想做你的唯一
3楼-- · 2020-08-25 05:55

Most of the time, it will be sufficient to define a functor. More specifically, a less-than comparator. The benefit is that the functor, along with the unmodified string class, can be stored in STL containers, and STL will use your custom functors for operations.

It is a class with a default constructor and an overloaded function call operator
bool operator()(const string& x, const string& y)
which performs case-insensitive comparison.

You can also define a equality functor, depending on your need.

True, it is no longer possible to use the == and < operators literally on objects of your string class, instead you'd need to create an instance of the functor and use it like a function call. But I don't think there's any other alternative in C++.

Edited: I misunderstood the question. The non-relevant part of response is removed.

Edited 2: Please completely disregard my answer ...

查看更多
Lonely孤独者°
4楼-- · 2020-08-25 06:09

I think most more experienced C++ programmers would agree that this is poor idea. If anything, std::string already has way too many member functions, and adding still more will make a bad situation worse. Worse still, if you were going to do this, you'd probably do it by inheritance -- but std::string isn't designed to be used as a base class, and using it as one will lead to code that's fragile and error-prone.

For another idea of how to do this, you might want to read Guru of the Week #29. Do read the whole article though, to get an idea of both how to do it, and why you probably don't want to. Ultimately, what you have right now is probably the best option -- keep the case insensitive searching separate from std::string itself.

查看更多
我想做一个坏孩纸
5楼-- · 2020-08-25 06:16

std::string is not made to be extended.

You could encapsulate an std::string into a class of yours and set those member functions in that class.

查看更多
登录 后发表回答