C++標準ライブラリの概要
一、C++標準ライブラリの主要コンポーネント:
1、標準Cライブラリ
2、I/Oフロー技術(標準入出力装置に対して標準I/Oと称し、外部ディスク上のファイルに対する入出力をファイルI/Oと称し、メモリに指定された文字列記憶空間に対する入出力をシリアルI/Oと称する)
3、string類テンプレート
4、容器(vector、list、queue、stack、deque、map、set、bitset)
5、アルゴリズム
6、国際化に対するサポート
7、デジタル処理のサポート
8、診断サポート(3中のエラー報告方式:Cの断言、エラー番号、例外)
二、I/Oフロー技術
C++はデータの入出力を実現するために膨大なクラスライブラリを定義し、主にios、istream、ostream、iostream、ifstream、ofstream、fstream、istrstream、ostrstream、strstrstreamなどが含まれており、iosはベースクラスであり、残りは直接または間接派生クラスである.
iosは直接4つのクラスを派生する:入力ストリームiostream、出力ストリームostream、ファイルストリームベースクラスfstream、文字列ベースクラスstrstream.
C++のI/Oストリームライブラリがiostream、fstream、strstrstreamの3つのクラスライブラリファイルに含まれていることは、上記の説明で容易に理解できます.
C++は上記の3つのクラスライブラリだけでなく、cin、cout、cerr、clogの標準I/O操作におけるクラスオブジェクトも提供しています.
フォーマットコントロールオペレータ:
文字列判定関数:
1、empty()
2、length()
3、resize()長さを変える
5、c_str()
1、標準Cライブラリ
2、I/Oフロー技術(標準入出力装置に対して標準I/Oと称し、外部ディスク上のファイルに対する入出力をファイルI/Oと称し、メモリに指定された文字列記憶空間に対する入出力をシリアルI/Oと称する)
3、string類テンプレート
4、容器(vector、list、queue、stack、deque、map、set、bitset)
5、アルゴリズム
6、国際化に対するサポート
7、デジタル処理のサポート
8、診断サポート(3中のエラー報告方式:Cの断言、エラー番号、例外)
二、I/Oフロー技術
C++はデータの入出力を実現するために膨大なクラスライブラリを定義し、主にios、istream、ostream、iostream、ifstream、ofstream、fstream、istrstream、ostrstream、strstrstreamなどが含まれており、iosはベースクラスであり、残りは直接または間接派生クラスである.
iosは直接4つのクラスを派生する:入力ストリームiostream、出力ストリームostream、ファイルストリームベースクラスfstream、文字列ベースクラスstrstream.
C++のI/Oストリームライブラリがiostream、fstream、strstrstreamの3つのクラスライブラリファイルに含まれていることは、上記の説明で容易に理解できます.
C++は上記の3つのクラスライブラリだけでなく、cin、cout、cerr、clogの標準I/O操作におけるクラスオブジェクトも提供しています.
フォーマットコントロールオペレータ:
#include <iostream> // iomanip iostream,
#include <iomanip>
using namespace std;
int main(){
int x = 30, y = 300, z = 1024;
cout << x << ' ' << y << ' ' << z << endl;
//
cout << oct << x << ' ' << y << ' ' << z << endl;
//
cout << hex << x << ' ' << y << ' ' << z << endl;
//
cout << setiosflags(ios::showbase | ios::uppercase);
cout << x << ' ' << y << ' ' << z << endl;
cout << resetiosflags(ios::showbase | ios::uppercase);
cout << x << ' ' << y << ' ' << z << endl;
//
cout << dec << x << ' ' << y << ' ' << z << endl;
return 0;
}
カスタムフローオペレータ:#include <iostream>
using namespace std;
ostream &lin(ostream &myos){
return myos << "
-----------------";
}
int main(){
cout << lin << lin << lin << endl;
return 0;
}
I/Oオペレータリロード:#include <iostream>
#include <string.h>
using namespace std;
class Student{
friend ostream& operator << (ostream& ot, Student& popup);
char name[10];
unsigned int age;
unsigned long num;
public:
Student(char *na, unsigned int al, unsigned long number):age(al),
num(number){
strcpy(name, na);
}
};
ostream& operator << (ostream& ot, Student& popup){
ot << "Name:" << popup.name << endl << "Age:" << popup.age << endl
<< "Number:" << popup.num << endl << "---------------------" << endl;
return ot;
}
int main(){
Student a("Wang", 18, 1234), b("zhao", 19, 4312), c("liu", 20, 2341);
cout << a << b << c;
return 0;
}
書き込みファイル:#include <iostream>
#include <stdlib.h>
#include <fstream>
using namespace std;
int main(void){
// ,
ofstream f1("a:wr1.dat");
if(!f1){
cerr << "a:wr1.data file not open!" << endl;
}
for(int i=0; i<21 ; i++){
f1 << i << ' ';
}
f1.close();
return 0;
}
ファイルの内容を読む:#include <iostream>
#include <std.ib.h>
#include <fstream>
int main(){
// ,
ifstream f1("wrl.dat", ios::in | ios::nocreate);
// f1
if(!f1){
cerr << "wr1.data file not open!" << endl;
exit(1);
}
int x;
while(f1 >> x)
cout << x << ' ';
cout << endl;
f1.close();
return 0;
}
入出力フロー操作:#include <iostream>
#include <strstream>
using namespace std;
int main(){
char a[50];
char b[50];
istrstream sin(a); // sin, a
// sout, b
ostrstream sout(b, sizeof(b));
//
cin.getline(a, sizeof(a));
char ch = ' ';
int x;
while(ch !='@'){
// '@'
if(ch >= 48 && ch <= 57){
//
sin.putback(ch);
sin >> x;
//
sout << x << ' ';
}
//
sin.get(ch);
}
sout << '@' << ends;
//
cout << b;
cout << endl;
return 0;
}
構築文字列:#include <string>
#include <iostream>
using namespace std;
int main(){
string Mystring1(10, ' ');
string Mystring2 = "This is a string";
string Mystring3(Mystring2);
cout << "string1 is : " << Mystring1 << endl;
cout << "string2 is : " << Mystring2 << endl;
cout << "stirng3 is : " << Mystring3 << endl;
return 0;
}
文字列判定関数:
1、empty()
2、length()
3、resize()長さを変える
#include <iostream>
#include <string>
using namespace std;
int main(){
string TestString = "ll11223344565666";
cout << TestString << "
size: " << TestString.length() << endl;
TestString.resize(5);
cout << TestString << "
size: " << TestString.size() << endl;
TestString.resize(10);
cout << TestString << "
size: " << TestString.size() << endl;
TestString.resize(15, '6');
cout << TestString << "
size: " << TestString.size() << endl;
return 0;
}
4、append() 5、c_str()
#include <string>
#include <iostream>
using namespace std;
int main(){
string str1("012");
string str2("345");
cout << "str1 = " << str1.c_str() << endl;
cout << "str2 = " << str2 << endl;
// str2 str1
str1.append(str2);
cout << "str1 = " << str1 << endl;
//
const char* ch = str1.c_str();
for(int i=0; i<str1.length(); i++){
cout << ch[i] << ' ';
}
cout << endl;
str1.append(str2.c_str(), 2); // str1
str1.append(1, 'A');
str1.append(str2.begin(), str2.end());
cout << "str1 = " << str1 << endl;
cout << endl;
return 0;
}
文字と文字列の接続#include <string>
#include <iostream>
using namespace std;
int main(){
string result;
string S1 = "ABC";
string S2 = "DEF";
char CP1[] = "GHI";
char C = 'J';
cout << "S1 is " << S1 << endl;
cout << "S2 is " << S2 << endl;
cout << "C is " << C << endl;
result = CP1 + S1;
cout << "CP1 + S1 is " << result << endl;
result = S1 + C;
cout << "S1 + C is " << result << endl;
result = S1 + S2;
cout << "S1 + S2 is " << result << endl;
result = CP1 + C + S1;
cout << "CP1 + C + S1 is " << result << endl;
result = S1 + CP1 + C;
cout << "S1 + CP1 + C is " << result << endl;
return 0;
}
文字列反復:#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
const string hello("Hello, how are you?");
string s(hello.begin(), hello.end());
cout << "s : " << s << endl;
string::iterator pos;
for(pos = s.begin(); pos != s.end(); ++pos){
cout << *pos << ' ';
}
cout << endl;
//
reverse(s.begin(), s.end());
cout << "reverse: " << s << endl;
//
s.erase(unique(s.begin(), s.end()), s.end());
cout << "no duplictes: " << s << endl;
}