实现具有自定义行为的字符串类
在我们的一堂课上,先生说模板允许我们自定义类的行为,然后他给出了字符串类的例子,用几行代码我们就可以从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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这非常棘手。您只需编写自己的 traits 类,具体来说,您需要从
char_traits
类模板派生它,并重新定义 eq()和 compare() 函数(注意:仅重新定义 eq() 是行不通的;即使 compare() 的重新定义没有任何变化>,你必须将它写在你的派生类中!)。假设这个特征类sequence_traits
并调用您的自定义字符串sequence
。毕竟,字符串是字符序列!注意:从您的帖子中了解到,您希望将
alphabets[i] == Alphabets[25-i]
视为相同,这意味着第一个字母和最后一个字母字母相同,第二个字母和倒数第二个字母相同,依此类推!然后您可以执行此
typedef
以便于使用:您就完成了。您现在可以使用
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 classsequence_traits
and call your custom stringsequence
. 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!And then you can do this
typedef
for easy use: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
您需要查看 char_traits 类型。这是一种特征类的示例,您可以将其与 basic_string 一起使用以获得工作字符串类型。如果您定义自己的特征类,则可以像这样构建自定义字符串:
现在 CustomTraits 定义的特征将用于确定字符串的工作方式。
作为您所说的示例,您可以执行以下操作:
现在,您可以使用新类型,并且该类将同等对待“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:
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:
Now, you can use the new type and the class will treat 'a' and 'z' identically.
您想要创建自定义
char_traits
并用它实例化std::basic_string
。You want to make your custom
char_traits
and instantiatestd::basic_string
with that.