パート 7 STL-基礎
1. STL の最初の紹介
1.1 STLの誕生
1.2 STLの基本概念
1.3 STL の 6 つの主要コンポーネント
1.4 STL のコンテナ、アルゴリズム、イテレータ
1.5 コンテナアルゴリズム反復子の最初の紹介
1.5.1 ベクターストアの組み込みデータ型
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>//标准算法的头文件
//vector容器存放内置数据类型
void myPrint(int val) {
cout << val << endl;
}
void test01() {
//创建了一个vector容器,数组
vector<int> v;
//想容器中插入数据
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);
//通过迭代器访问容器中的数据‘
//vector<int>::iterator itBegin = v.begin();//起始迭代器 指向容器中第一个元素
//vector<int>::iterator itEnd = v.end();//结束迭代器 指向容器中最后一个元素的下一个地址
第一种遍历方式
//while (itBegin != itEnd) {
// cout << *itBegin << endl;
// itBegin++;
//}
//第二种遍历方式
/*for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << endl;
}*/
//第三种遍历方式 利用stl提供遍历算法
for_each(v.begin(), v.end(), myPrint);//回调函数
}
int main() {
test01();
}
1.5.2 ベクターはカスタム データ型を保存します
学習目標: カスタム データ型をベクターに保存して出力する
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>//标准算法的头文件
#include <string>
class Person {
public:
Person(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
void test01() {
vector<Person>v;
Person p1("aaa", 11);
Person p2("bbb", 22);
Person p3("ccc", 33);
Person p4("ddd", 44);
Person p5("eee", 55);
//添加数据
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
//遍历容器中的数据
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {
cout << "姓名: " << (*it).m_Name << "年龄: " << (*it).m_Age << endl;
}
}
void test02() {
vector<Person*>v;
Person p1("aaa", 11);
Person p2("bbb", 22);
Person p3("ccc", 33);
Person p4("ddd", 44);
Person p5("eee", 55);
//添加数据
v.push_back(&p1);
v.push_back(&p2);
v.push_back(&p3);
v.push_back(&p4);
v.push_back(&p5);
//遍历容器中的数据
for (vector<Person *>::iterator it = v.begin(); it != v.end(); it++) {
Person * p = (*it)
cout << "姓名: " << p->m_Name << "年龄: " << p->m_Age << endl;
}
}
int main() {
//test01();
test02();
}
1.5.3 ベクターコンテナのネストされたコンテナ
学習目標: コンテナ内でコンテナをネストし、すべてのデータを走査して出力します。
#include<iostream>
using namespace std;
#include <vector>
void test01() {
vector <vector<int>>v;
//创建小容器
vector<int>v1;
vector<int>v2;
vector<int>v3;
vector<int>v4;
vector<int>v5;
//向小容器添加数据
for(int i = 0; i < 4; i++) {
v1.push_back(i + 1);
v2.push_back(i + 2);
v3.push_back(i + 3);
v4.push_back(i + 4);
}
//将小容器插入到大容器中
v.push_back(v1);
v.push_back(v2);
v.push_back(v3);
v.push_back(v4);
//通过大容器,把所有数据遍历一遍
for (vector <vector <int>> ::iterator it = v.begin(); it != v.end(); it++) {
// (*it)------容器 vector<int>
for (vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++)
{
cout << *vit << " ";
}
cout << endl;
}
}
int main() {
test01();
}
2. STL共通コンテナ
2.1 文字列コンテナ
2.1.1文字列の基本概念
2.1.2文字列コンストラクター
#include<iostream>
using namespace std;
#include <string>
//string 的构造
void test01() {
string s1;//默认构造
const char * str = "hello world";
string s2(str);
cout << "s2 = " << s2 << endl;
string s3(s2);
cout << "S3 = " << s3 << endl;
string s4(10, s3);
cout << "s4 = " << s4 << endl;
}
int main() {
test01();
}
要約: 文字列のさまざまな構築方法は比較できませんが、それらを簡単に使用するにはどうすればよいですか?
2.1.3 文字列代入操作
#include<iostream>
using namespace std;
//string赋值
void test01() {
string str1;
str1 = "hello world";
cout << "str1 = " << str1 << endl;
string str2;
str2 = str1;
string str3;
str3 = "A";
string str4;
str4.assign("hell c++");
cout << "str4 = " << str4 << endl;
string str5;
str5.assign("hello c++", 5);
cout << "str5 = " << str5 << endl;
string str6;
str6.assign(str5);
string str7;
str7.assign(10,"w");
}
int main() {
test01();
}
概要: 文字列値を割り当てる方法はたくさんありますが、operator= の方がより実用的です。
2.1.4string 文字列の連結
2.1.5文字列の検索と置換
2.1.6 文字列の比較
2.1.7 文字列文字アクセス
修正も同様に行えます
2.1.8文字列の挿入と削除*
str の挿入および削除の添字は 0 から始まります。
2.1.9文字列の部分文字列
2.2vector容器
2.2.1ベクトルの基本概念
2.2.2ベクターコンストラクター
ベクターのさまざまな構築方法は比較するものではなく、柔軟に使用してください
2.2.3ベクターコピー操作
2.2.4 vetcor の容量とサイズ
2.2.5 vetcor の挿入と削除
2.2.6 ベクトルデータアクセス
2.2.7 ベクトル交換コンテナ
実際の使用量 メモリを縮小する
2.2.8 ベクトル予約スペース
2.3 デックコンテナ
2.3.1 deque コンテナの基本概念
2.3.2 両端キューコンストラクター
2.3.3 デキュー代入操作
2.3.4 デキューサイズの操作
2.3.5 deque の挿入と削除
2.3.6 データアクセスのデキュー
2.3.7 デックソート
要約: ソート アルゴリズムは非常に実用的で、使用するときにヘッダー ファイル アルゴリズムを組み込むだけです。
2.4スタックコンテナ
2.4.1 スタックの基本概念
2.4.2 共通スタックインターフェイス
2.5キューコンテナ
2.5.1 キューの基本概念
2.5.2 キュー共通インターフェース
2.6 リストコンテナ
2.6.1 リストの基本概念
2.6.2 リストコンストラクタ
2.6.3 リストの割り当てと交換
2.6.4 リストサイズの操作
2.6.5 リストの挿入と削除
2.6.6 リストデータアクセス
2.7.7 リストの反転とソート
2.7.8 ケースの分類
2.7セット容器
2.7.1 セットの基本概念
2.7.2 セットコンテナの構造
2.7.3 サイズの設定と交換
2.7.4 セットの挿入と削除
2.7.5 セットの検索と統計
2.7.6 セットとマルチセットの違い
要約:
重複データの挿入が許可されていない場合は、set を使用できます。
重複したデータを挿入する必要がある場合は、マルチセットを使用してください
2.7.7 ペアの作成
2.7.8 セットコンテナの仕分け
ストア組み込み関数
カスタム関数を保存する
タイプを定義する
組み込み関数を変更する
インスタンス化するときに使用されます
要約:
カスタム データ型の場合、set はデータを挿入する前に照合順序を指定する必要があります。
2.8 マップコンテナ
2.8.1 マップの基本概念
2.8.2 マップの構築と割り当て
概要: マップ内のすべての要素はペアで表示されます。データを挿入するときはペアを使用します。
2.8.3 マップのサイズとスワッピング
2.8.4 マップの挿入と削除
4 番目の方法の使用は推奨されません。キーを使用して値にアクセスできます。
2.8.5 地図の統計と検索
2.8.6 マップコンテナのソート
ファンクタ
2.9 ケースと従業員のグループ化
[外部リンク画像の転送に失敗しました。ソース サイトにはリーチ防止メカニズムがある可能性があります。画像を保存して直接アップロードすることをお勧めします (img-4JdS95jz-1667824623562) (C:/Users/62476/AppData/Roaming/Typora/) typora-user-images/image-20221107165840580.png)]
3 stl 関数オブジェクト
3.1 機能形成
3.1.1 関数オブジェクトの概念
3.1.2 関数オブジェクトの使用法
3.2 述語
3.2.1 述語の概念
単項述語
バイナリ述語
3.3 組み込み関数オブジェクト
3.3.1 裏切り者機能オブジェクトの意味
3.3.2 算術関手
3.3.3 関係関数子
3.3.4 論理関数