栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

21天学通C++

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

21天学通C++

Day01学习目录
  • 第2章 C++程序的组成部分
    • 2.1 程序的组成部分
      • 2.1.2 程序的主体——main()
  • 第3章 使用变量和常量
    • 3.1.6 变量命名约定
    • 3.2 常见C++变量类型
      • 3.2.6 指定宽度整型定义
    • 3.4 使用auto自动推断类型
    • 3.5 使用typedef替换变量类型
    • 3.6 什么是常量
      • 3.6.1 字面常量
      • 3.6.2 使用const将变量声明为常量
      • 3.6.3 使用constexpr定义常量表达式
      • 3.6.4 枚举
  • 第4章 管理数组和字符串
    • 4.1 一维数组
      • 4.1.1 数组的初始化
    • 4.2 多维数组
      • 4.2.1 数组的初始化
  • 第5章 使用表达式、语句和运算符
    • 5.3 运算符
      • 5.3.4 前缀运算符(i++/i--)和后缀运算符(++i/--i)
      • 5.3.9 高阶逻辑运算符
      • 5.3.10 按位运算符NOT(~)、AND(&)、OR(|)和XOR(^)
      • 5.3.11 按位左移运算符(<<)和右移运算符(>>)
  • 第6章 三大条件语句
      • 6.1.4 使用 switch-case 进行条件处理
      • 6.2.5 使用基于范围的for循环

第2章 C++程序的组成部分 2.1 程序的组成部分 2.1.2 程序的主体——main()

在很多C++应用程序中,都使用了类似下面的main()函数变种:

int main (int argc, char* argv[])

括号中的内容是提供给程序的参数。该程序可能允许用户执行时提供命令行参数,如:

program.exe /DoSomethingSpecific

其中,/DoSomethingSpecific 是操作系统传递给程序的参数。

第3章 使用变量和常量 3.1.6 变量命名约定

函数命名:每个单词的首字母都大写;如:MultiplyNumbers();
变量命名:第一个单词的首字母小写;如:firstNumber、secondNumber;

3.2 常见C++变量类型


float范围:-3.4E38 — +3.4E38
double范围:-1.79E+308 ~ +1.79E+308

3.2.6 指定宽度整型定义

C++11引入了固定宽度的整型,比如:uint8_t、int8_t、uint16_t、int16_t、uint32_t、int32_t、uint64_t和int64_t,要使用这些类型,必须包含头文件cstdint。

#include 
#include 
int main() {
	uint8_t a = 0;
	return 0;
}
3.4 使用auto自动推断类型

使用auto时必须对变量进行初始化,因为编译器需要根据初始值来确定变量的类型。

auto largeNumber = 2500000000000;

编译器将自动将largeNumber设置为long long型。

3.5 使用typedef替换变量类型
typedef unsigned_int STRICTLY_POSITIVE_INTEGER;
STRICTLY_POSITIVE_INTEGER numEggsInBasket = 4532;
3.6 什么是常量

常量包括:
1.字面常量
2.使用关键字const声明的常量
3.使用关键字constexpr声明的常量表达式
4.使用关键字enum声明的枚举常量
5.使用#define定义的常量(已摒弃,不推荐)

3.6.1 字面常量
cout << "helloworld" << endl;

其中的helloworld就是一个字符串字面常量。

3.6.2 使用const将变量声明为常量
const double pi = 22.0 / 7;
3.6.3 使用constexpr定义常量表达式
constexpr double GetPi() { return 22.0 / 7; }
constexpr double TwicePi() { return 2 * GetPi(); }

corecrt_math_defines.h中定义了高精度的pi值,可以直接使用:

#include 
#include  //M_PI在此头文件中
int main()
{
	printf("%.48f", M_PI);  //小数点后有效位数为48位
	return 0;
}
3.6.4 枚举

在有些情况下,变量只能有一组特定的取值。可以使用关键字enum来声明枚举。枚举由一组称为枚举量(emumerator)的常量组成。

enum RainbowColors
	{
		Violet = 25,
		Indigo,
		Blue,
		Red
	};
RainbowColors color = Blue;

编译器将枚举量(Voilet等)转换为整数,每个枚举量都比前一个大1。可以指定起始量,如果没有指定,编译器认为起始值为0。
如果需要,也可以初始化地给每个枚举量都指定值。

