Aarkan
Aarkan

Reputation: 4109

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? I checked the template arguments of std::basic_string, but it does not take anything of this sort.

Upvotes: 0

Views: 117

Answers (2)

MSalters
MSalters

Reputation: 179779

There's an overloaded constructor for std::string which will take an iterator pair. 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", i.e. a small char[N] inside the std::string object itself. That is very, very likely incompatible with your memory layout.

Upvotes: 0

Karthik T
Karthik T

Reputation: 31952

One idea is to provide begin(std::string) and end(std::string) functions which return your iterators. These can then be used in all the places which expect iterators.

begin() free function style is from c++11 onwards.

Upvotes: 1

Related Questions