天天看點

11.C++(标準模闆庫,詳細)

(建立于2018/1/2)

#ifndef _ITERATOR_DEBUG_LEVEL
#define _ITERATOR_DEBUG_LEVEL 0
#else
#undef _ITERATOR_DEBUG_LEVEL
#define _ITERATOR_DEBUG_LEVEL 0
#endif

#include <iostream>
#include <string>
#include <algorithm> //算法

using namespace std;

//STL standard template libary 标準模(mu)闆庫 C++一部分,編譯器自帶
//Android NDK支援
//java.lang java.util包中API,java的一部分

//string初始化

void main()
{
    //string由c字元串衍生過來的
    string s1 = "craig david";
    string s2("7 days");
    string s3 = s2;
    string s4(10,'a');

    cout << s4 << endl;

    system("pause");
}

//string周遊
void main()
{
    string s1 = "craig david";
    //           ^
    //1 數組方式
    for (int i = 0; i < s1.length(); i++)
    {
        cout << s1[i] << endl;
    }
    //2 疊代器指針
    for (string::iterator it = s1.begin(); it != s1.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
    //3 at函數(charAt)
    // 可能會抛出異常
    try
    {
        for (int i = 0; i < s1.length() + 3; i++)
        {
            cout << s1.at(i) << " ";
        }
    }
    catch (...)
    {
        cout << "異常" << endl;
    }


    system("pause");
}


//string字元串->c字元串轉換
void main()
{
    //string -> char*
    string s1 = "walking away";
    const char* c = s1.c_str();
    printf("%s\n",c);

    //
    string s2 = c;

    //string->char[]
    //從string中指派字元到char[]
    char arr[50] = {0};
    s1.copy(arr,4,0);
    
    cout << arr << endl;

    system("pause");
}


//字元串拼接
void main()
{
    string s1 = "alan";
    string s2 = "jackson";
    
    //1.
    string s3 = s1 + s2;

    string s4 = " pray";

    //2.
    s3.append(s4);

    cout << s3 << endl;

    system("pause");
}

//字元串查找替換
void main()
{
    string s1 = "apple google apple iphone";
    //從0開始查找"google"的位置
    int idx = s1.find("google", 0);
    cout << idx << endl;

    //統計apple出現的次數
    int idx_app = s1.find("apple",0);
    //npos大于任何有效下标的值
    int num = 0;
    //npos:  如果作為一個傳回值(return value)表示沒有找到比對項
    while (idx_app != string::npos)
    {
        num++;
        cout << "找到的索引:" << idx_app << endl;
        idx_app+=5;
        idx_app = s1.find("apple", idx_app);
    }

    cout << num << endl;
    system("pause");
}

void main()
{
    string s1 = "apple google apple iphone";
    //0-5(不包含5)替換為jobs
    s1.replace(0, 5, "jobs");
    cout << s1 << endl;

    //所有apple替換為jobs
    int idx = s1.find("apple", 0);
    while (idx != string::npos)
    {
        s1.replace(idx, 5, "jobs");
        idx += 5;
        idx = s1.find("apple", idx);
    }

    cout << s1 << endl;
    system("pause");
}

//删除(截取)和插入
void main()
{
    string s1 = "apple google apple iphone";
    //删除a,找到a所在的指針
    string::iterator it = find(s1.begin(),s1.end(),'g');
    //隻能删除一個字元
    s1.erase(it);
    
    //開頭末尾插入字元串
    s1.insert(0, "macos");
    s1.insert(s1.length(), " facebook");

    cout << s1 << endl;
    system("pause");
}

//java StringBuffer才可變
//String 不可變
//大小寫轉換
void main()
{
    string s1 = "JASON";
    //原始字元串的起始位址,原始字元串的結束位址, 目标字元串的起始位址, 函數名稱
    transform(s1.begin(), s1.end()-1,s1.begin(), tolower);
    cout << s1 << endl;


    transform(s1.begin(), s1.end() - 1, s1.begin(), toupper);
    cout << s1 << endl;

    system("pause");
}

//-----------------------------------------
//容器
//Vector
//初始化
#include <vector>
void printVector(vector<int> &v)
{
    //通過數組的方式周遊
    for (int i = 0; i < v.size(); i++)
    {
        cout << v[i] << endl;
    }
}
void main()
{
    //1.
    vector<int> v1;
    v1.push_back(20);
    v1.push_back(40);
    v1.push_back(15);
    v1.push_back(7);

    //2.
    vector<int> v2 = v1;

    //3.部分複制
    vector<int> v3(v1.begin(),v1.begin()+2);
    
    printVector(v3);
    

    system("pause");
}

//添加 删除
void main()
{
    //添加到結尾
    vector<int> v1;
    v1.push_back(20);
    v1.push_back(40);
    v1.push_back(15);
    v1.push_back(7);

    //通路頭部
    v1.front() = 11;
    //通路尾部
    v1.back() = 90;

    //删除結尾的元素
    //v1.pop_back();
    while (v1.size() > 0)
    {
        cout << "末尾的元素:" << v1.back() << endl;
        v1.pop_back();
    }

    printVector(v1);

    system("pause");
}

//數組的方式
void main()
{
    vector<int> v1;
    v1.push_back(20);
    v1.push_back(40);
    v1.push_back(15);
    v1.push_back(7);

    v1[2] = v1[2] +10;

    //容器等價于動态數組 
    vector<int> v2(10);
    for (int i = 0; i < v2.size(); i++)
    {
        v2[i] = i + 1;
    }

    printVector(v2);

    system("pause");
}

//疊代器周遊
//疊代器的種類(正向,反向疊代器)
void main()
{
    vector<int> v1;
    v1.push_back(20);
    v1.push_back(40);
    v1.push_back(15);
    v1.push_back(7);
    //正向
    for (vector<int>::iterator it = v1.begin(); it < v1.end(); it++)
    {
        cout << *it << endl;
    }
    cout << "-----------------" << endl;
    //反向疊代
    for (vector<int>::reverse_iterator it = v1.rbegin(); it < v1.rend(); it++)
    {
        cout << *it << endl;
    }

    system("pause");
}

//删除
void main()
{
    vector<int> v1(10);
    for (int i = 0; i < v1.size(); i++)
    {
        v1[i] = i + 1;
    }

    //删除指定位置
    vector<int>::iterator it = v1.begin();
    it += 3;
    v1.erase(it);

    //distance(v1.begin(), it);

    //删除區間
    v1.erase(v1.begin(), v1.begin() + 3);

    for (vector<int>::iterator it = v1.begin(); it < v1.end(); it++)
    {
        if (*it == 5)
        {       
            printf("%x\n", it);
            vector<int>::iterator tmp = v1.erase(it); //注意以後開發中編譯器版本問題
            printf("%x,%x\n",it,tmp);
        }
    }

    //插入
    v1.insert(v1.begin() + 2, 100);
    v1.insert(v1.end() - 1, 200);

    printVector(v1);

    system("pause");
}

//雙向隊列
#include <deque>
void printDeque(deque<int>& q)
{
    for (int i = 0; i < q.size(); i++)
    {
        cout << q[i] << endl;
    }
}

void main()
{
    deque<int> d1;
    //添加到尾部
    d1.push_back(2);
    d1.push_back(10);
    //添加到頭部
    d1.push_front(-90);
    d1.push_front(-30); 

    //printDeque(d1);

    //cout << d1.front() << endl;
    //cout << d1.back() << endl;

    //兩個方向彈出
    //d1.pop_back();
    //d1.pop_front();

    printDeque(d1);

    //查找第一個-90元素索引位置,無需周遊
    deque<int>::iterator it = find(d1.begin(),d1.end(),-90);
    if (it != d1.end())
    {
        int idx = distance(d1.begin(), it);
        cout << "索引位置為:" << idx << endl;
    }


    system("pause");
}

//資料結構 算法 分離
//Collections.shuff sort