c++11 学习及测试(shared_ptr, unique_ptr, allocator, function)

本文探讨了C++中智能指针(shared_ptr、unique_ptr)的构造与使用方法,并展示了如何利用allocator进行高效的内存管理。同时介绍了函数指针的高级用法function,通过实例演示了不同类型函数的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

智能指针shared_ptr、unique_ptr,构造及使用,make_sheard<typeName>,可以构造sheard_ptr,但是并没有make_unique。。

#include <iostream>
#include <vector>
#include <memory>
using namespace std;

class SharedStrings {
public:
    SharedStrings(): _strsPtr(make_shared< vector<string> >()) {
    }
    void push(const string& s) {
        _strsPtr->push_back(s);
    }
    void show() const {
        for_each(_strsPtr->begin(), _strsPtr->end(), [](const string& x){cout << x << endl;});
    }
private:
    shared_ptr< vector<string> > _strsPtr; //公用元素,省空间
};
int main(int argc, char* argv[]) {

    shared_ptr<string> p(new string("wangbing"));
    cout << *p << endl;

    SharedStrings a;
    {
        SharedStrings b;
        b.push("wangbing");
        b.push("WANGBING");
        a = b;
    }
    a.show();

    unique_ptr<int> uniq(new int(42));
    unique_ptr<int> un2(uniq.release());
    //un2.release(); 释放指针所有权,没有被接受,运行期报错
    uniq.reset(un2.release());
    cout << *uniq << endl;
    cout << "Hello world!" << endl;

    return 0;
}
内存管理器allocator,四步走:1,分空间(allocate) 2,构造(construct) 3,析构(destroy) 4,释放空间(deallocate)

#include <iostream>
#include <memory>
using namespace std;

int main(int argc, char* argv[]) {
    allocator<string> alloc;
    int n = 10;
    string* p = alloc.allocate(n);
    string* q = p;
    while(q != p+n) {
        alloc.construct(q++, 10, 'w');
        cout << *(q-1) << endl;
    }
    while(q != p) {
        alloc.destroy(--q);
    }
    alloc.deallocate(p, n);
    cout << "Hello world!" << endl;

    return 0;
}

函数指针的高端写法 function< reType<argType...> >

#include <iostream>
#include <functional>
#include <map>
#include <string>

using namespace std;

class add {
    int operator()(int a, int b) const {
        return a + b;
    }
};

int plu(int a, int b) {
    return a + b;
}

class A {
private:
    int _a = 0;
public:
    A(){}
    explicit A(int a): _a(a) {
    }
    explicit operator bool() const {
        return _a != 0;
    }
    explicit operator int() const {
        return _a;
    }
};

auto divide = [](int a, int b){return a / b;};
int main(int argc, char* argv[]) {
    map<string, function< int(int, int) > > opt{/*{"+", add()},*/
                                                {"+", plu},
                                                {"-", minus<int>()},
                                                {"/", divide},
                                                {"*", [](int a, int b){return a*b;}}};
    cout << opt["+"](8, 4) << endl;
    cout << opt["-"](8, 4) << endl;
    cout << opt["*"](8, 4) << endl;
    cout << opt["/"](8, 4) << endl;
    A a;
    if(a) {
        cout << "wangbing" << endl;
    } else {
        cout << "hehe" << endl;
    }
    int b = static_cast<int>(a) + 10;

    cout << b << endl;
    cout << "Hello world!" << endl;

    return 0;
}



[New Thread 0x7fffe62b0640 (LWP 93378)] [INFO] [1752816588.865113102] [imu_camera_sync]: IMU and Camera Sync Node started [INFO] [1752816588.865206898] [imu_camera_sync]: Waiting for synchronized IMU and Image data... Thread 1 "imu_camera_sync" received signal SIGSEGV, Segmentation fault. 0x00005555555a10c2 in ImuCameraSync::imu_callback(std::shared_ptr<sensor_msgs::msg::Imu_<std::allocator<void> > >) () (gdb) bt #0 0x00005555555a10c2 in ImuCameraSync::imu_callback(std::shared_ptr<sensor_msgs::msg::Imu_<std::allocator<void> > >) () #1 0x0000555555584107 in std::_Function_handler<void (std::shared_ptr<sensor_msgs::msg::Imu_<std::allocator<void> > >), std::_Bind<void (ImuCameraSync::*(ImuCameraSync*, std::_Placeholder<1>))(std::shared_ptr<sensor_msgs::msg::Imu_<std::allocator<void> > >)> >::_M_invoke(std::_Any_data const&, std::shared_ptr<sensor_msgs::msg::Imu_<std::allocator<void> > >&&) () #2 0x0000555555583043 in std::__detail::__variant::__gen_vtable_impl<std::__detail::__variant::_Multi_array<std::__detail::__variant::__deduce_visit_result<void> (*)(rclcpp::AnySubscriptionCallback<sensor_msgs::msg::Imu_<std::allocator<void> >, std::allocator<void> >::dispatch(std::shared_ptr<sensor_msgs::msg::Imu_<std::allocator<void> > >, rclcpp::MessageInfo const&)::{lambda(auto:1&&)#1}&&, std::variant<std::function<void (sensor_msgs::msg::Imu_<std::allocator<void> > const&)>, std::function<void (sensor_msgs::msg::Imu_<std::allocator<void> > const&, rclcpp::MessageInfo const&)>, std::function<void (rclcpp::SerializedMessage const&)>, std::function<void (rclcpp::SerializedMessage const&, rclcpp::MessageInfo const&)>, std::function<void (std::unique_ptr<sensor_msgs::msg::Imu_<std::allocator<void> >, std::default_delete<sensor_msgs::msg::Imu_<std::allocator<void> > > >)>, std::function<void (std::unique_ptr<sensor_msgs::msg::Imu_<std::allocator<void> >, std::default_delete<sensor_msgs::msg::Imu_<std::allocator<void> > > >, rclcpp::MessageInfo const&)>, std::function<void (std::unique_ptr<rclcpp::SerializedMessage, std::default_delete<rclcpp::SerializedMessage> >)>, std::function<void (std::unique_ptr<rclcpp::SerializedMessage, std::default_delete<rclcpp::Seri--Type <RET> for more, q to quit, c to continue without paging--q Quit 怎么看到具体的代码位置阿?
最新发布
07-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值