c++ 11 14 17 20後的新特性總結二:無序容器、元組、正規表示式

哇塞~哇塞~發表於2020-12-17

1.容器

#include <forward_list>//單向連結串列
#include <list>
    //有序容器
#include <map>
#include <set>
    //無序容器
#include <unordered_set>
#include <unordered_map>

auto tupleFoo(int num)
{
    if(0 == num)
        return std::make_tuple(13, "Jon", "js");
    if(1 == num)
        return std::make_tuple(14, "Wendy", "sh");
    if(2 == num)
        return std::make_tuple(12, "Keiven", "hongkong");
}

void container_run()
{
    //線性容器
#include <array>
    const int arr_len = 5;//or constexpr int arr_len = 4;
    std::array<int, arr_len> arr = {11,2,23,4};
    if(arr.empty())
    {

    }
    else
    {
        cout<<arr.size()<<endl;
        std::sort(arr.begin(), arr.end(), [](int a, int b){
            return a > b;
        });

        for(auto i:arr)
            cout<<i<<endl;
    }
//unordered container
    std::unordered_map<int, string> mmp = {
        {1, "z"},
        {6, "y"},
        {3, "c"}
    };

    for(auto i:mmp)
        cout<<i.first<<endl;
}

2.元組

#include <tuple>
    /*元組
     *std::make_tuple:構造元組
     * std::get:獲取元組某個位置的值
     * std::tie:元組拆包
     * std::tuple_cat:元組合並
     * std::tuple_size:元組大小
    */
    auto student = tupleFoo(1);
    cout<<std::get<2>(student)<<endl;
    cout<<std::get<int>(student)<<endl;//std::get<string>(student):編譯錯誤(student元組中存在多個string型別)
    auto new_student = std::tuple_cat(student, tupleFoo(0));
    cout<<std::tuple_size<decltype(new_student)>::value<<endl;
    int age;string name,hometown;
    std::tie(age, name, hometown) = tupleFoo(2);
    cout<<age<<" "<<name<<" "<<hometown<<endl;
}

void inteligence_ptr()
{
#include <memory>
    /*std::shared_ptr強指標
     * use_count:引用計數
     * get:返回原始指標
     * reset:清除當前指標引用(use_count=0),其他智慧指標(use_count-1)
     * */
    auto pointer = std::make_shared<int>(10);
    cout<<"pointer reference & :"<<pointer.use_count()<<endl;
    auto pointer1 = pointer;
    cout<<pointer1.use_count()<<endl;
    auto p = pointer.get();
    cout<<pointer.use_count()<<endl;

    auto pp = pointer1;
    cout<<pp.use_count()<<"raw pointer:"<<pointer.use_count()<<endl;
    pointer.reset();//pointer.use_count()==0
    cout<<pointer1.use_count()<<endl;//其他pointerX:use_count-=1;
    cout<<pointer.use_count()<<endl;

    /*std::unique_ptr獨佔指標
     *獨佔智慧指標,禁止其他智慧指標共享同一個物件
    */
    auto unique_ptr = std::make_unique<int>(6);
    auto unique_ptr_1 = std::move(unique_ptr);//必須使用右值引用,才能轉移給其他ptr;
    //auto unique_ptr_2 = unique_ptr_1;禁止共享

    /*std::weak_ptr弱指標
     *弱引用不會引起引用計數增加
     * std::weak_ptr 沒有* 運算子和-> 運算子,所以不能夠對資源進行操作,它的唯一作用就是用於
        檢查std::shared_ptr 是否存在,其expired() 方法能在資源未被釋放時,會返回true,否則返回
        false。
    */
    std::weak_ptr<int> weakPtr = pointer1;
    cout<<"weak ptr :"<<pointer1.use_count()<<endl;
}

3.正規表示式

#include <regex>
void RegEx()
{
    //正規表示式-regular expression
    /*std::regex:正規表示式形式
     *std::regex_match(std::string, std::regex):判斷是否匹配正則形式,返回true/false
     *std::smatch:std::regex中的N個元素(std::regex_match(std::string, std::smatch, std::regex))
    */
    std::string str[] = {"abc.txt", "123.txt", "q-a.txt"};
    std::regex txt_regex("[a-z]+\.txt");
    for(const auto &i:str)
        cout<<i<<" is match :"<<(std::regex_match(i, txt_regex)?"true":"false")<<endl;

}

相關文章