C++要领

#扩展名

常见的有C、c、cxx、cpp、c++等,注意,UNIX区分大小写,这意味着应使用大写的C字符。实际上,小写c扩展名也有效,但标准C才使用小写的c。因此,为避免在UNIX系统上发生混淆,对于C程序应使用c,而对于C++程序则请使用C。如果不在乎多输入一两个字符,则对于某些UNIX系统,也可以使用扩展名cc和cxx

# 注释

// 单行注释
/*
	多行注释
*/

# 函数

函数使用前先声明,或者include头文件

#include <iostream>// 使用cin和cout必须包含
#include <cmath>

int main() {
    using namespace std;// 名称命名空间
/*	
	using std::cin;
	using std::cout;
    using std::endl;	效果等同
*/	
    
/*
	 std::cout << "abc" << site << " feet to the side." << std::endl;	效果等同
*/
    double area;
    cout << "enter the floor area, in square feet,of your home:";
    cin >> area;
    double site = sqrt(area);
    cout << "that's th equivalent of a square " << site << " feet to the side." << endl;
    cout << "how fascinating" << endl;

    return 0;
}

C++能够使用printf( )、scanf( )和其他所有标准C输入和输出函数,只需要包含常规C语言的stdio.h文件。C++的输入工具它们在C版本的基础上作了很多改进。

# 简单变量

# 整型

short、int、long、longlong、unsigned系列

#include <iostream>
#include <climits>

int main() {
    using namespace std;
    int n_int = INT_MAX;
    short n_short = SHRT_MAX;
    long n_long = LONG_MAX;
    long long n_llong = LLONG_MAX;

    cout << sizeof(int) << " bytes" << endl;
    cout << sizeof(short) << " bytes" << endl;
    cout << sizeof(long) << " bytes" << endl;
    cout << sizeof(long long) << " bytes" << endl;
    cout << "Max values" << endl;
    cout << n_int << endl;
    cout << n_short << endl;
    cout << n_long << endl;
    cout << n_llong << endl;

    return 0;
}
/*
4 bytes
2 bytes
4 bytes
8 bytes
Max values
2147483647
32767
2147483647
9223372036854775807
*/

确定常量的类型

cout << "year = " << 1492 << "\n";// 默认int存储1492

char:特殊的整型

# 字符和字符串

'a'是字符

"a"是字符串

# 布尔类型

布尔转为整型

int ans = true; // 赋值为1
int ans = true; // 赋值为1

整型转为布尔

bool start = -100;// true
bool stop = 0;// false

# const 限定符

const int Months = 12;// 定义时候就得初始化

Months变成了只读变量,即使常量

相比#define好在知道指定了类型,且限定作用域内起作用

# 特殊初始化语法

区别于 C 语言的初始化方法

#include <iostream>
#include <climits>

int main() {
    using namespace std;
    int a = 100;
    cout << a << endl;
    int b(10);
    cout << b << endl;
    int c{1};
    cout << c << endl;
    return 0;
}

# 类型转换

# 类型转换的规则

{}是列表初始化,不允许缩窄()

在计算表达式,会进行int提升(bool、char等转换成int)

小范围和大范围运算会转为大范围

强制类型转换:

(long) a // 强制转换类型1,临时作用 --C语言风格
long (a) // -- C++风格
static_cast<long> (a) // --强制类型转换形式2 

# 自动判断类型 auto声明

auto n = 100; // int
auto a = 1.0; // double
autp y = 1.3e12L; // long double

# 数组

# 三要素

  1. 每个元素的类型
  2. 数组名
  3. 元素数量

# 声明

short mouth [12];

其中12元素数量必须已知,不可为变量(但是可以const int Size声明常量)

# 初始化规则

只能在定义数组的时候初始化,可以少于数组数目

int cards[4] = {3,6,8}; // 允许,序号3元素为0
int bards[] = {3,6,8}; // 允许,有三个元素
/*
int hards[4];
hards[4] = {3,6,7,8}; -- 不允许
hards = cards; -- 不允许
*/

可以单个元素赋值

cards[1] = 3;

C++11中=可省略

int bards[] {3,6,8};

{}列表初始化,不允许缩窄

char mards[] = {32342344,6,8}; // 32342344超出范围

# 字符串

字符串是以\0结尾的数组

char cat[] = {'f','a','t','e','s','s','a','\0'};
char fish[] = "Bubbles";

注意:'' 表示字符,""表示字符串,不可互换

空白自动拼接两字符串

strlen()计算字符串长度(没有\0),区分sizeof()

# 读取字符串

cin通过空白字符判断结束

cin.getline(name,20)放入name,读取20-1个字符 – 面向行输入(回车终止并且丢弃回车)

cin.get(name,20)放入name,读取20-1个字符 – 面向行输入(回车终止并且保留回车,无法跨越回车)

cin.get(name,20);// 不可处理回车
cin.get();// 可处理回车

cin.get(name,20).get();// 写法2

cin.clear();// 复位失效位

网站的管理员,似乎是个萌新🤔,CTBUer