中国大学MOOCプログラム設計とアルゴリズム(三):C++対象向けプログラム設計第8週標準テンプレートライブラリSTL(一)ノートのstring類

42666 ワード

第8週標準テンプレートライブラリSTL(一)1.stringクラス2.標準テンプレートライブラリSTLの概要(一)3.標準テンプレートライブラリSTLの概要(二)4.vector,deque,list 5.関数オブジェクト
1.stringクラス
stringクラス
stringクラスはテンプレートクラス:typedef basic_string string; stringクラスを使用してヘッダファイルを含める
stringオブジェクトの初期化:
string s1("Hello");
string month = "March";
string s2(8,'x');//s2 = "xxxxxxxx";

エラーの初期化方法:
string error1 =  'c'; //  
string error2('u'); //  
string error3 = 22; //  
string error4(8); //  

stringオブジェクトに文字を割り当てることができます
string s;
s = 'n';

例:
#include 
#include 
using namespace std;
int main(int argc, char* argv[ ]){
	string s1("Hello");
	cout << s1 << endl;
	string s2(8,'x');
	cout << s2 << endl;
	string month = "March";
	cout << month << endl;
	string s;
	s='n';
	cout << s << endl;
	return 0;
}
  :
Hello
xxxxxxxx
March
n

stringオブジェクトの長さをメンバー関数length()で読み出す.
string s("hello");
cout << s.length() << endl;

stringはストリーム読み出し演算子をサポートする
string stringObject;
cin >> stringObject;

stringはgetline関数をサポートする
string s;
getline(cin ,s);

stringの付与と接続
使用=割り当て
string s1("cat"), s2;
s2 = s1;//s1,s2   

assignメンバー関数でコピー
string s1("cat"), s3;
s3.assign(s1);

assignメンバー関数で部分的にコピー
string s1("catpig"), s3;
s3.assign(s1, 1, 3);// s1     1       3    s3

1文字のコピー
s2[5] = s1[3] = ‘a’;

stringオブジェクトへの個々のアクセス文字
string s1("Hello");
for(int i=0;i<s1.length();i++)
cout << s1.at(i) << endl;
//   cout << s1[i] << endl;

stringオブジェクトにアクセスする文字には、s[i]とs.at(i)の2つの方法があります.違いは、メンバー関数atが範囲チェックを行い、範囲を超えた場合out_を投げ出すことです.of_rangeは異常ですが、下付き演算子[]は範囲チェックをしません.
+演算子で文字列を接続
string s1("good "), s2("morning!");
s1 += s2;
cout << s1; //good morning!

メンバー関数appendで文字列を接続する
string s1("good "), s2("morning! ");
s1.append(s2);//s1 = "good morning!"
cout << s1;
s2.append(s1, 3, s1.size());//s1.size(),s1   
cout << s2;
//    3  ,s1.size()   ,            ,             

ひかくstring
リレーショナル演算子でstringのサイズを比較します.=,>,>=、戻り値はboolタイプで、戻りtrueが成立します.そうしないとfalseが返されます.たとえば、次のようにします.
string s1("hello"),s2("hello"),s3("hell");
bool b = (s1 == s2);
cout << b << endl;
b = (s1 == s3);
cout << b << endl;
b = (s1 > s3);
cout << b << endl;1
0
1

メンバー関数compareでstringのサイズを比較する
string s1("hello"),s2("hello"),s3("hell");
int f1 = s1.compare(s2);
int f2 = s1.compare(s3);
int f3 = s3.compare(s1);
int f4 = s1.compare(1,2,s3,0,3); //s1[1-2]; s3[0-3]
int f5 = s1.compare(0,s1.size(),s3);//s1 0-end
cout << f1 << endl << f2 << endl << f3 << endl;
cout << f4 << endl << f5 << endl;
  :
0 // hello == hello
1 // hello > hell
-1 // hell < hello
-1 // el < hell
1 // hello > hell

サブストリング
メンバー関数substr(i,length):下付きiの開始長さが5のサブ列で、終了を越えると終了までのみです.
string s1("hello world"), s2;
s2 = s1.substr(4,5); //   4  5   
cout << s2 << endl;
  :
o wor

スワップstring
メンバー関数swap
string s1("hello world"), s2("really");
s1.swap(s2);
cout << s1 << endl;
cout << s2 << endl;
  :
really
hello world

stringの文字を探します
メンバー関数find()は、s 1で「lo」が最初に現れた場所を前後に検索し、見つかった場合は「lo」の開始位置、すなわちlの位置の下付きを返します.見つからない場合はstring::npos(stringで定義された静的定数)を返します.
string s1("hello world");
s1.find("lo");

メンバー関数rfind()は、s 1で「lo」が最初に現れた場所を後方から前方に検索し、見つかった場合、「lo」の開始位置、すなわちlが位置する位置の下付き文字列の先頭に対する下付き文字列を返します.見つからない場合はstring::nposを返します.
string s1("hello world");
s1.rfind("lo");

