Share
Embed
Transcript
57 slides
-
basic_string_view overview basic_string_view overview basic_string_viewは所有権を持たない 参照もとを変更しない ⽂字列の参照を持ち、なんかいい感じに加⼯して使える 参照して持ち回し、⽂字列の検索や⽐較ができる Tips:⽣⽂字列リテラルは静的に⽣存する #include<basic_string_view>
std::basic_string_viewfunc(){ return hoge ; } intmain(){ //Itisvalidbecauserawstringliteralshavestaticstorageduratioin std::basic_string_viewhoge(); } -
宣⾔等 宣⾔等 以降はメンバに CharT*data_ size_typesize_ を持つもののして進めていきます namespacestd{ template<classCharT,classTraits=char_traits<CharT>> classbasic_string_view;
usingbasic_string_view=basic_string_view<char>; usingu16basic_string_view=basic_string_view<char16_t>; usingu32basic_string_view=basic_string_view<char32_t>; usingwbasic_string_view=basic_string_view<wchar_t>; } -
basic_string_view literals basic_string_view literals basic_string_viewを⽣⽂字列リテラルから構築できるもの constexprstring_viewoperator""sv(constchar*str,size_tlen)noexcept; constexpru16string_viewoperator""sv(constchar16_t*str,size_tlen)noexcept; constexpru32string_viewoperator""sv(constchar32_t*str,size_tlen)noexcept; constexprwstring_viewoperator""sv(constwchar_t*str,size_tlen)noexcept; #include<string_view>
intmain(){ usingnamespacestd::literals; std::string_viewsv= hoge sv; } -
literaloperatorsには⽂字列がコンパイル時に渡るため コンパイラはconstchar[N]という型情報から⽂字列も⻑さを決定できる (できるのでする) この事実から、basic_string_viewのコンストラクタでは構築不可能である null⽂字を含む⽂字列の構築が可能となる コンストラクタで初期化すると⽂字列の⻑さに対して線形時間を要するのに対し リテラルオペレータは定数時間が保証されている ⽣⽂字列リテラルから構築するときは常にoperator svで良さそう #include<string_view>
intmain(){ usingnamespacestd::literals; std::string_viewsv1= a\0b\0c sv;//OK!size=5 std::string_viewsv2( a\0b\0c );//Oops!size=1 } -
メモ メモ なぜ、次のようなコンストラクタがないのか? 理由は、以下がよく⽰している バッファを多めにとってnull終端してからstring_viewを作っているのに バッファ分読みに⾏かれてれてしまう この理由から、配列の⻑さを⽂字列の⻑さとして⽤いるコンストラクタがない 参考 template<std::size_tn> constexprbasic_string_view(constCharT(&s)[n]):basic_string_view(s,n){}
charx[255]; sprintf(x, hellofolks ); //oops,sv.size()==255! std::string_viewsv(x); https://stackoverflow.com/questions/47337363/why-there-isnt-a- template-constructor-for-stdstring-view -
member types member types //types usingtraits_type=traits; usingvalue_type=charT; usingpointer=value_type*; usingconst_pointer=constvalue_type*; usingreference=value_type&;
usingconst_reference=constvalue_type&; usingconst_iterator=implementation-defined; usingiterator=const_iterator; usingconst_reverse_iterator=reverse_iterator<const_iterator>; usingreverse_iterator=const_reverse_iterator; usingsize_type=size_t; usingdifference_type=ptrdiff_t; staticconstexprsize_typenpos=size_type(-1); -
compare( 続き) compare( 続き) (1):⽂字列の全体同⼠の⽐較 (2):呼び出し元の部分⽂字列と引数の⽂字列全体の⽐較 substr(pos1,n1).compare(str)と等価 (3):部分⽂字列同⼠の⽐較 substr(pos1,n1).compare(str.substr(pos2,n2))と等価 constexprintcompare(basic_string_viewsv)constnoexcept;//(1)
constexprintcompare(size_typepos1, size_typen1, basic_string_viewsv)const;//(2) constexprintcompare(size_typepos1, size_typen1, basic_string_viewsv, size_typepos2, size_typen2)const;//(3) -
compare( 続き) compare( 続き) 基本的に引数がbasic_string_viewから⽂字列のポインタに変わっただけ (4)、(5)はsをbasic_string_view(s)にして先程のものを呼び出すものと等価である (6)は、ポインタが部分⽂字列の先頭を指すものとされており注意が必要 constexprintcompare(constCharT*s)const;//(4) constexprintcompare(size_typepos1,
size_typen1, constCharT*s)const;//(5) constexprintcompare(size_typepos1, size_typen1, constCharT*s, size_typen2)const;//(6) -
Searching Searching find, rfind, find_first_of, find_last_of, find_first_not_of, and find_last_not_of. find,
rfind, find_first_of, find_last_of, find_first_not_of, and find_last_not_of. -
find find 指定した位置posから、呼び出しもとの参照する⽂字列に含まれる部分⽂字列svが 最初に現れる位置を検索し、戻り値とする ⾒つからなければbasic_string_view::npos rfind rfind 指定した位置posから、呼び出しもとの参照する⽂字列に含まれる部分⽂字列svが 最後に現れる位置を検索し、戻り値とする ⾒つからなければbasic_string_view::npos
constexprsize_typefind(basic_string_viewsv,size_typepos=0)constnoexcept; constexprsize_typerfind(basic_string_views,size_typepos=npos)constnoexcept; -
find_first_of find_first_of 指定した位置posから、⽂字列sv内のいずれかの⽂字が 最初に現れる位置を検索し、戻り値とする ⾒つからなければbasic_string_view::npos find_last_of find_last_of 指定した位置posから、⽂字列sv内のいずれかの⽂字が 最後に現れる位置を検索し、戻り値とする ⾒つからなければbasic_string_view::npos
constexprsize_typefind_first_of(basic_string_viewsv,size_typepos=0)constnoexcept constexprsize_typefind_last_of(basic_string_viewsv,size_typepos=npos)constnoexce -
find_first_not_of find_first_not_of 指定した位置posから、⽂字列sv内のいずれの⽂字にも⼀致しない 最初に現れる位置を検索し、戻り値とする ⾒つからなければbasic_string_view::npos find_last_not_of find_last_not_of 指定した位置posから、⽂字列sv内のいずれの⽂字にも⼀致しない 最後に現れる位置を検索し、戻り値とする ⾒つからなければbasic_string_view::npos
constexprsize_typefind_first_not_of(basic_string_viewsv, size_typepos=0)constnoexcept; constexprsize_typefind_last_not_of(basic_string_viewsv, size_typepos=0)constnoexcept;