实现具有自定义行为的字符串类

发布于 2024-10-10 20:25:44 字数 267 浏览 3 评论 0原文

在我们的一堂课上,先生说模板允许我们自定义类的行为,然后他给出了字符串类的例子,用几行代码我们就可以从STL自定义字符串类,就像我们可以让它处理' a'和'z'相同,'b'和'y'相同,'c'和'x'相同等等。类似'A'和'Z'相同等。

“abc”==“zyx”为真;
"Abc" == "zyx" 为 false;
"Abc == "Zyx" is true;

等等

我正在考虑实现这样的字符串类,但我无法这样做。我们如何使用模板来实现这样的字符串类?

In one of our class sir said that template allows one to customize behavior of class, and then he gave example of string class, that with few lines of code we can customize string class from STL, as in, we can make it to treat 'a' and 'z' same, 'b' and 'y' same, 'c' and 'x' same and so on. Similary 'A' and 'Z' same etc.

"abc" == "zyx" is true;
"Abc" == "zyx" is false;
"Abc == "Zyx" is true;

etc

I was thinking of implementing such string class, but I am not able to do so. How can we implement such string class using templates?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一百个冬季 2024-10-17 20:25:44

这非常棘手。您只需编写自己的 traits 类,具体来说,您需要从 char_traits 类模板派生它,并重新定义 eq()compare() 函数(注意:仅重新定义 eq() 是行不通的;即使 compare() 的重新定义没有任何变化>,你必须将它写在你的派生类中!)。假设这个特征类 sequence_traits 并调用您的自定义字符串 sequence。毕竟,字符串是字符序列!

注意:从您的帖子中了解到,您希望将 alphabets[i] == Alphabets[25-i] 视为相同,这意味着第一个字母和最后一个字母字母相同,第二个字母和倒数第二个字母相同,依此类推!

struct sequence_traits : char_traits<char>
{
    //'a' and 'z' are equal
    //'b' and 'y' are equal
    //'c' and 'x' are equal, and so on.
    //that implies, 'a' + 'z' == 'b' + 'y' == 'c' + 'x' == 'd'  + 'w == so on
    //same for upper cases!
    static bool eq(const char& left, const char& right)
    {   
        return ( left == right) || (left + right == 'a' + 'z') || ( left + right == 'A' + 'Z') ;
    }
    static int compare(const char *first1, const char *first2, size_t count)
    {   
        for (; 0 < count; --count, ++first1, ++first2)
            if (!eq(*first1, *first2))
                return (lt(*first1, *first2) ? -1 : +1);
        return (0);
    }
};

然后您可以执行此 typedef 以便于使用:

typedef basic_string<char, sequence_traits> sequence;

您就完成了。您现在可以使用sequence。 :-)

工作示例:http://www.ideone.com/ByBRV


阅读本文以了解如何操作它的详细工作原理: http://www.gotw.ca/gotw/029.htm

It's very tricky. All you need to write your own traits class, specifically you need to derive it from char_traits<> class template, and redefine eq() and compare() function (Note: only redefining eq() would not work; even though there is no change in the redefinition of compare(), you've to write it in your derived class as such!). Lets say this traits class sequence_traits and call your custom string sequence. After all, string is a sequence of characters!

Note : What I understand from your post that you want alphabets[i] == alphabets[25-i] to be treated as same, that means, first letter and last letter same, second letter and second last letter same, and so on!

struct sequence_traits : char_traits<char>
{
    //'a' and 'z' are equal
    //'b' and 'y' are equal
    //'c' and 'x' are equal, and so on.
    //that implies, 'a' + 'z' == 'b' + 'y' == 'c' + 'x' == 'd'  + 'w == so on
    //same for upper cases!
    static bool eq(const char& left, const char& right)
    {   
        return ( left == right) || (left + right == 'a' + 'z') || ( left + right == 'A' + 'Z') ;
    }
    static int compare(const char *first1, const char *first2, size_t count)
    {   
        for (; 0 < count; --count, ++first1, ++first2)
            if (!eq(*first1, *first2))
                return (lt(*first1, *first2) ? -1 : +1);
        return (0);
    }
};

And then you can do this typedef for easy use:

typedef basic_string<char, sequence_traits> sequence;

You're done. You can use sequence now. :-)

Working example : http://www.ideone.com/ByBRV


Read this article to know how it works in detail : http://www.gotw.ca/gotw/029.htm

郁金香雨 2024-10-17 20:25:44

您需要查看 char_traits 类型。这是一种特征类的示例,您可以将其与 basic_string 一起使用以获得工作字符串类型。如果您定义自己的特征类,则可以像这样构建自定义字符串:

class CustomTraits { ... };
typedef basic_string<char, CustomTraits> CustomString;

现在 CustomTraits 定义的特征将用于确定字符串的工作方式。

作为您所说的示例,您可以执行以下操作:

class CustomTraits: public char_traits<char> {
public:
    /* Redefine equality to compare 'a' and 'z' equal. */
    static bool eq(char one, char two) {
         return one == two || (one == 'a' && two == 'z' || one == 'z' && two == 'a');
    }
};
typedef basic_string<char, CustomTraits> StringWithAAndZEqual;

现在,您可以使用新类型,并且该类将同等对待“a”和“z”。

You'll want to look at the char_traits type. This is an example of one type of traits class that you can use in with basic_string in order to get a working string type. If you define your own traits class, you can build a custom string like this:

class CustomTraits { ... };
typedef basic_string<char, CustomTraits> CustomString;

And now the traits defined by CustomTraits will be used to determine how the string works.

As an example along the lines of what you were saying, you could do something like this:

class CustomTraits: public char_traits<char> {
public:
    /* Redefine equality to compare 'a' and 'z' equal. */
    static bool eq(char one, char two) {
         return one == two || (one == 'a' && two == 'z' || one == 'z' && two == 'a');
    }
};
typedef basic_string<char, CustomTraits> StringWithAAndZEqual;

Now, you can use the new type and the class will treat 'a' and 'z' identically.

时光与爱终年不遇 2024-10-17 20:25:44

您想要创建自定义 char_traits 并用它实例化 std::basic_string

You want to make your custom char_traits and instantiate std::basic_string with that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文