1. 程式人生 > >TfLite: 一些C++特性

TfLite: 一些C++特性

1.std::unique_ptr

#include <iostream>
 
void someFunction()
{
    Resource *ptr = new Resource;
    int x;
    std::cout << "Enter an integer: ";
    std::cin >> x;
    if (x == 0)
        throw 0; // the function returns early, and ptr won’t be deleted!
 
    // do stuff with ptr here
    delete ptr;
}

std::unique_ptr is the C++11 replacement for std::auto_ptr. It should be used to manage any dynamically 
allocated object that is not shared by multiple objects. That is, std::unique_ptr should completely own 
the object it manages, not share that ownership with other classes. std::unique_ptr lives in the <memory> header.

Let’s take a look at a simple smart pointer example:

#include <iostream>
#include <memory> // for std::unique_ptr
 
class Resource
{
public:
    Resource() { std::cout << "Resource acquired\n"; }
    ~Resource() { std::cout << "Resource destroyed\n"; }
};
 
int main()
{
    // allocate a Resource object and have it owned by std::unique_ptr
    std::unique_ptr<Resource> res(new Resource);
 
    return 0;
} // res goes out of scope here, and the allocated Resource is destroyed
Because the std::unique_ptr is allocated on the stack here, it’s guaranteed to eventually go out of scope, 
and when it does, it will delete the Resource it is managing.

You can, of course, use std::unique_ptr as a composition member of your class. This way, 
you don’t have to worry about ensuring your class destructor deletes the dynamic memory, 
as the std::unique_ptr will be automatically destroyed when the class object is destroyed. 
However, do note that if your class object is dynamically allocated, 
the object itself is at risk for not being properly deallocated, in which case even a smart pointer won’t help.

2.pair/ make_pair
pair是將2個數據組合成一個數據,當需要這樣的需求時就可以使用pair

include <iostream>
#include <utility>
#include <string>
usingnamespace std;

int main () {
    pair <string,double> product1 ("tomatoes",3.25);
    pair <string,double> product2;
    pair <string,double> product3;

    product2.first ="lightbulbs"; // type of first is string
    product2.second =0.99; // type of second is double

    product3 = make_pair ("shoes",20.0);

    cout <<"The price of "<< product1.first <<" is $"<< product1.second <<"\n";
    cout <<"The price of "<< product2.first <<" is $"<< product2.second <<"\n";
    cout <<"The price of "<< product3.first <<" is $"<< product3.second <<"\n";
    return0;
}

3.vector
向量 相當於一個數組
在記憶體中分配一塊連續的記憶體空間進行儲存。支援不指定vector大小的儲存。STL內部實現時,
首先分配一個非常大的記憶體空間預備進行儲存,即capacituy()函式返回的大小,當
超過此分配的空間時再整體重新放分配一塊記憶體儲存,這給人以vector可以不指定vector即一個連續記憶體的大小的感覺。
通常此預設的記憶體分配能完成大部分情況下的儲存。
不指定一塊記憶體大小的陣列的連續儲存,即可以像陣列一樣操作,但可以對此陣列
進行動態操作。通常體現在push_back() pop_back()

4.map
map映照容器的元素資料是一個鍵值和一個映照資料組成的,鍵值與映照資料之間具有一一映照的關係。
map映照容器的資料結構是採用紅黑樹來實現的,插入鍵值的元素不允許重複,
比較函式只對元素的鍵值進行比較,元素的各項資料可通過鍵值檢索出來。
使用map容器需要標頭檔案包含語句“#include<map>”

5.C++ Lambda表示式基本用法
https://lellansin.wordpress.com/2014/01/02/c-lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95/
C++ Lambda表示式基本用法
建立一個匿名函式並執行。Objective-C採用的是上尖號^,而C++ 11採用的是配對的方括號[]
捕獲選項

[] Capture nothing (or, a scorched earth strategy?)
[&] Capture any referenced variable by reference
[=] Capture any referenced variable by making a copy
[=, &foo] Capture any referenced variable by making a copy, but capture variable foo by reference
[bar] Capture bar by making a copy; don’t copy anything else
[this] Capture the this pointer of the enclosing class