find()は、検索を開始する場所を指定できますが、戻り値は先頭に対する下付きです.
string s1("hello worlld");
cout << s1.find("ll",1) << endl;
cout << s1.find("ll",2) << endl;
cout << s1.find("ll",3) << endl;
//      1,2,3    “ll”2
2
9

メンバー関数find_first_of()s 1で「abcd」のいずれかの文字が最初に現れた場所を前から後ろに検索し、見つかったらアルファベットの位置を返し、見つからなかったらstring::nposを返します.
string s1("hello world");
s1.find_first_of(“abcd");

メンバー関数find_last_of()s 1で「abcd」のいずれかの文字が最後に現れた場所を検索し、見つかったらアルファベットの位置を返し、見つからなかったらstring::nposを返します.
string s1("hello world");
s1.find_last_of(“abcd");

メンバー関数find_first_not_of()s 1で「abcd」にないアルファベットが最初に現れた場所を前から後ろに探し、見つかったらアルファベットが見つかった場所を返し、見つからなかったらstring::nposを返します.
string s1("hello world");
s1.find_first_not_of(“abcd");//      'h'   

メンバー関数find_last_not_of()s 1で「abcd」にないアルファベットが最初に現れた場所を後から前へ検索し、見つかったらアルファベットが見つかった場所を返し、見つからなかったらstring::nposを返します.
string s1("hello world");
s1.find_last_not_of(“abcd");
string s1("hello worlld");
cout << s1.find("ll") << endl;
cout << s1.find("abc") << endl;
cout << s1.rfind("ll") << endl;
cout << s1.rfind("abc") << endl;
cout << s1.find_first_of("abcde") << endl;
cout << s1.find_first_of("abc") << endl;
cout << s1.find_last_of("abcde") << endl;
cout << s1.find_last_of("abc") << endl;
cout << s1.find_first_not_of("abcde") << endl;
cout << s1.find_first_not_of("hello world") << endl;
cout << s1.find_last_not_of("abcde") << endl;
cout << s1.find_last_not_of("hello world") << endl;2
4294967295
9
4294967295
1
4294967295
11
4294967295
0
4294967295
10
4294967295
//4294967295        nops

stringの文字を削除
メンバー関数erase()
string s1("hello worlld");
s1.erase(5);
cout << s1;
cout << s1.length();
cout << s1.size();
//      5       
  :
hello55


stringの文字を置換
メンバー関数replace()
string s1("hello world");
s1.replace(2,3, “haha");
cout << s1;
// s1   2    3     “haha”
  :hehaha world
string s1("hello world");
s1.replace(2,3, "haha", 1,2);
cout << s1;
//  s1   2    3     “haha”    1   2   
  :heah world

stringに文字を挿入する
メンバー関数insert()
string s1("hello world");
string s2(“show insert");
s1.insert(5,s2); //  s2  s1  5   
cout << s1 << endl;
s1.insert(2,s2,5,3);
// s2   5   3     s1  2   
cout << s1 << endl;
  :
helloshow insert world
heinslloshow insert world

C言語char*文字列に変換
メンバー関数c_str()
string s1("hello world");
printf("%s
"
, s1.c_str()); // s1.c_str() const char * , ‘\0’ 。 : hello world

メンバー関数data()
string s1("hello world");
const char * p1=s1.data();
for(int i=0;i<s1.length();i++)
	printf("%c",*(p1+i));
//s1.data()     char *       , s1        p1  。
  :
hello world

文字列コピー
メンバー関数copy()
string s1("hello world");
int len = s1.length();
char * p2 = new char[len+1];
s1.copy(p2,5,0);
p2[5]=0;
cout << p2 << endl;
// s1.copy(p2,5,0)  s1   0           5                 p2。               。
  :
hello

文字列フロー処理
標準ストリームとファイルストリームの入出力に加えてstringからの入出力も可能である.istreamやosteramのように標準ストリームの入出力を行い、istringstreamやostringstreamで文字列上の入出力を行い、メモリ入出力とも呼ばれます.
#include 
#include 
#include 

文字列入力ストリームistringstream
string input("Input test 123 4.7 A");
istringstream inputString(input);
string string1, string2;
int i;
double d;
char c;
inputString >> string1 >> string2 >> i >> d >> c;
cout << string1 << endl << string2 << endl;
cout << i << endl << d << endl << c <<endl;
long L;
if(inputString >> L) cout << "long
"
;//inputString , else cout << "empty
"
; : Input test 123 4.7 A empty

文字列出力ストリームistringstream
ostringstream outputString;
int a = 10;
outputString << "This " << a << "ok" << endl;
cout << outputString.str();
  :
This 10ok