简体   繁体   English

在std :: string中使用自己的迭代器

[英]Using own iterators with std::string

I have defined my own character iterator class. 我已经定义了自己的字符迭代器类。 Can I somehow plug it in to std::string so that I dont have to implement the std::string kind of interface again? 我可以以某种方式将其插入到std :: string中,这样我就不必再次实现std :: string类的接口了吗? I checked the template arguments of std::basic_string, but it does not take anything of this sort. 我检查了std :: basic_string的模板参数,但是没有任何此类内容。

One idea is to provide begin(std::string) and end(std::string) functions which return your iterators. 一种想法是提供begin(std::string)end(std::string)函数,它们返回您的迭代器。 These can then be used in all the places which expect iterators. 然后可以在所有需要迭代器的地方使用它们。

begin() free function style is from c++11 onwards . begin()自由函数样式从c ++ 11开始

There's an overloaded constructor for std::string which will take an iterator pair. std :: string有一个重载的构造函数,它将带有一个迭代器对。 That would work, but create a copy. 那可以,但是创建一个副本。 That is of course the best you can do. 那当然是您可以做的最好的事情。 Consider an implementation which has the "Small String Optimization", ie a small char[N] inside the std::string object itself. 考虑具有“小字符串优化”的实现,即std::string对象本身内部的小char[N] That is very, very likely incompatible with your memory layout. 那很可能与您的内存布局不兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM