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
 

猜你喜欢

转载自blog.csdn.net/u011279649/article/details/83649963