Lang/C++/C++11/コンテナ/string
の編集
[
トップ
] [
編集
|
差分
|
履歴
|
添付
|
リロード
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
]
-- 雛形とするページ --
App
App/archiver
App/audio
App/audio/audacity
App/audio/mpg321
App/audio/puddletag
App/base64
App/Browser
App/column
App/diff-patch
App/downloader
App/ffmpeg
App/ffprobe
App/find
App/gimp
App/gnuplot
App/ImageMagick
App/info
App/inxi
App/LibreOffice
App/locate
App/md5
App/Music
App/Music/frescobaldi
App/Music/MuseScore
App/OCR
App/od
App/OpenOffice.org
App/pagers
App/pdf
App/QRコード
App/ripgrep
App/screen
App/script
App/sed
App/selenium
App/signal-desktop
App/skype
App/sylpheed
App/tmux
App/ttyrec
App/uniq
App/UUID
App/wget
App/xargs
App/テキスト処理
App/仮想化
App/仮想化/Docker
App/仮想化/qemu
App/仮想化/webos
AutoTicketLinkName
BracketName
CentOS
CentOS/yum
CrossCompile
Debian
Debian/kernel
Debian/kernel/ver8-or-older
Debian/Plan9関連
Debian/ver5
Debian/ver6
Debian/ver7
Debian/ver8
Debian/ver9
Debian/ver10
Debian/ver11
Debian/ver12
Debian/X
Debian/X/日本語
Debian/パッケージ管理
Deveice/Sensor/MotionSensor
Device
Device/Mouse
Device/Net/wifi
Device/Scanner
Device/Sensor
Device/Sensor/MotionSensor
Device/tty
Device/xmodmap
Editor
Editor/atom
Editor/binary
Editor/binary/beav
Editor/binary/hexedit
Editor/emacs
Editor/emacs/clang-format
Editor/emacs/flycheck
Editor/emacs/gdb-mode
Editor/emacs/github/copilot
Editor/emacs/html-mode
Editor/emacs/lisp
Editor/emacs/lsp-mode
Editor/emacs/markdown-mode
Editor/emacs/neotree
Editor/emacs/package-install
Editor/emacs/sdic
Editor/emacs/speedbar
Editor/emacs/XEmacs
Editor/emacs/ローカルビルド
Editor/emacs/多言語
Editor/fte
Editor/vi
FormattingRules
FrontPage
Help
InterWiki
InterWikiName
InterWikiSandBox
iPhone
Lang
Lang/C++
Lang/C++/Boost
Lang/C++/C++11
Lang/C++/C++11/thread
Lang/C++/C++11/コンテナ
Lang/C++/C++11/コンテナ/string
Lang/C++/C++11/初期化子
Lang/C++/C++17
Lang/C++/C++20
Lang/C++/error
Lang/C++/macro
Lang/C++/template
Lang/C++/コンテナ
Lang/C++/処理系
Lang/C-Sharp
Lang/Go
Lang/html
Lang/html/5
Lang/html/5/canvas
Lang/html/css
Lang/Java
Lang/Java/jar
Lang/Objective-C
Lang/Pascal
Lang/Perl
Lang/Perl/CGI
Lang/Perl/thread
Lang/Perl/utf-8
Lang/pike
Lang/Python
Lang/Python/Class
Lang/Python/Error
Lang/Python/import対象パス
Lang/Python/pip
Lang/Python/基本文法
Lang/Python/開発環境
Lang/Rust
Lang/sed
Lang/shell
Lang/shell/bash
Lang/shell/bash/設定
Lang/shell/bash/設定/command-not-found
Lang/Tcl_Tk_Expect
Lang/Tcl_Tk_Expect/Ctrl-キーマップ
Lang/TypeScript
Lang/xml
Mac
Memo
memo
MenuBar
midi
Multimedia
Net
Net/apache
Net/apache2
Net/dig
Net/dns/server
Net/domain
Net/http/アクセス制御
Net/iptables
Net/IRC
Net/Jenkins
Net/lighttpd
Net/mail
Net/mail/X-Face
Net/news
Net/news/gnus
Net/ntp
Net/pukiwiki
Net/pukiwiki/plugin
Net/resolv.conf
Net/SPAM
Net/ss
Net/tcpdump
Net/traceroute
Net/twitter
Net/vpn
Net/web
Net/webAPI
Net/wifi
Net/wifi/AP
Net/監視
otherlinks
PHP
Plan9
PPC
PPC/LinuxPPC
PPC/MkLinux
Prog
Prog/AI
Prog/autotools
Prog/cmake
Prog/Compiler/gcc
Prog/CrossReferencer
Prog/debug
Prog/debug/gdb
Prog/debug/lldb
Prog/debug/ltrace
Prog/debug/memory
Prog/debug/strace
Prog/formatter/clang-format
Prog/GoogleTest
Prog/Gtk
Prog/IDE
Prog/IDE/Geany
Prog/ldd
Prog/make
Prog/nm
Prog/Profiler
Prog/Qt
Prog/readelf
Prog/Sound
Prog/StaticCodeAnalysis
Prog/StaticCodeAnalysis/cppcheck
Prog/strip
Prog/TagSystem
Prog/TagSystem/cscope
Prog/TagSystem/ctags-exuberant
Prog/TagSystem/ebrowse
Prog/TagSystem/etags
Prog/TagSystem/gtags
Prog/TagSystem/id-utils
Prog/TagSystem/lctags
Prog/TagSystem/rtags
Prog/TagSystem/SilentBob
Prog/X/X11
Prog/プロセス間通信
Prog/プロセス間通信/SharedMemory
Prog/プロセス間通信/singal
Prog/人為的なシグナル
Prog/分散ビルド/distcc
Prog/逆アセンブル
PukiWiki
PukiWiki/1.4
PukiWiki/1.4/Manual
PukiWiki/1.4/Manual/Plugin
PukiWiki/1.4/Manual/Plugin/A-D
PukiWiki/1.4/Manual/Plugin/E-G
PukiWiki/1.4/Manual/Plugin/H-K
PukiWiki/1.4/Manual/Plugin/L-N
PukiWiki/1.4/Manual/Plugin/O-R
PukiWiki/1.4/Manual/Plugin/S-U
PukiWiki/1.4/Manual/Plugin/V-Z
RecentDeleted
RS232C
SandBox
Security
Security/aide
Security/chkrootkit
Security/clamav
Security/gpg
Security/pkcs
Security/ssh
Security/test
Security/パスワード解析
System
System/Audio
System/beep
System/bluetooth
System/cpu
System/dm-crypt
System/fs
System/fs/ext2-3
System/fs/mount
System/fs/nfs
System/fs/tmpfs
System/fs/xfs
System/fs/大容量のファイルを高速に作成する
System/fs/強制物理フォーマット
System/grub
System/Hardware情報
System/HDD/IDE/チューニング
System/input
System/iostat
System/Kernel
System/Kernel/Module
System/locale
System/memory
System/OpenCL
System/os
System/power/battery
System/Process
System/random
System/rpm
System/runlevel
System/signal
System/sudo
System/swap
System/USB-Boot
System/user
System/シリアル接続
System/環境変数
TeX
TeX/MusiXTeX
tools
Top/Editor/emacs/python開発環境
transcription/old
Ubuntu
VersionCtl
VersionCtl/git
VersionCtl/git/Gerrit
VersionCtl/git/Gerrit/old
VersionCtl/git/git-gutter+
VersionCtl/git/git-imerge
VersionCtl/git/magit
VersionCtl/git/magit/2.1.0
VersionCtl/git/magit/2.8.0
VersionCtl/git/magit/2.13.1
VersionCtl/git/magit/v3.2.1
VersionCtl/git/magit/v3.3.0
VersionCtl/git/subversion からの移行
VersionCtl/git/subversion とのコマンド対比
VersionCtl/git/tig
VersionCtl/git/統計
VersionCtl/hg
VersionCtl/Mercurial
VersionCtl/repo
VersionCtl/Subversion
WikiEngines
WikiName
WikiWikiWeb
Windows
Windows/cmd
X
X/DisplayManager
X/DisplayManager/gdm3
X/DisplayManager/lightdm
X/font
X/font/old
X/GNOME3
X/input
X/input/OnScreenKeyboard
X/input/キーアサインを変更する
X/input/グラゴル文字
X/input/日本語
X/input/日本語/ATOK X3
X/input/日本語/ibus
X/input/日本語/uim
X/KDE
X/remote
X/screensaver
X/terminal
X/terminal/mlterm
X/terminal/urxvt
X/terminal/uxterm
X/vnc
X/vnc/tigervnc
X/WindowManager
X/WindowManager/9wm
X/WindowManager/amiwm
X/WindowManager/awesome
X/WindowManager/CDE
X/WindowManager/cinnamon
X/WindowManager/dwm
X/WindowManager/flwm
X/WindowManager/i3
X/WindowManager/jwm
X/WindowManager/matchbox
X/WindowManager/MATE
X/WindowManager/mlvwm
X/WindowManager/qvwm
X/WindowManager/ratpoison
X/WindowManager/ude
X/WindowManager/WindowMaker
X/WindowManager/xfce4
X/xdotool
X/Xnest
X/xrdp
X/xvfb
YukiWiki
スタジオ・ホール
旅
旅/Czech
旅/三重
旅/京都・滋賀
旅/兵庫
旅/千葉
旅/各務
旅/和歌山
旅/埼玉
旅/山形
旅/岐阜
旅/岡山
旅/岩手
旅/島根
旅/愛媛
旅/新潟
旅/東京
旅/栃木
旅/神奈川
旅/秋田
旅/群馬
旅/茨城
旅/長野・山梨
旅/静岡
旅/静岡/伊豆
未整理
食
食/チェコ
食/各国
食/料理
食/激辛
食/蕎麦
食/郷土料理
#topicpath(); c++11 相当の文字列型全般 ////////////////////////////////////////////////////////////////////////////// * 目次 [#f82a4887] #contents(); ////////////////////////////////////////////////////////////////////////////// * コンストラクタ [#j24451d7] |~example code |~description |~remark | |string a; |空の文字列を生成 || |string b = "example"; | 右辺に渡された文字列で生成 || |string c( b, 1, 3 ); |元になる文字列 b の1番目から3番目の部分文字列から文字列を生成 || |string d( "example", 2 ); |文字列先頭2文字からなる文字列を生成 || |string e( 10, 't' ); |10個の文字 't' からなる文字列を生成 || |string f = { 'e, 'x', 'a' }; |文字の初期化子リストから文字列を生成|| |string g = b; |string 変数 b のコピーを構築 || |string i( b.begin(), b.end() ); |string 変数 b の iterator によって指定された範囲から文字列を生成 |逆順を指定すると、gcc 4.9.2 では basic_string::_S_create が "std::length_error" でコアダンプする | ////////////////////////////////////////////////////////////////////////////// * 比較 [#ze22d38d] - 2つの string 変数同士の比較には、以下の演算子が使用出来る == != < > <= >= - 大小比較については文字コード順に行われる。 ////////////////////////////////////////////////////////////////////////////// * 連結 [#wb027a67] - 文字列同士の連結には + 演算子を使用する ////////////////////////////////////////////////////////////////////////////// * 文字列長の取得 [#gf9e5d1c] - 以下の2つの関数がある。これらに挙動の差異はない。 -- size_type std::basic_string::length() -- size_type std::basic_string::size() ////////////////////////////////////////////////////////////////////////////// * 文字列を空にする [#qf83482d] - void std::basic_string::clear() ////////////////////////////////////////////////////////////////////////////// * empty(): 空文字かどうかを判定する [#n0279c8f] - bool std::base_string::empty() ////////////////////////////////////////////////////////////////////////////// * 文字列ポインタを取得する [#y0eb62c6] - std::basic_string::c_str() -- 末尾の NULL 文字を含めた文字列を返す。 - std::basic_string::data() -- C++11になる前までは、末尾のNULL文字を含めない仕様になっていた。 - c_str(), data() とも、''文字列オブジェクトの内容が変化した時には無効になる'' ////////////////////////////////////////////////////////////////////////////// * iterator による操作 [#f3406696] - 1文字ごとに操作 - 順方向 : begin(), end() -- 返されるiterator が const になるか否かは、オブジェクトの状態によって異なる。 - 逆順方向: rbegin(), rend() - const_iterator:cbegin(), cend() -- オブジェクトの状態に関係なく const_iterator を返す ////////////////////////////////////////////////////////////////////////////// * 部分文字列の取得 [#q04f94f0] - 書式: basic_str::substr( size_type pos = 0, size_type n = npos ) const; -- pos: 取得したい部分の先頭 index -- n : 取得する長さ - pos > size() の場合、std::out_of_range 例外が送出される ////////////////////////////////////////////////////////////////////////////// * 数値 ←→文字列変換 [#d86b4551] - [[数値→文字列>Lang/C++/C++11#to_string]] - [[文字列→数値>Lang/C++/C++11#sto]] ////////////////////////////////////////////////////////////////////////////// * 文字列検索 [#search-str] //============================================================================ ** 文字列検索し、最初に見つかった位置を取得する [#find] - 書式: size_type find( const basic_string& str, size_type pos = 0 ) const noexcept; str: 検索文字列 pos: 検索開始位置 n : 検索する文字列の長さ size_type find( const CharT* s, size_type pos, size_type n ) const; s : 検索文字列 pos: 検索開始位置 n : 検索する文字列の長さ size_type find( const CharT* s, size_type pos = 0 ) const noexcept; s : 検索文字列 pos: 検索開始位置 n : 検索する文字列の長さ size_type find( CharT c, size_type pos = 0 ) const noexcept; c : 検索文字 pos: 検索開始位置 n : 検索する文字列の長さ //============================================================================ ** 文字列検索し、最後に見つかった位置を取得する [#rfind] - 書式: size_type rfind( const basic_string& str, size_type pos = npos ) const noexcept; str: 検索文字列 pos: 検索開始位置 n : 検索する文字列の長さ size_type rfind( const CharT* s, size_type pos, size_type n ) const; s : 検索文字列 pos: 検索開始位置 n : 検索する文字列の長さ size_type rfind( const CharT* s, size_type pos = npos ) const; s : 検索文字列 pos: 検索開始位置 n : 検索する文字列の長さ size_type rfind( CharT c, size_type pos = npos ) const noexcept; c : 検索文字 pos: 検索開始位置 n : 検索する文字列の長さ - rfind() の pos は、検索範囲のうち最も大きな index を指定する //============================================================================ ** 文字群の中から最初に見つかった index を取得する [#e056c189] - 書式: //============================================================================ ** 文字群の中から最後に見つかった index を取得する [#fc698687] - 書式: ////////////////////////////////////////////////////////////////////////////// * std::wstring [#wstring] - wchar_t 型文字列 - 文字コードの規定はないが、wchat_t が2バイトの環境では UTF-16 の文字列として使われてきた。 ////////////////////////////////////////////////////////////////////////////// * std::u16string [#u16string] - char16_t 型文字列 - 文字コードは UTF-16 - see also: [[std::string>Lang/C++/コンテナ#string]], [[std::wstring>Lang/C++/コンテナ#wstring]] ////////////////////////////////////////////////////////////////////////////// * std::u32string [#u32string] - char32_t 型文字列 - 文字コードは UTF-32 - see also: [[std::string>Lang/C++/コンテナ#string]], [[std::wstring>Lang/C++/コンテナ#wstring]] ////////////////////////////////////////////////////////////////////////////// * printf のように文字列をフォーマットする [#ie23e55e] - C++11 で導入された可変引数テンプレートを使って、以下の関数を実装する: #include <string> #include <cstdio> #include <vector> template <typename ... Args> std::string format( const std::string& fmt, Args ... args ) { size_t len = std::snprintf( nullptr, 0, fmt.c_str(), args ... ); std::vector<char> buff( len + 1 ); std::snprintf( &buff[0], len + 1, fmt.c_str(), args ... ); return std::string( &buff[0], &buff[0] + len );- } - 呼び出し例 #include <iostream> int main(int argc, char *argv[]) { int aaa = 120; double ad = 23.55; std::string buff = format("test data = %d, ad = %f\n", aaa, ad); std::cout << buff << std::endl; return 0; }
#topicpath(); c++11 相当の文字列型全般 ////////////////////////////////////////////////////////////////////////////// * 目次 [#f82a4887] #contents(); ////////////////////////////////////////////////////////////////////////////// * コンストラクタ [#j24451d7] |~example code |~description |~remark | |string a; |空の文字列を生成 || |string b = "example"; | 右辺に渡された文字列で生成 || |string c( b, 1, 3 ); |元になる文字列 b の1番目から3番目の部分文字列から文字列を生成 || |string d( "example", 2 ); |文字列先頭2文字からなる文字列を生成 || |string e( 10, 't' ); |10個の文字 't' からなる文字列を生成 || |string f = { 'e, 'x', 'a' }; |文字の初期化子リストから文字列を生成|| |string g = b; |string 変数 b のコピーを構築 || |string i( b.begin(), b.end() ); |string 変数 b の iterator によって指定された範囲から文字列を生成 |逆順を指定すると、gcc 4.9.2 では basic_string::_S_create が "std::length_error" でコアダンプする | ////////////////////////////////////////////////////////////////////////////// * 比較 [#ze22d38d] - 2つの string 変数同士の比較には、以下の演算子が使用出来る == != < > <= >= - 大小比較については文字コード順に行われる。 ////////////////////////////////////////////////////////////////////////////// * 連結 [#wb027a67] - 文字列同士の連結には + 演算子を使用する ////////////////////////////////////////////////////////////////////////////// * 文字列長の取得 [#gf9e5d1c] - 以下の2つの関数がある。これらに挙動の差異はない。 -- size_type std::basic_string::length() -- size_type std::basic_string::size() ////////////////////////////////////////////////////////////////////////////// * 文字列を空にする [#qf83482d] - void std::basic_string::clear() ////////////////////////////////////////////////////////////////////////////// * empty(): 空文字かどうかを判定する [#n0279c8f] - bool std::base_string::empty() ////////////////////////////////////////////////////////////////////////////// * 文字列ポインタを取得する [#y0eb62c6] - std::basic_string::c_str() -- 末尾の NULL 文字を含めた文字列を返す。 - std::basic_string::data() -- C++11になる前までは、末尾のNULL文字を含めない仕様になっていた。 - c_str(), data() とも、''文字列オブジェクトの内容が変化した時には無効になる'' ////////////////////////////////////////////////////////////////////////////// * iterator による操作 [#f3406696] - 1文字ごとに操作 - 順方向 : begin(), end() -- 返されるiterator が const になるか否かは、オブジェクトの状態によって異なる。 - 逆順方向: rbegin(), rend() - const_iterator:cbegin(), cend() -- オブジェクトの状態に関係なく const_iterator を返す ////////////////////////////////////////////////////////////////////////////// * 部分文字列の取得 [#q04f94f0] - 書式: basic_str::substr( size_type pos = 0, size_type n = npos ) const; -- pos: 取得したい部分の先頭 index -- n : 取得する長さ - pos > size() の場合、std::out_of_range 例外が送出される ////////////////////////////////////////////////////////////////////////////// * 数値 ←→文字列変換 [#d86b4551] - [[数値→文字列>Lang/C++/C++11#to_string]] - [[文字列→数値>Lang/C++/C++11#sto]] ////////////////////////////////////////////////////////////////////////////// * 文字列検索 [#search-str] //============================================================================ ** 文字列検索し、最初に見つかった位置を取得する [#find] - 書式: size_type find( const basic_string& str, size_type pos = 0 ) const noexcept; str: 検索文字列 pos: 検索開始位置 n : 検索する文字列の長さ size_type find( const CharT* s, size_type pos, size_type n ) const; s : 検索文字列 pos: 検索開始位置 n : 検索する文字列の長さ size_type find( const CharT* s, size_type pos = 0 ) const noexcept; s : 検索文字列 pos: 検索開始位置 n : 検索する文字列の長さ size_type find( CharT c, size_type pos = 0 ) const noexcept; c : 検索文字 pos: 検索開始位置 n : 検索する文字列の長さ //============================================================================ ** 文字列検索し、最後に見つかった位置を取得する [#rfind] - 書式: size_type rfind( const basic_string& str, size_type pos = npos ) const noexcept; str: 検索文字列 pos: 検索開始位置 n : 検索する文字列の長さ size_type rfind( const CharT* s, size_type pos, size_type n ) const; s : 検索文字列 pos: 検索開始位置 n : 検索する文字列の長さ size_type rfind( const CharT* s, size_type pos = npos ) const; s : 検索文字列 pos: 検索開始位置 n : 検索する文字列の長さ size_type rfind( CharT c, size_type pos = npos ) const noexcept; c : 検索文字 pos: 検索開始位置 n : 検索する文字列の長さ - rfind() の pos は、検索範囲のうち最も大きな index を指定する //============================================================================ ** 文字群の中から最初に見つかった index を取得する [#e056c189] - 書式: //============================================================================ ** 文字群の中から最後に見つかった index を取得する [#fc698687] - 書式: ////////////////////////////////////////////////////////////////////////////// * std::wstring [#wstring] - wchar_t 型文字列 - 文字コードの規定はないが、wchat_t が2バイトの環境では UTF-16 の文字列として使われてきた。 ////////////////////////////////////////////////////////////////////////////// * std::u16string [#u16string] - char16_t 型文字列 - 文字コードは UTF-16 - see also: [[std::string>Lang/C++/コンテナ#string]], [[std::wstring>Lang/C++/コンテナ#wstring]] ////////////////////////////////////////////////////////////////////////////// * std::u32string [#u32string] - char32_t 型文字列 - 文字コードは UTF-32 - see also: [[std::string>Lang/C++/コンテナ#string]], [[std::wstring>Lang/C++/コンテナ#wstring]] ////////////////////////////////////////////////////////////////////////////// * printf のように文字列をフォーマットする [#ie23e55e] - C++11 で導入された可変引数テンプレートを使って、以下の関数を実装する: #include <string> #include <cstdio> #include <vector> template <typename ... Args> std::string format( const std::string& fmt, Args ... args ) { size_t len = std::snprintf( nullptr, 0, fmt.c_str(), args ... ); std::vector<char> buff( len + 1 ); std::snprintf( &buff[0], len + 1, fmt.c_str(), args ... ); return std::string( &buff[0], &buff[0] + len );- } - 呼び出し例 #include <iostream> int main(int argc, char *argv[]) { int aaa = 120; double ad = 23.55; std::string buff = format("test data = %d, ad = %f\n", aaa, ad); std::cout << buff << std::endl; return 0; }
テキスト整形のルールを表示する