検索
ホームページphp教程php手册標準 C++ での string クラスの使用法の概要

標準 C++ での string クラスの使用法の概要

Jun 13, 2016 pm 08:04 PM
c++stringコードオープンソース標準プログラミングプログラミング言語ソフトウェア開発

MFC プログラミングを使用したことがある友人は、CString クラスに非常に感銘を受けるはずですよね?確かに、MFC の CString クラスは非常に便利で使いやすいです。しかし、MFC フレームワークを離れた場合、非常に便利に使用できるクラスはあるでしょうか?答えは「はい」です。 MFC フレームワークを使用しなくても、MFC の API を使用する方法はあるという人もいるかもしれません。具体的な操作方法はこの記事の最後に記載されています。実際、多くの人は標準 C++ での string クラスの使用を無視するかもしれません。標準 C++ で提供されている string クラスの機能も非常に強力で、プロジェクトを開発するときに一般的に使用できます。ここで、出発点として、いくつかの具体的な使用法をリストします。さて、ナンセンスな話はやめて、本題に移りましょう。

標準 C++ で文字列クラスを使用するには、

を含める必要があります

#include // ではなく であることに注意してください。.h が付いたものは C 言語のヘッダー ファイル

です。

std::string を使用;

std::wstring を使用する;

または

名前空間 std を使用します;

これで、それぞれ char と wchar_t に対応する string/wstring を使用できるようになります。

string と wstring の使用法は同じです。以下では、導入に string のみを使用します。


文字列クラスのコンストラクター:

string(const char *s); //c で初期化 string s

string(int n,char c); //n 文字で初期化 c
さらに、文字列クラスはデフォルトの構造もサポートしますstring s1; string s2="hello"; などの関数とコピー コンストラクターはすべて正しい書き方です。構築された文字列が長すぎて表現できない場合、length_error 例外がスローされます。

文字列クラスの文字操作:
const char &operator[](int n)const;const char &at(int n)const;
char &operator[ ](int n);
char &at(int n);
operator[] と at() はどちらも現在の文字列の n 番目の文字の位置を返しますが、at 関数は範囲チェックを提供し、範囲外の例外、添字演算子 [] はチェックされたアクセスを提供しません。
const char *data()const;//null で終了しない c 文字配列を返します
const char *c_str()const;//null で終了する c string
int copy( char *s , int n, int pos = 0) const;//現在の文字列の pos で始まる n 文字を s で始まる文字配列にコピーし、実際のコピー数を返します

文字列の特性の説明:
int Capacity()const; // 現在の容量 (つまり、メモリを追加せずに文字列に格納できる要素の数) を返します。 int max_size()const; //文字列オブジェクトに格納できる最大の文字列の長さを返します
int size()const; //現在の文字列のサイズを返します
int length( )const; //現在の文字列の長さを返します
bool empty()const; //現在の文字列が空かどうか
voidsize(int len,char c);//現在のサイズを設定します文字列を len に代入し、足りない長さを文字 c Part
で埋めます。

文字列クラスの入出力操作:

文字列クラスのオーバーロードされた演算子 Operator>> が入力に使用され、同じオーバーロードされた演算子 Operator関数 getline(istream &in,string &s); は、改行文字 'n' で区切られた文字列を入力ストリーム in から s に読み取るために使用されます。

文字列割り当て:

string &operator=(const string &s);//文字列 s を現在の文字列に割り当てます string &assign(const char *s);// 値を割り当てますwith c type string s
string &assign(const char *s,int n);//c から始まる n 文字の値を代入 string s
string &assign(const string &s);//文字を代入 String s は現在の文字列に割り当てられます
string &assign(int n,char c);//n 文字 c を現在の文字列に割り当てます
string &assign(const string &s,int start,int n);/ /Assign文字列 s の start から現在の文字列までの n 文字
string &assign(const_iterator first, const_itertor last);//最初と最後のイテレータの間の部分を文字列
に代入します

文字列接続:

string &operator+=(const string &s);//文字列 s を現在の文字列の末尾に接続します string &append(const char *s); // c 型の string s を現在の文字列の末尾に接続します
string &append(const char *s,int n) //c 型の string s の最初の n 文字を現在の文字列の末尾に接続します
string &append(const string &s); //operator+=()
string と同じ &append(const string &s,int pos,int n);//文字列 s の pos から始まる n 文字を現在の文字列の末尾に接続します。 string
string &append(int n,char c); //現在の文字列の末尾に n 文字 c を追加します
string &append(const_iterator first, const_iterator last);//最初と最後のイテレータを変更しますbetween は現在の文字列
の末尾に接続されます


