STL の概要と文字列クラスの簡単な使用法

標準テンプレート ライブラリ STL (標準テンプレート ライブラリ) は、次のようなテンプレート クラスと関数のセットです。

  • 情報を保存するためのコンテナ
  • コンテナに保存されている情報にアクセスするためのイテレータ
  • コンテナの内容を操作するためのアルゴリズム

容器

連続コンテナと連想コンテナの 2 つのカテゴリが含まれます。これに加えて、コンテナアダプターもあります

  • シーケンシャルコンテナ -------> std::vector / std::deque / std::list / std::forward_list
  • 关联容器 -------> std:: set / unowned_set / map / unowned_map / mutiset / unorder_multiset / multimap / unowned_multimap
  • コンテナアダプタ -------> std::stack/queue/priority_queue
    ここに画像の説明を挿入
    ここに画像の説明を挿入

ここに画像の説明を挿入

イテレータ

最も単純なイテレータはポインタです。STL イテレータはテンプレート クラス (いわば汎用ポインタ) です。

STL イテレータは、次の 2 つの基本カテゴリに分類されます。

  • 入力イテレータ: 入力イテレータを逆参照することにより、コレクション内にあるオブジェクトを参照します。最も厳密な入力イテレータにより、オブジェクトが読み取り専用でのみアクセスできることが保証されます。
  • 出力イテレータ: プログラマがコレクションに対して書き込み操作を実行できるようにします。最も厳密な出力反復子により、書き込み操作のみが実行できることが保証されます。

上記の 2 種類の基本的な反復子を継続的に改良すると、次のようになります。

  • 前方反復子: 単一リンクされたリストの入力と出力を許可します。
  • 双方向イテレータ: 前方イテレータの改良。デクリメント操作を実行して後方に移動でき、二重リンクリストに使用されます。
  • ランダム アクセス反復子: 双方向反復子を改良し、オフセットを加算または減算したり、2 つの反復子を減算して 2 つの要素間の相対距離を取得したりすることもできます。これは配列でよく使用されます。

STLアルゴリズム

次のアルゴリズムは std 名前空間のテンプレート関数であり、ヘッダー ファイル **<algorithm>** を含める必要があります。

std::find: コレクション内の値を検索します。

find_if: ユーザー指定の述語に基づいてコレクション内の値を検索します。

reverse: コレクション内の要素の順序を逆にします。

Remove_if: ユーザー定義の述語に基づいてコレクションから要素を削除します

変換: ユーザー定義の変換関数を使用してコンテナ内の要素を変換します。


STL文字列クラス

std::string と std::wstring は、実際には同じテンプレート クラス std::basic_string<T> を具体化したものです。

最も一般的に使用される文字列関数には、コピー、連結、文字と部分文字列の検索、切り捨て、STL アルゴリズムによる文字列反転と大文字小文字変換の実装が含まれます。

string クラスを使用するには、ヘッダー ファイル **<string>** をインクルードする必要があります

インスタンス化してコピーする

#include <iostream>
#include <string>
using namespace std;

int main()
{
    
    
    const char* constCStyleString = "Hello string!";
    cout << "Const string: " << constCStyleString << endl;
    //方式1:使用C风格常量字符串初始化string对象(类似于:string str2 = constCStyleString;)
    string strFromConst (constCStyleString);
    cout << "strFromConst is: " << strFromConst << endl;
    //方式2:构造器方式(类似于:string str2 = "Hello String!";)
    string str2 ("Hello String!");
    cout << "string str2: " << str2 << endl;
    //复制:
    string strCopy (str2);
    cout << "strCopy: " << strCopy << endl;
    //构造C风格字符串的前三个字符(传入的必须是const char*, 如果传入str2,就是其他重载函数)
    string strPartialCopy (constCStyleString, 3);
    cout << "strPartialCopy: " << strPartialCopy << endl;
    //方式3:=复制构造string对象
    string equalTestString = "abc";
    cout << "equalTestString: " << equalTestString << endl;
    //包含指定字符数量的string对象(10个a字符)
    string strRepeatChar (10, 'a');
    cout << "strReapeatChar: " << strRepeatChar << endl;
    system("pause");
    return 0;
}

文字列の文字内容にアクセスする

#include <iostream>

int main()
{
    
    
    using namespace std;

    string strSTLString ("Hello String");

    //方式1:通过string实现的下标运算符([])以类似数组的语法遍历string对象中的字符
    cout << "Displaying the elements in the string using array-syntax: " << endl;
    for (size_t nCharCounter = 0; 
        nCharCounter < strSTLString.length()
        ; ++nCharCounter)
    {
    
    
        cout << "Character [" << nCharCounter << "] is: ";
        cout << strSTLString [nCharCounter] << endl;
    }
    cout << endl;

    //方式2:迭代器
    cout << "Displaying the contents of the string using iterators: " << endl;
    int charOffset = 0;
    string::const_iterator iCharacterLocator;
    for ( iCharacterLocator = strSTLString.begin()
        ; iCharacterLocator != strSTLString.end()
        ; ++iCharacterLocator)
    {
    
    
        cout << "Character [" << charOffset++ << "] is: " ;
        cout << *iCharacterLocator << endl;
    }
    cout << endl;

    cout << "The char* represemtation of the string is: ";
    cout << strSTLString.c_str() << endl;
    return 0;
}

