C++でプロトタイプを作成していて、utf8を認識できない不変の文字列を作るためのいくつかのコンセプトを試しながら、私は次のジレンマを立てました。同様に、部分文字列を返す代わりに、元の文字列の一部を参照する部分文字列を返すことができるようにします。std :: stringの定数ビューを返すには?
// 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'クラスをC++ 17から利用できますか? –
代わりに、いくつかのライブラリはC++ 14で ''を実装していましたが、その前にboostにstring_viewライブラリがありました。あなたはGSLを使うこともできます。 –
jaggedSpire