文字列比較:
bool演算子==(const string &s1,const string &s2)const;//2つの文字列が等しいかどうかを比較します
operator"> "、"="、"int Compare(const string &s) const;//現在の文字列と Size
int を比較します。 Compare(int pos, int n,const string &s)const;//pos から始まる n 文字で構成される現在の文字列を s
int のサイズと比較しますcompare(int pos, int n,const string &s,int pos2 ,int n2)const;//現在の文字列の pos から始まる n 文字からなる文字列と s の

を比較します

//pos2 から始まる n2 文字からなる文字列のサイズ
int Compare(const char *s) const;
int Compare(int pos, int n, const char *s) const;
int Compare(int pos, int n, const char *s, int pos2) const;
比較関数は、> の場合は 1、 の場合は 0 を返します。


文字列の部分文字列:
string substr(int pos = 0, int n = npos) const;//pos から始まる n 文字で構成される文字列を返します

文字列交換:
void swap(string &s2) //現在の文字列と s2 の値を交換します


文字列クラスの検索関数:
int find(char c, int pos = 0) const;//pos から開始して、現在の文字列内の文字 c の位置を検索します
int find(const char *s, int pos = 0) const;//pos から始まる現在の文字列内の文字列 s の位置を検索します
int find(const char *s, int pos, int n) const; // pos から開始して、現在の文字列 s の最初の n 文字の位置を検索します。
int find(const string &s, int pos = 0) const;現在の文字列内の文字列 s の位置
//検索が成功した場合は位置に戻り、そうでない場合は string::npos
int rfind(char c, int pos = npos) const; の値を返します。 //pos から開始し、現在の文字列内の位置を後ろから前に検索します。
int rfind(const char *s, int pos = npos) const;
int rfind(const char *s, int) pos, int n = npos) const;
int rfind (const string &s,int pos = npos) const;
//pos から開始して、最初の n で構成される文字列の位置を前後に検索します。成功した場合は、現在の文字列内の文字列 s の位置が返されます。失敗した場合は、string::npos
int find_first_of(char c, int pos = 0) const;// から開始します。 pos で最初に出現する文字を検索します。 c
int find_first_of(const char *s, int pos = 0) const;
int find_first_of(const char *s, int pos, int n) const;
int find_first_of(const string &s,int pos = 0) const;
//pos から開始 s の最初の n 文字で構成される配列内の現在の文字列の最初の文字の位置を見つけます。検索が失敗した場合、 string::npos
int find_first_not_of(char c, int pos = 0) const;
int find_first_not_of(const char *s, int pos = 0) const;
int find_first_not_of(const char * s, int pos,int n) const;
int find_first_not_of(const string &s,int pos = 0) const;
//現在の文字列から string s にない最初の文字の位置を検索します。 string、失敗は string::npos を返します
int find_last_of(char c, int pos = npos) const;
int find_last_of(const char *s, int pos = npos) const;
int find_last_of(const char *s , int pos, int n = npos) const;
int find_last_of(const string &s,int pos = npos) const;
int find_last_not_of(char c, int pos = npos) const;
int find_last_not_of( const char *s, int pos = npos) const;
int find_last_not_of(const char *s, int pos, int n) const;
int find_last_not_of(const string &s,int pos = npos) const;
//find_last_of と find_last_not_of は、後ろから前に検索する点を除いて、find_first_of と find_first_not_of に似ています


文字列クラスの置換関数:
string &replace(int p0, int n0, const char *s);//p0 から始まり p0 にある n0 文字を削除します。 string s
string &replace(int p0, int n0, const char *s, int n);//p0 から n0 文字を削除し、p0 に string sstring &replace( int p0, int n0,const string &s);//p0 から n0 文字を削除し、p0 に string s を挿入
string &replace(int p0, int n0,const string &s, int pos, int n); //p0 から n0 文字を削除し、文字列 s の p0
string &replace(int p0, int n0,int n, char c) //p0 から n0 文字を削除,次に、p0 に n 文字を挿入します。 c
string &replace(iterator first0, iterator last0,const char *s);//[first0, last0) の間の部分を置換します。文字列 s
string &replace(iterator first0, iterator last0,const char *s, int n);//[first0, last0) の間の部分を s
string の最初の n 文字に置き換えます &replace (iterator first0, iterator last0, const string &s);//Replace [first0, last0) の間の部分を文字列 s
string &replace(iterator first0, iterator last0,int n, char c);//Replace [first0, last0) の間の部分を n 文字で置き換えます c
string &replace(iterator first0, iterator last0, const_iterator first, const_iterator last);// [first0, last0) の間の部分を [first , last) string
に置き換えます