方法 1: オフセット (nCharCounter) が文字列オブジェクト ( length() ) の長さを超えていないことを確認します。

方法 2: イテレータへの逆参照アクセス

文字列を連結する

#include <iostream>
#include <string>
using namespace std;

int main()
{
    
    
    string str1 = "Hello";
    string str2 = "world";

    //方式1:运算符“+=”
    str1 += str2;
    cout << "after +=,str1: " << str1 << endl;

    //方式2:append方法
    string str3 = "Fun is not need to use pointers!";
    str1.append(str3);
    cout << "after append, str1: " << str1 << endl;

    //append方法的重载:接收一个C风格字符串
    const char* constCStyleString = "You however still can!";
    str1.append(constCStyleString);
    cout << str1 << endl;
    
    system("pause");
    return 0;
}

文字または部分文字列を検索する

#include <iostream>
#include <string>
using namespace std;

int main()
{
    
    
    string strSample ("Good day String! Today is beautiful!");
    cout << "The sample string is: " << endl;
    cout << strSample << endl << endl;

    //string类中的find方法,以下表示从索引20开始查找子字符串“day”
    size_t charPos = strSample.find("day", 20);
    if(charPos != string::npos) //npos是一个静态常量,如果相等代表访问完string对象所有元素仍未找到“day”
    {
    
    
        cout << "First instance of \"day\" was found at position " << charPos;
    }
    else
    {
    
    
        cout << "Substring not found." << endl;
    }
    cout << endl << endl;

    cout << "Locating all instances of \"day\" " << endl;
    size_t SubstringPos = strSample.find("day", 0);
    while (SubstringPos != string::npos)
    {
    
    
        cout << "\"day\" was found at position " << SubstringPos << endl;

        size_t nSearchPosition = SubstringPos + 1;

        SubstringPos = strSample.find("day", nSearchPosition);
    }
    cout << endl;

    //查找字符
    cout << "Locating all instances of character 'a'" << endl;
    const char charToSearch = 'a';
    charPos = strSample.find(charToSearch, 0);

    while (charPos != string::npos)
    {
    
    
        /* code */
        cout << "'" << charToSearch << "' is found" ;
        cout << " at position: " << charPos << endl;

        size_t charSearchPos = charPos + 1;
        charPos = strSample.find(charToSearch, charSearchPos);
    }

    system("pause");
    return 0;
}

切り詰める

/*
string类提供erase函数,可用于:
    • 在给定偏移位置和字符数时删除指定数目的字符。
    string sampleStr ("Hello String! Wake up to a beautiful day!"); 
    sampleStr.erase (13, 28); // 13是起始位置,28是要删除的字符串长度。结果为:Hello String! 
    
    • 在给定指向字符的迭代器时删除该字符。
    sampleStr.erase (iCharS); // iterator points to a specific character 
    
    • 在给定由两个迭代器指定的范围时删除该范围内的字符。
    sampleStr.erase (sampleStr.begin (), sampleStr.end ()); 
*/
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main()
{
    
    
    string str ("Hello String! Wake up to a beautiful day!");
    cout << "原始字符串为:" << str << endl;

    cout << "删除指定数目的字符(13-28):" << endl;
    str.erase(13, 28);
    cout << str << endl;

    //
    cout << "删除指定字符(S):" << endl;
    auto iCharS = find ( str.begin (), str.end (), 'S' );
    if(iCharS != str.end())
    {
    
    
        cout << "删除成功,str:" ;
        str.erase(iCharS);
        cout << str << endl;
    }
    cout << str << endl;

    //左闭右开
    cout << "另一种截断方式:" << endl;
    str.erase(str.begin(), str.end());
    if (str.length() == 0)
    {
    
    
        cout << "这个字符串为空" << endl;
    }

    system("pause");
    return 0;
}

逆行

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main()
{
    
    
    string strSample("Hello String! We will reverse you!");
    cout << "Orignal string is: ";
    cout << strSample << endl;

    //使用泛型算法std::reverse
    cout << "reverse:";
    reverse(strSample.begin(), strSample.end());
    cout << strSample << endl;
    system("pause");
    return 0;
}

大文字と小文字の変換

#include <string> 
#include <iostream> 
#include <algorithm> 

int main () 
{
    
     
    using namespace std; 

    cout << "Please enter a string for case-convertion:" << endl; 
    cout << "> "; 

    string inStr; 
    getline (cin, inStr); 
    cout << endl; 

    //使用transform
    transform(inStr.begin(), inStr.end(), inStr.begin(), ::toupper); 
    cout << "The string converted to upper case is: " << endl; 
    cout << inStr << endl << endl; 

    transform(inStr.begin(), inStr.end(), inStr.begin(), ::tolower); 
    cout << "The string converted to lower case is: " << endl; 
    cout << inStr << endl << endl; 

    return 0; 
}

おすすめ

転載: blog.csdn.net/weixin_43869409/article/details/129541203