C++11简单练习

#include <stdio.h>
#include <string.h>
#include <map>
#include <vector>
#include <array>
#include <iostream>
#include <memory>

using namespace std;

std::map<int, int> DataMap = {{1, 2}, {3, 4}};

int main(int argv, char* argc[])
{
    char szBuffer[3] = {0};
    strncpy(szBuffer, "ab", 2);
    char szTmp[3] = {0};
    szTmp[0] = szBuffer[0] >> 2;

    auto a = 10;
    auto b = "abc";
    auto c = "def";
    //auto d = b + c;
    auto e = 20;
    auto f = a + e;
    printf("f:%d, d:%s\n", f, b);


    //istd::map<int, int> DataMap = {{1, 2}, {3, 4}};
    //printf("map:%d, %d\n)
    for (auto d : DataMap)
    {
        cout << d.first << endl;
        cout << d.second << endl;
    }

    auto func = [&b](){
        b = "aabc";
        //cout << b <<endl;
    };
    func();
    cout << b <<endl;

    vector<int> DataVec;
    DataVec.push_back(1);
    DataVec.push_back(2);
    DataVec.push_back(3);

    for (auto &tmp : DataVec)
    {
        tmp = 1;
        //cout << tmp << endl;
    }

    for (auto tmp : DataVec)
    {
        cout << tmp << endl;
    }

    //array save stack
    array<string, 4> arr = {"v", "b", "n", "m"};
    for (auto tmp : arr)
    {
        cout << tmp << endl;
    }

    auto ptr = make_shared<int>(10);
    cout << ptr.use_count() << endl;

    {
        shared_ptr<int> ptr1 = ptr;
        cout << ptr1.use_count() << endl;
        cout << ptr.use_count() << endl;
    }
    cout << ptr.use_count() << endl;

    unique_ptr<int> ptr2(new int(10));
    //auto ptr3 = ptr2;//error

    weak_ptr<int> ptr3 = ptr;
    //auto ptr4 = ptr3;
    cout << ptr3.use_count() << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值