C++ STL の一般的なメソッドの概要

アルゴリズム

C++ STL (標準テンプレート ライブラリ) は、ヘッダー ファイルにカプセル化された多くのアルゴリズム メソッドを提供します。これらのアルゴリズムは、ベクター、デック、リスト、セット、マップなどのさまざまなコンテナーに適用できます。

一般的に使用されるいくつかのアルゴリズム手法とその例を次に示します。

1. find(): 指定された要素がコンテナ内に存在するかどうかを検索します。

例:

vector<int> vec = {
    
    1, 2, 3, 4, 5};
auto it = find(vec.begin(), vec.end(), 3);
if (it != vec.end()) {
    
    
    cout << "Found element: " << *it << endl;
} else {
    
    
    cout << "Element not found" << endl;
}

2. sort(): コンテナ内の要素を並べ替えます。

例:

vector<int> vec = {
    
    3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
sort(vec.begin(), vec.end());
for (auto i : vec) {
    
    
    cout << i << " ";
}

3. reverse(): コンテナ内の要素を反転します。

例:

vector<int> vec = {
    
    1, 2, 3, 4, 5};
reverse(vec.begin(), vec.end());
for (auto i : vec) {
    
    
    cout << i << " ";
}

4.accumulate(): コンテナ内の要素の合計を計算します。

例:

vector<int> vec = {
    
    1, 2, 3, 4, 5};
int sum = accumulate(vec.begin(), vec.end(), 0);
cout << "Sum of elements: " << sum << endl;

5. count(): コンテナ内の指定された要素の数を計算します。

例:

vector<int> vec = {
    
    1, 2, 3, 4, 5, 3, 2, 1};
int cnt = count(vec.begin(), vec.end(), 3);
cout << "Number of 3s: " << cnt << endl;

6. max_element(): コンテナ内の最大の要素を返します。

例:

vector<int> vec = {
    
    1, 2, 3, 4, 5};
auto it = max_element(vec.begin(), vec.end());
cout << "Max element: " << *it << endl;

7. min_element(): コンテナ内の最小の要素を返します。

例:

vector<int> vec = {
    
    1, 2, 3, 4, 5};
auto it = min_element(vec.begin(), vec.end());
cout << "Min element: " << *it << endl;

8. find_if(): 指定された条件を満たすコンテナ内の要素を検索します。

例:

vector<int> vec = {
    
    1, 2, 3, 4, 5};
auto it = find_if(vec.begin(), vec.end(), [](int n) {
    
     return n % 2 == 0; });
if (it != vec.end()) {
    
    
    cout << "Found even element: " << *it << endl;
} else {
    
    
    cout << "Even element not found" << endl;
}

9.remove(): 指定された要素をコンテナから削除します。

例:

vector<int> vec = {
    
    1, 2, 3, 4, 5};
auto it = remove(vec.begin(), vec.end(), 3);
vec.erase(it, vec.end());
for (auto i : vec) {
    
    
    cout << i << " ";
}

10.transform(): コンテナ内の要素を変換します。

例:

vector<int> vec = {
    
    1, 2, 3, 4, 5};
vector<int> vec2;
transform(vec.begin(), vec.end(), back_inserter(vec2), [](int n) {
    
     return n * 2; });
for (auto i : vec2) {
    
    
    cout << i << " ";
}

容器

1.ベクトル

Vector は、次の一般的なメソッドを提供する動的配列コンテナです。

- Push_back() 関数: 配列の末尾に要素を挿入します。

vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
// v中包含3个元素,分别是1, 2, 3

- Pop_back() 関数: 配列の末尾の要素をポップします。

vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.pop_back();
// v中包含2个元素,分别是1, 2

- insert() 関数: 要素を挿入します。

vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
auto it = v.begin();
it++; // it指向第二个元素
v.insert(it, 4);
// v中包含4个元素,分别是1, 4, 2, 3

- Erase() 関数: 要素の削除

vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
auto it = v.begin();
it++; // it指向第二个元素
v.erase(it);
// v中包含2个元素,分别是1, 3

- empty() 関数: ベクトルが空かどうかを判断します。

vector<int> v;
bool isEmpty = v.empty(); // isEmpty的值为true

-size()関数: 配列のサイズを変更します。

vector<int> v;
v.resize(3);
// v中包含3个元素,都是0
v.resize(5, 1);
// v中包含5个元素,前3个是0,后2个是1

2. したがって

Deque は、次の一般的なメソッドを提供する両端キュー コンテナです。

- Push_back() 関数: キューの最後に要素を挿入します。

deque<int> d;
d.push_back(1);
d.push_back(2);
d.push_back(3);
// d中包含3个元素,分别是1, 2, 3

- Push_front() 関数: キューの先頭に要素を挿入します。

deque<int> d;
d.push_front(1);
d.push_front(2);
d.push_front(3);
// d中包含3个元素,分别是3, 2, 1

- Pop_back() 関数: キューの最後にある要素をポップします。

deque<int> d;
d.push_back(1);
d.push_back(2);
d.push_back(3);
d.pop_back();
// d中包含2个元素,分别是1, 2

- Pop_front() 関数: キューの先頭要素をポップします。

deque<int> d;
d.push_back(1);
d.push_back(2);
d.push_back(3);
d.pop_front();
// d中包含2个元素,分别是2, 3

- insert() 関数: 要素を挿入します。

deque<int> d;
d.push_back(1);
d.push_back(2);
d.push_back(3);
auto it = d.begin();
it++; // it指向第二个元素
d.insert(it, 4);
// d中包含4个元素,分别是1, 4, 2, 3

- Erase() 関数: 要素の削除

deque<int> d;
d.push_back(1);
d.push_back(2);
d.push_back(3);
auto it = d.begin();
it++; // it指向第二个元素
d.erase(it);
// d中包含2个元素,分别是1, 3

- empty() 関数:両端キューが空かどうかを判断します

deque<int> d;
bool isEmpty = d.empty(); // isEmpty的值为true

-size()関数:キューのサイズを変更します。

deque<int> d;
d.resize(3);
// d中包含3个元素,都是0
d.resize(5, 1);
// d中包含5个元素,前3个是0,后2个是1

3.セット

set は、次の一般的なメソッドを提供するコレクション コンテナです。

- insert() 関数: 要素を挿入します。

set<int> s;
s.insert(1);
s.insert(2);
s.insert(3);
// s中包含3个元素,分别是1, 2, 3

- Erase() 関数: 要素の削除

set<int> s;
s.insert(1);
s.insert(2);
s.insert(3);
s.erase(2);
// s中包含2个元素,分别是1, 3

- find() 関数: 要素の検索

set<int> s;
s.insert(1);
s.insert(2);
s.insert(3);
auto it = s.find(2);
if (it != s.end()) {
    
    
    cout << "2 is in the set" << endl;
}

- empty() 関数: セットが空かどうかを判断します。

set<int> s;
bool isEmpty = s.empty(); // isEmpty的值为true

- カスタム照合順序

カスタムの並べ替えルールに従ってセット内の要素を並べ替える必要がある場合は、カスタム比較関数またはカスタム比較クラスを使用できます。たとえば、要素を昇順に並べる場合は、「未満」演算子を定義できます。

struct cmp {
    
    
    bool operator()(const int& a, const int& b) {
    
    
        return a < b; // 从小到大排序
    }
};
set<int, cmp> s;
s.insert(3);
s.insert(2);
s.insert(1);
// s中包含3个元素,分别是1, 2, 3,按照从小到大排序

4. リスト

list は、次の一般的なメソッドを提供する二重リンク リスト コンテナです。

- Push_back() 関数: リンクされたリストの最後に要素を挿入します。

list<int> l;
l.push_back(1);
l.push_back(2);
l.push_back(3);
// l中包含3个元素,分别是1, 2, 3

- Push_front() 関数: リンクされたリストの先頭に要素を挿入します。

list<int> l;
l.push_front(1);
l.push_front(2);
l.push_front(3);
// l中包含3个元素,分别是3, 2, 1

- Pop_back() 関数: リンクされたリストの末尾の要素をポップします。

list<int> l;
l.push_back(1);
l.push_back(2);
l.push_back(3);
l.pop_back();
// l中包含2个元素,分别是1, 2

- Pop_front()関数: リンクリストの先頭要素をポップアップします。

list<int> l;
l.push_back(1);
l.push_back(2);
l.push_back(3);
l.pop_front();
// l中包含2个元素,分别是2, 3

- Erase() 関数: 要素の削除

list<int> l;
l.push_back(1);
l.push_back(2);
l.push_back(3);
auto it = l.begin();
it++; // it指向第二个元素
l.erase(it);
// l中包含2个元素,分别是1, 3

- empty() 関数: リストが空かどうかを判断します。

list<int> l;
bool isEmpty = l.empty(); // isEmpty的值为true

- カスタム照合順序

list は、順序付けされたコンテナーではなくリンクされたリストであるため、カスタム並べ替えルールをサポートしません。カスタムの並べ替えルールに従って要素を並べ替える必要がある場合は、set、vector、deque などのコンテナーの使用を検討できます。
C++ STL のスタックとマップは、スタックとマップを実装するためによく使用される 2 つのコンテナです。以下に、例を示した方法の概要を示します。

5.スタック

stack は、次の一般的なメソッドを提供する後入れ先出し (LIFO) コンテナーです。

- Push() 関数: スタックの先頭に要素を挿入します。

stack<int> s;
s.push(1);
s.push(2);
s.push(3);
// 栈中元素为3,2,1

- Pop() 関数: スタックの最上位要素をポップします。

stack<int> s;
s.push(1);
s.push(2);
s.push(3);
s.pop();
// 栈中元素为2,1

- top() 関数: スタックの最上位要素にアクセスします。

stack<int> s;
s.push(1);
s.push(2);
s.push(3);
int x = s.top(); // x的值为3

- empty() 関数: スタックが空かどうかを判断します。

stack<int> s;
bool isEmpty = s.empty(); // isEmpty的值为true

6. 地図

map は、次の一般的なメソッドを提供するキーと値のペアのマッピング コンテナーです。

- insert() 関数: キーと値のペアを挿入します。

map<string, int> m;
m.insert(make_pair("Alice", 20));
m.insert(make_pair("Bob", 25));
m.insert(make_pair("Charlie", 30));
// m中包含3个键值对,分别是{"Alice": 20}, {"Bob": 25}, {"Charlie": 30}

- Erase() 関数: キーと値のペアを削除します。

map<string, int> m;
m.insert(make_pair("Alice", 20));
m.insert(make_pair("Bob", 25));
m.insert(make_pair("Charlie", 30));
m.erase("Bob");
// m中包含2个键值对,分别是{"Alice": 20}, {"Charlie": 30}

- find() 関数: キーに対応する値を検索します。

map<string, int> m;
m.insert(make_pair("Alice", 20));
m.insert(make_pair("Bob", 25));
m.insert(make_pair("Charlie", 30));
auto it = m.find("Bob");
if (it != m.end()) {
    
    
    int age = it->second; // age的值为25
}

- empty() 関数: マップが空かどうかを判断します。

map<string, int> m;
bool isEmpty = m.empty(); // isEmpty的值为true

- カスタム照合順序

カスタムの並べ替えルールに従ってマップ内のキーを並べ替える必要がある場合は、カスタム比較関数またはカスタム比較クラスを使用できます。たとえば、キーを最小値から最大値の順に並べる場合は、「未満」演算子を定義できます。

struct cmp {
    
    
    bool operator()(const string& a, const string& b) {
    
    
        return a < b; // 从小到大排序
    }
};
map<string, int, cmp> m;
m.insert(make_pair("Alice", 20));
m.insert(make_pair("Bob", 25));
m.insert(make_pair("Charlie", 30));
// m中包含3个键值对,分别是{"Alice": 20}, {"Bob": 25}, {"Charlie": 30},按照键从小到大排序

7. キュー

queue は、次の一般的なメソッドを提供する先入れ先出し (FIFO) コンテナです。

- Push() 関数: キューの最後に要素を挿入します。

queue<int> q;
q.push(1);
q.push(2);
q.push(3);
// 队列中元素为1,2,3

- Pop() 関数: キューの最初の要素をポップアップします。

queue<int> q;
q.push(1);
q.push(2);
q.push(3);
q.pop();
// 队列中元素为2,3

- Front() 関数: キューの最初の要素にアクセスします

queue<int> q;
q.push(1);
q.push(2);
q.push(3);
int x = q.front(); // x的值为1

- back() 関数: キューの末尾要素にアクセスします。

queue<int> q;
q.push(1);
q.push(2);
q.push(3);
int x = q.back(); // x的值为3

- empty() 関数: キューが空かどうかを判断します。

queue<int> q;
bool isEmpty = q.empty(); // isEmpty的值为true

8. 優先キュー

priority_queue は優先キューであり、各ポップアップ要素はキュー内で最も高い優先順位を持つ要素です。デフォルトでは、要素は最大から最小の順に優先順位が付けられます。priority_queue は、次の一般的なメソッドを提供します。

- Push() 関数: キューの最後に要素を挿入します。

priority_queue<int> pq;
pq.push(3);
pq.push(1);
pq.push(2);
// 队列中元素为3,2,1

- Pop() 関数: キューの最初の要素をポップアップします。

priority_queue<int> pq;
pq.push(3);
pq.push(1);
pq.push(2);
pq.pop();
// 队列中元素为2,1

- top() 関数: キューの最初の要素にアクセスします。

priority_queue<int> pq;
pq.push(3);
pq.push(1);
pq.push(2);
int x = pq.top(); // x的值为3

- empty() 関数: キューが空かどうかを判断します。

priority_queue<int> pq;
bool isEmpty = pq.empty(); // isEmpty的值为true

- カスタム照合順序

要素の照合順序をカスタマイズする必要がある場合は、演算子をオーバーロードすることで実現できます。たとえば、要素を昇順に並べる場合は、「未満」演算子を定義できます。

struct cmp {
    
    
    bool operator()(int a, int b) {
    
    
        return a > b; // 从小到大排序
    }
};
priority_queue<int, vector<int>, cmp> pq;
pq.push(3);
pq.push(1);
pq.push(2);
// 队列中元素为1,2,3

C++ STL の string は、可変長文字列を表すクラスです。string クラスは、文字列を操作および処理するための多くの関数メソッドを提供します。以下に、一般的に使用される文字列関数メソッドとその使用例をいくつか示します。

1. コンストラクター

- デフォルトのコンストラクター

string s1; // s1是一个空字符串

- パラメータ付きコンストラクタ

string s2("hello"); // s2是一个字符串,内容为"hello"
string s3(5, 'a'); // s3是一个字符串,内容为"aaaaa"

2. 代入操作

- 代入演算子

string s1 = "hello";
string s2 = s1; // s2的内容和s1相同
string s3;
s3 = s1; // s3的内容和s1相同

- assign() 関数

string s1 = "hello";
string s2;
s2.assign(s1); // s2的内容和s1相同
string s3;
s3.assign("world"); // s3的内容为"world"

3. アクセス操作

- []演算子

string s = "hello";
char c = s[0]; // c的值为'h'

- at()関数

string s = "hello";
char c = s.at(0); // c的值为'h'

- フロント()関数

string s = "hello";
char c = s.front(); // c的值为'h'

- back() 関数

string s = "hello";
char c = s.back(); // c的值为'o'

4.挿入操作

- Push_back() 関数

string s = "hello";
s.push_back('!'); // s的内容为"hello!"

- insert()関数

string s = "hello";
s.insert(0, "world"); // s的内容为"worldhello"

5. 削除操作

-pop_back()関数

string s = "hello!";
s.pop_back(); // s的内容为"hello"

- 消去()関数

string s = "hello world";
s.erase(6, 5); // s的内容为"hello"

6. 検索操作

- find()関数

string s = "hello world";
int pos = s.find("world"); // pos的值为6

- rfind()関数

string s = "hello world";
int pos = s.rfind("l"); // pos的值为9

7. 置換操作

- replace() 関数

string s = "hello world";
s.replace(6, 5, "you"); // s的内容为"hello you"

8. 部分文字列の操作

- substr() 関数

string s = "hello world";
string sub = s.substr(0, 5); // sub的内容为"hello"

9. その他の操作

- length() 関数

string s = "hello world";
int len = s.length(); // len的值为11

- size() 関数

string s = "hello world";
int len = s.size(); // len的值为11

- empty() 関数

string s = "hello world";
bool isEmpty = s.empty(); // isEmpty的值为false

おすすめ

転載: blog.csdn.net/gezongbo/article/details/130184105