文字列クラスの挿入関数:
string &insert(int p0, const char *s);
string &insert(int p0, const char *s, int n) ;
string &insert(int p0,const string &s);
string &insert(int p0,const string &s, int pos, int n);
//最初の 4 つの関数は位置 p0 に文字列を挿入します。 s
string の pos から始まる最初の n 文字 &insert(int p0, int n, char c);//この関数は p0 に n 文字を挿入します c
iterator insert(iterator it, char c); //Insert文字 c を挿入し、挿入後のイテレータの位置を返します
void insert(iterator it, const_iterator first, const_iterator last);//it の [first, last) の間に文字を挿入します
void insert(iterator it, int n, char c);//n 文字 c

を挿入します


文字列クラスの削除関数
iterator Erase(iterator first, iterator last);//[first, last) の間のすべての文字を削除し、削除後のイテレータを返します Position
iterator Erase(iterator it);//それが指す文字を削除し、削除後のイテレータの位置を返す
string &erase(int pos = 0, int n = npos);//pos から始まる n を削除文字の場合、変更された文字列

を返します


文字列クラスのイテレータ処理:
文字列クラスは、ポインタと同様に、個々の文字にアクセスするための構文を提供します。 、反復子は範囲をチェックしません。
string::iterator または string::const_iterator を使用して反復子変数を宣言する場合、const_iterator では反復の内容を変更できません。一般的に使用される反復子関数は次のとおりです。
const_iterator begin()const;
iterator begin(); //string の開始位置を返します
const_iterator end()const; 🎜>const_iterator rbegin()const;
iterator rbegin(); // 文字列の最後の文字の位置を返します
const_iterator rend()const;
iterator rend (); // 先頭に戻ります
を実装するイテレータ string :: reverse_iterator、string :: const_reverse_iterator を設定することにより、文字列の最初の文字位置
RBEGIN と Rend を後方からの反復アクセスに使用します。

文字列ストリーム処理:
ヘッダー ファイル で #include
を定義し、ostringstream 変数と isstringstream 変数を定義することによって実現されます。 例: string input("hello 、これはテストです");
isstringstream is(input);
string s1,s2,s3,s4;
is>>s1>>s2>>s3>>s4;//s1= " hello,this",s2="is",s3="a",s4="test"
ostringstream os;
os cout 上記は、C++ 文字列クラスの簡単な紹介です。うまく使えば、その機能は MFC の CString クラスとそれほど遜色ありません (笑)、個人的な意見です。

最後に、Win32 アプリケーションで CString などの MFC の一部のクラスを参照する方法を紹介します。

1. プロジェクト ディレクトリを右クリックし、[プロパティ]--->[構成プロパティ]--->[全般]--->[MFC の使用]--->[MFC を使用する]を選択します。静的ライブラリ" ,

デフォルトは、以下に示すように「標準 Windows ライブラリを使用する」です:

標準 C++ での string クラスの使用法の概要2. 使用するすべてのヘッダー ファイルの前に #include を含めます。たとえば、ソース コードで使用できるように、#include ヘッダー ファイルを stdafx.h ファイルの先頭に含めることができます。

CString クラスですが、これには欠点もあります。つまり、コンパイルされたプログラムは元のプログラムよりもはるかに大きくなります。小さなプログラムを試し、「標準 Windows ライブラリを使用する」を選択してコンパイルしました

のリリース バージョンは約 92 kb、「静的ライブラリで MFC を使用する」を使用してコンパイルされたリリース バージョンは約 192 kb で、これは個人的な考慮事項です...

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

WebStorm Mac版

WebStorm Mac版

便利なJavaScript開発ツール

SecLists

SecLists

SecLists は、セキュリティ テスターの究極の相棒です。これは、セキュリティ評価中に頻繁に使用されるさまざまな種類のリストを 1 か所にまとめたものです。 SecLists は、セキュリティ テスターが必要とする可能性のあるすべてのリストを便利に提供することで、セキュリティ テストをより効率的かつ生産的にするのに役立ちます。リストの種類には、ユーザー名、パスワード、URL、ファジング ペイロード、機密データ パターン、Web シェルなどが含まれます。テスターはこのリポジトリを新しいテスト マシンにプルするだけで、必要なあらゆる種類のリストにアクセスできるようになります。

Dreamweaver Mac版

Dreamweaver Mac版

ビジュアル Web 開発ツール

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。