在使用C++进行原型设计和实验,尝试一些概念来创建一个支持utf8的不可变字符串时,我遇到了以下困境:
是否有办法返回一个不可变字符串的视图。例如,我想返回一个子字符串的视图,而不是返回一个新的子字符串。
// Just some quick prototyping of ideas.
// Heavier than just a normal string.
// Construction would be heavier too because of the indices vector.
// Size would end up being O1 though.
// Indexing would also be faster.
struct ustring {
std::string data;
std::vector<size_t> indices;
// How do I return a view to a string?
std::string operator [](size_t const i) const {
return data.substr(indices[i], indices[i + 1] - indices[i]);
}
};
string_view
类? - Paul Rooney<experimental/string_view>
,在此之前,Boost库已经有了string_view库。您也可以使用GSL。 - jaggedSpire