C++语法糖全记录 截止5月31日

本文概述了C++中的预处理指令(#if)使用,介绍了`using`关键字替代typedef,重点讲解了`unique_ptr`的内存管理和不可复制类`INoCopy`。还涵盖了左值与右值的概念,以及main函数参数解析。讨论了C++11中的const关键字、extern的用法和列表初始化。
摘要由CSDN通过智能技术生成

在这里插入图片描述

1.预处理指令注释:

#if 1
cout << "Hello world\n";//输出指令
#if 0
cout << "I am a C++ programmer\n";
#endif
cout << "Test the /* \n";
#endif 

2.using在C++11中的使用:
代替了typedef:

using intvec = std::vector<int>;

3.unique_ptr的使用:

// 创建一个unique_ptr实例
unique_ptr<int> pInt(new int(5));
//独有的移动构造:
unique_ptr<int> pInt(new int(5));
unique_ptr<int> pInt2 = std::move(pInt);    // 转移所有权
//cout << *pInt << endl; // 出错,pInt为空
cout << *pInt2 << endl;
unique_ptr<int> pInt3(std::move(pInt2));

但可由函数返回:
// 示例:

unique_ptr<int> clone(int p)
{
    unique_ptr<int> pInt(new int(p));
    return pInt;// 返回unique_ptr
}
int main() {
    int p = 5;
    unique_ptr<int> ret = clone(p);
    cout << *ret << endl;
}
容器保存:
vector<unique_ptr<int>> vec;
unique_ptr<int> p(new int(5));
vec.push_back(std::move(p)); 

unique_ptr可以为空。可以通过reset()或者赋值nullptr释放管理对象。

4.继承下面这个类,实例无法被复制,引用:

class INoCopy
{
protected:
    INoCopy() = default;
    virtual ~INoCopy() = default;
    INoCopy(const INoCopy& other) = delete;
    INoCopy& operator=(const INoCopy& other) = delete;
    //&& 是右值引用
    INoCopy(INoCopy&& other) = delete;
    INoCopy& operator=(INoCopy&& other) = delete;
};

5.左值及右值:
lvalue 是“loactor value”的缩写,可意为存储在内存中、有明确存储地址(可寻址)的数据,而 rvalue 译为 “read value”,指的是那些可以提供数据值的数据(不一定可以寻址,例如存储于寄存器中的数据)。

int &c = 10;      // 错误
int && a = 10;       // 正确

6.main(int argc,char *argv[ ])
当输入prog para_1 para_2 有2个参数,则由操作系统传来的参数为:

argc=3,表示除了程序名外还有2个参数。

argv[0]指向输入的程序路径及名称。

argv[1]指向参数para_1字符串。

argv[2]指向参数para_2字符串。

   int main(int argc,char *argv[])
{
        printf("%s\n",argv[0]);//读取可执行程序(包括路径)

        /*读取参数*/
        int i = 1;
        while(i < argc)
        {
                printf("%s\n",argv[i]);
                i++;
        }
        //说明在dos和unix环境下,用""扩起来的表示其是一个字符串,代表着一个参数
        return 0;
}

7.const
const int 与int 的区别:int a;声明后 a可以赋予任何范围内常量。 如果用const int a 的话, a只能被赋予一个值,不能再赋予其他值。
限定返回值:特别时返回值为指针或引用时,不能用返回值来改变返回值所指或所引用的变量。

8.extern关键字:调用方式
(1)使用头文件调用,这时候,函数和变量必须在头文件中定义和声明。
(2)使用extern关键字调用,这时候函数和变量在.cpp或者.c文件中定义和声明。使用extern是在源文件的链接阶段寻找变量,不需要include

9.列表初始化

vector<int> int_vector{5, 4, 3, 2, 1};
list<int> int_list {5, 4, 3, 2, 1};
map<int, const char*> id2Name{{1,"Zhang"},{2, "Wang"},{3, "Li"}};

10.查看指定路径文件是否存在

for (int i = 0; i < MAX_DEPTH && !found; i++)
{
    const std::ifstream checkFile(filepath);
    //若存在文件,则返回true
    found = checkFile.is_open();
    if (found)
    {
        break;
    }
    //否则继续向根目录寻找
    filepath = "../" + filepath; // Try again in parent dir
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

颢师傅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值