C++学习笔记系列(一)

第一章

1、在C++中,可不显示地指定变量类型,使用关键字auto
例如:auto flag=true。这里将指定变量flag的类型交给了编译器,编译器会自动确定变量应为什么类型。
PS:auto时必须将变量初始化,否则会出现编译错误。
2、使用enum来定义变量只有一组特定的取值
例如:

enum RainbowColors
{ 
    violet=0,
    Indigo,
    Blue,
    Green,
    Yellow,
    Orange,
    Red
};

RainbowColors MyWorldsColor = blue ; #声明了常量MyWorldColor,这个常量只能取RainbowColors的值,声明枚举常量时,编译器把枚举值(voilet等)转化为整数,每个枚举值都比前一个大1.可以自己指定初始值,没有指定的话初始值为0.
3、为减少内存的占用,可以用std::vector来定义动态数组。需要包含头文件#include<vector>
例如:简要代码如下

#include<iostream>
#include<vector>
using namespace std;
int main()
{
   vector<int> DynArrNums(3)  #数组初始长度为3
   int AnotherNum=0;
   cin>>AnotherNum;
   DynArrNums.push_back(AnotherNum)  #使用这个函数将这个数字压入到矢量中
}

4、C++字符串的使用
需要使用头文件#include<string>才能使用string 变量名定义字符串变量。
例如:

#include<iostream>
#include<string>
using namespace std;
int main()
{
        string Greetings("Hello,std:string!");
        cout << Greetings << endl;
        cout << "enter a line o text:" << endl;
        string FirstLine;
        getline(cin, FirstLine);
        cout << "enter another" << endl;
        string SecLine;
        getline(cin, SecLine);
        cout << "concatenation:" << endl;
        string Contact = FirstLine + " " + SecLine;
        cout << Contact << endl;
        return 0;
   }

5、语句、运算符
要将一条放到两行中,可在第一行末尾添加反斜杠(\),也可将字符串字面量分成两个,如下例:

cout << "hello \
       world" << endl;
 或是:
 cout << "Hello"
       "world" <<endl;

使用后缀运算时,先将值赋给左值,再将右值递增或递减,左值都为执行前的旧值;使用前缀运算就相反,先将值递增或递减,再将结果赋给左值。

++变量名 一般优于 变量名++

#include<bitset>
cin>>InputNum;
bitset<8> InputBits (InputNum)  #转换为二进制
bitset<8> BitwiseNOT = (~InputNum) #按位运算符Not
bitset<8> BitwiseAND = () #AND运算
BitwiseOR = ()
BitwiseXOR=() #异或运算

6、控制流程序
字符初始化一般用:
char userselection= '\0;
死循环一般用来检测操作系统USB接口是否连接了设备,只要系统一直在运行,这种活动就不会停止。
一个函数可以包含多条return语句
7、函数重载
名称和返回类型相同,参数不同的函数称为重载函数。在应用程序中,如果使用不同的参数调用具有特定名称和返回类型的函数,重载函数将很有用。
按引用传递函数
即不是以返回值的方式而是以引用参数的方式提供给函数,如下所示:

#include<iostream>
#include<string>
using namespace std;
const double Pi = 3.1416;
void Area(double radius, double &result)
{
               result = Pi * radius *radius;
}
int main()
{
        cout << "enter radius:";
        double radius = 0;
        cin >> radius;
        double AreaFet = 0;
        Area(radius, AreaFet);
        cout << "the area is:" << AreaFet << endl;
        return 0;
}

8、内联函数
当定义一个函数时,执行函数的开销有可能非常高,所以使用关键字inline可以节省内存空间,将函数的内容直接放到它调用的地方,以提高代码的执行速度。但是应尽量少用关键字inline。

inline long DoubleNum(int InputNum)
{
    description;
}

9、lambda函数
lambda函数语法如下:
[optional parameters] (parameter list){statements;}
代码如下:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void DisplayNums(vector<int>&DynArray)
{
        for_each(DynArray.begin(), DynArray.end(), \
               [](int Element) {cout << Element << " "; });
        cout << endl;
}
int main()
{
        vector<int>MyNumbers;
        MyNumbers.push_back(501);
        MyNumbers.push_back(-1);
        MyNumbers.push_back(25);
        MyNumbers.push_back(-35);
        DisplayNums(MyNumbers);
        cout << "sorting them in descend orders" << endl;
        sort(MyNumbers.begin(), MyNumbers.end(), \
               [](int Num1, int Num2) {return (Num2 < Num1); });
        DisplayNums(MyNumbers);
        return 0;
}

当定义函数提供形式参数时,要将所有有默认参数值的参数放在列表末尾,要么给所有参数都指定默认值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小小谢先生

支持知识付费

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

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

打赏作者

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

抵扣说明:

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

余额充值