文字列を保持せず、参照するクラスです。std::string_view
と同義です。std::string
と異なり、ヒープアロケーションが発生しないため効率の良い文字列操作を行えます。次のように実装されています。
class StringView
{
const char* data;
size_t size;
};
data ポインタは確保済みの領域を指すのみで、メモリの所有権 (メモリを解放する義務) を持ちません。
個別インクルード
std::string との違い
std::string は動的にメモリを確保し文字列を保持します。そのため下記の例では 3 度、ヒープへのメモリアロケーションが発生します。
std::string src = "hello world !!";
std::string hello = src.substr(0, 5);
std::string world = src.substr(6, 5);
StringView の場合、ヒープアロケーションは発生しません。"hello world !!" という、データ領域に確保されている文字列をポインタで指しているためです。そのためアロケーションは発生しませんが std::string
と違い文字列の書き換えはできません。
BasicStringView substr(const size_type pos, const size_type n=npos) const
指定された範囲のビューを作成する
Definition StringView.hpp:189
BasicStringView< char > StringView
Definition StringView.hpp:385
std::ostream
オブジェクトへの出力
標準出力へ
#include <iostream>
int main()
{
std::cout << sv << std::endl;
}
ファイルへ
#include <fstream>
int main()
{
std::ofstream file{ "output.txt" };
file << sv;
}
USB シリアルへの出力
Arduino 環境では USB シリアルへ出力できます。出力値はシリアルモニターで確認できます。
void setup()
{
Serial.begin(115200);
}
void loop()
{
sv.show();
sv.showRaw();
}
サイズ
constexpr size_type size() const noexcept
ビューのサイズを取得する
Definition StringView.hpp:137
constexpr bool empty() const noexcept
ビュー終端にヌル終端文字を含むことは保証されないので c_str() は提供しない
Definition StringView.hpp:147
文字アクセス
char h = sv[0];
const char* data = sv.
data();
constexpr const_pointer data() const noexcept
ビューへのポインタを取得する
Definition StringView.hpp:130
constexpr const_reference back() const noexcept
ビューの終端の文字を取得する
Definition StringView.hpp:180
const_reference at(const size_type index) const noexcept
指定されたインデックスの文字を取得する
Definition StringView.hpp:161
constexpr const_reference front() const noexcept
ビューの先頭文字を取得する
Definition StringView.hpp:173
部分文字列作成
■ 指定された位置から N 文字の部分文字列を作成
■ 指定された終端文字までの部分文字列を作成
BasicStringView substrUntil(const char_type terminate) const
指定された終端文字までのビューを作成する
Definition StringView.hpp:213
■ 先頭の N 文字を削除した部分文字列を作成
BasicStringView removePrefix(const size_type n) const
先頭のN文字を削除したビューを作成する
Definition StringView.hpp:224
■ 末尾の N 文字を削除した部分文字列を作成
BasicStringView removeSuffix(const size_type n) const
末尾のN文字を削除したビューを作成する
Definition StringView.hpp:239
■ 指定された区切り文字で区切り、部分文字列のリストを作成
std::vector<Udon::StringView> result = sv.
split(
' ');
std::vector< BasicStringView > split(const char_type delimiter) const
指定された区切り文字で区切り、ビューのリストを作成する
Definition StringView.hpp:270
文字列比較
■ 単純な文字列比較
bool eq = a == b;
bool ne = a != b;
■ 指定の文字列で始まっているか判定
bool startsWith(const BasicStringView &string) const noexcept
ビューが指定したビューから始まっているか判定する
Definition StringView.hpp:254
■ 指定の文字列で終わっているか判定
bool endsWith(const BasicStringView &string) const noexcept
ビューが指定したビューで終わっているか判定する
Definition StringView.hpp:262
イテレータ
イテレータを定義しているため for 文などでイテレーションできます。
auto it = std::find(sv.begin(), sv.end(), 'w');
if (it != sv.end())
{
int index = std::distance(sv.begin(), it);
}
数値への変換
{
int number = *numopt;
}
オプショナル型
Definition Optional.hpp:62
{
double number = *numopt;
}
入れ替え
void swap(BasicStringView &other) noexcept
ビューを入れ替える
Definition StringView.hpp:313
文字列リテラル
auto sv = "hello world"_sv;
Definition StringView.hpp:393
std::string への変換
std::basic_string< char_type > toString() const
STL の文字列に変換する
Definition StringView.hpp:306