search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

C++ STL 容器基本範例實驗測試[07_CPP_STL_Algorithm ->純C++ Vector排序、反轉、搜尋範例] – jashliao部落格

C++ STL 容器基本範例實驗測試[07_CPP_STL_Algorithm ->純C++ Vector排序、反轉、搜尋範例]


資料來源:https://www.cnblogs.com/skyfsm/p/6934246.html

http://larry850806.github.io/2016/06/06/STL1/
http://larry850806.github.io/2016/06/06/STL2/

GITHUB 完整資料: https://github.com/jash-git/Base_CPP_STL_Example [ 包含免安裝開發環境:CodeBlocks 12.11 方便自己測試使用 ]
線上執行:https://www.tutorialspoint.com/compile_cpp_online.php / http://codepad.org/

Algorithm
    sort:排序
    reverse:反轉
    find:搜尋
本範例額外紀錄

    ▲ 陣列 轉存 vector

#include 
#include 
#include /* malloc, free, rand */
#include /*strncpy .....*/
#include /*c++ string class*/
#include 
#include 
#include 
#include 
#include 
#include
using namespace std;
/*
Algorithm
    sort:排序
    reverse:反轉
    find:搜尋
本範例額外紀錄
    ▲ 陣列 轉存 vector
*/
void Pause()
{
    printf("Press Enter key to continue...");
    fgetc(stdin);
}

int main()
{
    /*
    //原始範例
    int arr[] = {3, 1, 4, 2, 5};
    vector vec(arr, arr+5);    // vec = [3, 1, 4, 2, 5]


    // 排序前三個 -> [1, 3, 4, 2, 5]
    sort(vec.begin(), vec.begin() + 3);


    // 全部排序 -> [1, 2, 3, 4, 5]
    sort(vec.begin(), vec.end());


    // 反轉 -> [5, 4, 3, 2, 1]
    reverse(vec.begin(), vec.end());


    // 找找看 3 有沒有在裡面
    // 找不到就會回傳 vec.end()
    vector::iterator it;
    it = find(vec.begin(), vec.end(), 3);

    if(it != vec.end()){
        cout << "3 find" << endl;
    } else {
        cout << "3 not find" << endl;
    }
    //*/

    string arr01[] = {"03-three", "01-one", "04-four", "02-two", "05-five"};
    vector vec01(arr01, arr01+5);//陣列 轉存 vector
    int length = vec01.size();
    for(int i=0 ; i [1, 3, 4, 2, 5]
    sort(vec01.begin(), vec01.begin() + 3);
    length = vec01.size();
    for(int i=0 ; i [1, 2, 3, 4, 5]
    sort(vec01.begin(), vec01.end());
    length = vec01.size();
    for(int i=0 ; i [5, 4, 3, 2, 1]
    reverse(vec01.begin(), vec01.end());
    length = vec01.size();
    for(int i=0 ; i::iterator it01;
    string strbuf = "03-three";
    it01 = find(vec01.begin(), vec01.end(), strbuf);

    if(it01 != vec01.end()){
        cout << "3 find" << endl;
    } else {
        cout << "3 not find" << endl;
    }
    Pause();
    return 0;
} 




熱門推薦

本文由 jashliaoeuwordpress 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