第4章 管理数组和字符串 4.1 一维数组 4.1.1 数组的初始化
//1. 初始化为同一个值
int myNumbers_0[5] = {0};   
//2. 未指定初始值则默认初始化为0
int myNumbers_1[5] = {};   
//3. 分别初始化为不同的数值
int myNumbers_2[5] = { 1, 2, 3, 4, 5 };
//4. 也可只初始化部分元素
int myNumbers_3[5] = { 1, 2 };
//5. 数组长度使用常量定义
const int ARRAY_LENGTH = 5;
int myNumbers_4[ARRAY_LENGTH ] = { 1, 2, 3, 4, 5 };
//6. 如果知道数组中的初始值,可不指定数组包含的元素数
int myNumbers_5[] = { 1, 2 };
4.2 多维数组 4.2.1 数组的初始化
//1. 可以给数组的每个元素都指定初始值
int solarPanels[2][3] = { {0, 1, 2},{3, 4, 5} };
//2. 也可以将其全部初始化为0
int solarPanels[2][3] = { 0 };
//3. 也可以像定义一维数组一样定义多维数组,因为存储空间是连续的
int solarPanels[2][3] = { 0, 1, 2, 3, 4, 5 };
第5章 使用表达式、语句和运算符 5.3 运算符 5.3.4 前缀运算符(i++/i–)和后缀运算符(++i/–i)

前缀运算符:首先将自身的值赋给左值,然后自身加/减1.
后缀运算符:首先自身加/减1,然后将加减后的值赋给左值.

int startValue = 101;
cout << "Start value of integer being operated: " << startValue << endl;
int postfixIncrement = startValue++;
cout << "Result of Postfix Increment = " << postfixIncrement << endl;
cout << "After Postfix Increment, startValue = " << startValue << endl;
startValue = 101; // Reset
int prefixIncrement = ++startValue;
cout << "Result of Prefix Increment = " << prefixIncrement << endl;
cout << "After Prefix Increment, startValue = " << startValue << endl;
	
startValue = 101; // Reset
int postfixDecrement = startValue--;
cout << "Result of Postfix Decrement = " << postfixDecrement << endl;
cout << "After Postfix Decrement, startValue = " << startValue << endl;
	
startValue = 101; // Reset
int prefixDecrement = --startValue;
cout << "Result of Prefix Decrement = " << prefixDecrement << endl;
cout << "After Prefix Decrement, startValue = " << startValue << endl;

代码运行结果:

5.3.9 高阶逻辑运算符
  1. 异或XOR(^)运算符:相同得0,不同得1
	bool a = true;
	bool b = false;
	cout << (a ^ b) << endl;
5.3.10 按位运算符NOT(~)、AND(&)、OR(|)和XOR(^)

~:按位取反
&:对相应位执行 AND 运算
|: 对相应位执行OR运算
^:对相应位执行XOR运算

//bitset 是一种数据类型,可以对数字进行二进制的显示
//使用时需要包含bitset头文件
cout << "Enter a number (0 - 255): ";
unsigned short inputNum = 0;
cin >> inputNum;

bitset<8> inputBits(inputNum);
cout << inputNum << " in binary is " << inputBits << endl;

bitset<8> bitwiseNOT = (~inputNum);
cout << "Logical NOT ~" << endl;
cout << "~" << inputBits << " = " << bitwiseNOT << endl;

cout << "Logical AND, & with 00001111" << endl;
bitset<8> bitwiseAND = (0x0F & inputNum);// 0x0F is hex for 0001111
cout << "0001111 & " << inputBits << " = " << bitwiseAND << endl;

cout << "Logical OR, | with 00001111" << endl;
bitset<8> bitwiseOR = (0x0F | inputNum);
cout << "00001111 | " << inputBits << " = " << bitwiseOR << endl;

cout << "Logical XOR, ^ with 00001111" << endl;
bitset<8> bitwiseXOR = (0x0F ^ inputNum);
cout << "00001111 ^ " << inputBits << " = " << bitwiseXOR << endl;

运算结果如下:

5.3.11 按位左移运算符(<<)和右移运算符(>>)
  1. 左移运算符:将变量乘以2;
int doubleValue = num << 1;
  1. 右移运算符:将变量除以2;
int doubleValue = num >> 1;
第6章 三大条件语句 6.1.4 使用 switch-case 进行条件处理
enum DaysOfWeek
{
		Sunday = 0,
		Monday,
		Tuesday,
		Wednesday,
		Thursday,
		Friday,
		Saturday
};
int dayInput = Sunday; // Initialize to Sunday
cin >> dayInput;

switch (dayInput)
{
case Sunday:
		cout << "Sunday was named after the Sun" << endl;
		break;

case Monday:
		cout << "Monday was named after the Moon" << endl;
		break;
		
case Tuesday:
		cout << "Tuesday was named after Mars" << endl;
		break;

case Wednesday:
		cout << "Wednesday was named after Mercury" << endl;
		break;
				
case Thursday:
		cout << "Thursday was named after Jupiter" << endl;
		break;
						
case Friday:
		cout << "Friday was named after Venus" << endl;
		break;
					
case Saturday:
		cout << "Saturday was named after Saturn" << endl;
		break;
								
default:
		cout << "Wrong input, execute again" << endl;
		break;
}
6.2.5 使用基于范围的for循环
for (auto anElement : elements) {
...
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1026057.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号