c++primerノート----文字列、ベクトル、配列

14083 ワード

  • using声明
  • #include 
    using std::cin;
    using std::cout; using std::endl;
  • 定義および初期化string
  • string s1;  //     ,s1       
    string s2(s1);
    string s2 = s1;
    string s3 = "value";
    string s3("value");
    string s4(n, 'c');
  • 読み書きstring
  • string s;
    cin >> s;  //         (  、   、    ),           
    
    getline(cin. line);  //      ,       。        
    
    line.empty();
    
    auto len = line.size();  //len    `string::size_type`。 unsigned
    //             size()  ,      int 。
    //      int-->unsigned int
  • stirng加算:文字列のフォント値はstringオブジェクト
  • ではありません.
    string s4 =s1 +", ";
    string s5 = "hello " + ", ";  //  : +             string  
  • 各文字を処理または変更する forサイクル
  • #include 
    using std::string;
    
    string s("hello");
    decltype(s.size()) punct_cnt = 0;   //    
    for(auto c : s)         //      auto &c : s 。c     char
        if(ispunct(c))      //
            ++punct_cnt;
  • 下付き演算子[]:受信パラメータタイプはstring::size_typeです.シンボルタイプなし.c++標準ライブラリは、下標の合法性
  • の検出を要求しない.
  • vevtor:クラステンプレート、コンパイラ生成クラスのために作成された説明
  • #include 
    using std::vector
    
    vector<int> ivec;   //  <>      ,        
  • 定義および初期化vector
  • vector v1;  // vector
    vector v2(v1);
    vector v2 = v1;
    vector v3(n, val);
    vector v4(n);  //n   ,     
    vector v5{a, b, c};
    vector v5 = {a, b, c};
    
    vector<string> v6("hi");   //error : {}
    vector<string> v7{10};    //    v7(10).         
    vector<string> v8{10, "hi"};  //    v8(10, "hi")
  • インスタンス化:コンパイラがテンプレートに基づいてクラスまたは関数を作成するプロセス
  • vector操作
  • vector<int> v1;
    v1.push_back(1);  //    。        ,       for  
  • 反復器
  • auto b = v.begin(), e = v.end();  //    ,        
    for(auto it = s.begin(); it != s.end(); ++it)  
    //!=              ,  ++it  it++   
  • 反復器タイプ
  • vector<int>::iterator it;
    string::iterator it2;
    
    vector<int>::const_iterator it3;
    string::const_iterator it4;
    
    vector<int> v;
    const vector<int> cv;
    auto it1 = v.begin();    //vector::iterator ,          
    auto it2 = cv.begin();   //vector::const_iterator
    
    auto it3 = v.cbegin();  //vector::const_iterator
  • 反復アクセスメンバー
  • it->mem;
    (*it).mem;
  • 反復器を使用する循環体は、反復器が属する容器に元素
  • を添加しないでください.
  • constexprと定数式:値は変更されず、コンパイルプロセスで計算結果の式が得られます.字面値は定数式に属し、定数式で初期化されたconstオブジェクトも定数式
  • である.
    const int max_files = 20;
    const int limit = max_files + 1;
    int staff_size = 27;  //       
    const int sz = get_size(); //      constexpr  
    
    constexpr int mf =20;
  • 配列初期化
  • int arr[10];  //[]       
    
    int a2[] = {0, 1, 2};  //   3
    int a3[5] = {0, 1, 2}; //  2       0
  • 文字配列特殊性
  • char a1[] = {'c', 'd'}; //   2
    char a2[] = {'c', 'd', '\0';} //        
    char a3[] = "c++"; //   4,       
    const char a4[6] = "daniel"; //error:    7,         
  • 複雑な配列宣言
  • int *ptrs[10]; //ptrs   10        。              
    int &refs[10]; //error :         。       
    int (*parray)[10] = &arr; //parray   ,    10      
    int (&arrref)[10] = arr;  //arrref   ,    10      
    
    int *(&arry)[10] = ptrs; //arry   ,      10 int     
  • ポインタと配列:配列名とポインタはほぼ同じで、++,>,*,-などの演算が可能です.異なる点は、num++が不正であり、num++は配列の先頭を指す定数であり、左値としてはならない.
  • string nums[] = {"1", "2", "3"};
    string *p = &nums[0];  //[]           
  • Cスタイル文字列:文字列の表現と使用のために形成された約束俗称の書き方である.この習慣で書かれた文字列は、文字配列に格納され、空の文字(null)で終了します.
  • stringオブジェクトをCスタイル文字列
  • に変換
    char *str = s.c_str(); //      。
    //             ,        
  • 配列を使用してvectorオブジェクト
  • を初期化する.
    int int_arr[] = {0, 1, 2, 3, 4, 5};
    vector<int> ivec(begin(int_arr, end(int_arr))); //,     int* 
  • 多次元配列:配列の配列
  • int ia[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};//3   ,     4    
    int ia[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
    int ia[3][4] = {{1}, {2}, {3}};
    
    int (&row)[4] = ia[1];  // row   ia    4     
  • 範囲for
  • size_t cnt = 0;  //  []     size_t
    for(auto &row:ia)   //    auto row:ia。     int[4]     ,
                        //        。   &,      
        for(auto &col:row){   //      ,    &
            col = cnt;
        }
  • ポインタと多次元配列:配列名を使用すると、配列の先頭要素を指すポインタ
  • に自動的に変換されます.
    int ia[3][4];
    int (*p)[4] = ia;  //  -->  
    p = &ia[2];
    int (&row)[4] = ia[1];  // row   ia    4     
    
    for(auto p=ia; p!=ia+3; ++p)  //  -->  , p   ,    4      
        for(auto q=*p; q!=*p+4; ++q) //*p   ,*P    ,  -->  
    
    for(auto p=begin(ia); p!=end(ia);++p)
        for(auto q=begin(*p); q!=end(*p); ++q)
  • タイプ別名
  • using int_array = int[4];
    typedef int int_array[4];