C++:基础知识

基础知识

1、程序

<1>第一个C++程序

复制代码
#include <iostream>

int main() {
	std::cout << "Hello World!\n";
}

输出:

复制代码
Hello World!

<2>函数调用小案例:

复制代码
#include <iostream>

using namespace std;

double square(double x)
{
    return x*x;
}

void print_square(double x)
{
    cout<<"the square of "<< x << " is " <<square(x)<<"\n";
}

int main() {
    print_square(1.234);
}

输出

复制代码
the square of 1.234 is 1.52276

2、函数

<1>函数声明需要给出三部分信息:函数的名字、函数的返回值类型以及调用该函数必须提供的参数数量和类型。

例子:

cpp 复制代码
Elem* next_elem();//无参数,返回一个指向Elem的指针
void exit(int);//接受一个int参数,不返回任何值
double sqrt(double);//接受一个double参数,返回的也是一个double类型

3、类型、变量和生命周期

<1>类型

常用的类型:

cpp 复制代码
bool        //布尔值
char        //字符型
int         //整数
double      //浮点数
unsigned    //非负整数

各个类型的字节大小:

cpp 复制代码
#include <iostream>

using namespace std;

int main() {
    cout<< "bool类型的字节大小:"<<sizeof(bool)<<"\n";
    cout<< "char类型的字节大小:"<<sizeof(char)<<"\n";
    cout<< "int类型的字节大小:"<<sizeof(int)<<"\n";
    cout<< "double类型的字节大小:"<<sizeof(double)<<"\n";
    cout<< "unsigned类型的字节大小:"<<sizeof(unsigned)<<"\n";
}

输出:

cpp 复制代码
bool类型的字节大小:1
char类型的字节大小:1
int类型的字节大小:4
double类型的字节大小:8
unsigned类型的字节大小:4

<2>运算符

算数运算符:

cpp 复制代码
x+y     //加法
+x      //一元加法
x-y     //减法
-x      //一元减法
x*y     //乘法
x/y     //除法
x%y     //取余

比较运算符:

cpp 复制代码
x==y    //相等
x!=y    //不相等
x<y     //小于
x<=y    //大于
x>=y    //小于等于

逻辑运算符:

cpp 复制代码
x&y     //位与
x|y     //位或
x^y     //位异或
~x      //按位求补
x&&y    //逻辑与
x||y    //逻辑或
!x      //逻辑非
``
例子:

```cpp
#include <iostream>

using namespace std;

int main() {
    bool x = 0;
    bool y = 1;
    cout<<"0&1 = "<<(x&y)<<"\n";
    cout<<"0|1 = "<<(x|y)<<"\n";
    cout<<"0^1 = "<<(x^y)<<"\n";
    cout<<"~0 = "<<(~x)<<"\n";
    cout<<"0&&1 = "<<(x&&y)<<"\n";
    cout<<"0||1 = "<<(x||y)<<"\n";
    cout<<"!0 = "<<(!x)<<"\n";
}

输出:

cpp 复制代码
0&1 = 0
0|1 = 1
0^1 = 1
~0 = -1
0&&1 = 0
0||1 = 1
!0 = 1

<3>作用域

局部作用域:

生命在函数内的名字叫局部名字。局部名字的作用域从声明它的地方开始,到声明语句所在的块的末尾为止。

类作用域:

名字定义在类内,且位于函数之外,称为成员名字或类成员名字。作用域从声明它的{开始,到声明结束为止。

名字空间作用域:

名字定义在名字空间内,且位于函数外,称为名字空间成员名字。作用域从生命位置开始,到名字空间结束为止。

4、常量

<1>const

cpp 复制代码
const int a;            //a代表常整型数
int const b;            //b代表常整型数
const int *c;           //c代表指向常整型数的指针,即c的值是可变的,*c的是不能变的
int * const d;          //d代表常指针,即d的值是不可变的,但*d是可变的
int const * e const;    //e代表指向常整型数的常指针

<2>常量指针与指针常量

常量指针:

本质上是指针,可以被重新赋值。

const位于*的左侧。

表示方法:

cpp 复制代码
const int * p;
int const * p;
cpp 复制代码
#include <iostream>

using namespace std;

int main() {
    int x = 1;
    const int * p;
    //int const * p;
    p = &x;
    cout<<"x = "<<x<<"\n";
    cout<<"p = "<<p<<"\n";
}

输出:

cpp 复制代码
x = 1
p = 0x7ffe4606d734

常量指针可以被重新赋值:

cpp 复制代码
#include <iostream>

using namespace std;

int main() {
    int x = 1;
    int y = 2;
    const int * p;
    //int const * p;
    p = &x;
    cout<<"x = "<<x<<"\n";
    cout<<"p = "<<p<<"\n";
    p = &y;
    cout<<"y = "<<y<<"\n";
    cout<<"p = "<<p<<"\n";
}
cpp 复制代码
x = 1
p = 0x7ffec6cb2fe4
y = 2
p = 0x7ffec6cb2fe0

指针常量:

本质上是常量,指针本身不能改变,指针指向的内容可以改变。

const位于*的右侧。

表示方法:

cpp 复制代码
int * const p;

指针指向的内容可以改变:

cpp 复制代码
#include <iostream>

using namespace std;

int main() {
    int x = 1;
    int y = 2;
    int * const p = &x;
    cout<<"x = "<<x<<"\n";
    cout<<"p = "<<p<<"\n";
    cout<<"*p = "<<*p<<"\n";
    *p = y;
    cout<<"y = "<<y<<"\n";
    cout<<"p = "<<p<<"\n";
    cout<<"*p = "<<*p<<"\n";
}

输出:

cpp 复制代码
x = 1
p = 0x7fff6597bbcc
*p = 1
y = 2
p = 0x7fff6597bbcc
*p = 2

5、指针、数组和引用

<1>指针

定义指针使用*符号

操作过程中&表示取址符,*表示取值符

cpp 复制代码
#include <iostream>

using namespace std;

int main() {
    int x = 1;
    int* p;
    p = &x;
    cout<<"x = "<<x<<"\n";
    cout<<"p = "<<p<<"\n";
    cout<<"*p = "<<*p<<"\n";
}

输出:

cpp 复制代码
x = 1
p = 0x7ffd8f169f04
*p = 1

<2>数组

遍历数组里的值

cpp 复制代码
#include <iostream>

using namespace std;

int main() {
    int array[] = {1,2,3,4,5};
    for(int i = 0;i < 5;i++)//普通循环
        cout<<array[i]<<"\n";
}
cpp 复制代码
1
2
3
4
5

对数组进行拷贝

cpp 复制代码
#include <iostream>

using namespace std;

int main() {
    int array1[] = {1,2,3,4,5};
    int array2[5];
    for(int i = 0;i < 5;i++)
        array2[i] = array1[i];//方式一
    for(int i = 0;i < 5;i++)
        cout<<array2[i]<<"\n";
    cout<<"======\n";
    for(auto array3:array1)//方式二
        cout<<array3<<"\n";
}

输出:

cpp 复制代码
1
2
3
4
5
======
1
2
3
4
5

<3>引用

&表示引用,类似于指针,区别在于引用无须使用*访问所引用的值。
<1>引用在排序中的使用

cpp 复制代码
#include <iostream>

using namespace std;

void swap(int &x,int &y){
    int temp;
    temp = x;
    x = y;
    y = temp;
}

int main() {
    int a = 1;
    int b = 2;
    cout<<"排序前:\n";
    cout<<"a = "<<a<<"\n";
    cout<<"b = "<<b<<"\n";
    swap(a,b);
    cout<<"排序后:\n";
    cout<<"a = "<<a<<"\n";
    cout<<"b = "<<b<<"\n";

}

输出:

cpp 复制代码
排序前:
a = 1
b = 2
排序后:
a = 2
b = 1

<2>常引用

在函数的引用中加const

作用:

1>让变量指向的内存空间只读,防止被修改

2>指向常量

cpp 复制代码
#include <iostream>

using namespace std;

struct teacher{
    string name;
    int age;
};

void getTeacher(const teacher &t){
    cout<<"t.name:"<<t.name<<"\n";
    cout<<"t.age:"<<t.age<<"\n";
}

int main() {
    teacher t;
    t.name = "张三";
    t.age = 28;
    getTeacher(t);
}

输出:

cpp 复制代码
t.name:张三
t.age:28

引用中加const修饰后,被引用的值无法被修改

cpp 复制代码
void getTeacher(const teacher &t){
    //t.age = 29;
    cout<<"t.name:"<<t.name<<"\n";
    cout<<"t.age:"<<t.age<<"\n";
}

此时执行代码后会报错

相关推荐
独自破碎E11 小时前
Leetcode1438绝对值不超过限制的最长连续子数组
java·开发语言·算法
東雪木11 小时前
编程算法学习——数组与排序算法
学习·算法
你撅嘴真丑11 小时前
方格取数 矩阵取数游戏 -动态规划
算法·动态规划
澜莲花11 小时前
python图色之opencv基础---验证码实战
开发语言·python·opencv
沐知全栈开发11 小时前
Numpy 数组操作
开发语言
yaoxin52112311 小时前
279. Java Stream API - Stream 拼接的两种方式:concat() vs flatMap()
java·开发语言
前端小L11 小时前
贪心算法专题(十三):画地为牢的艺术——「划分字母区间」
javascript·算法·贪心算法
@小码农11 小时前
202512 电子学会 Scratch图形化编程等级考试三级真题(附答案)
服务器·开发语言·数据结构·数据库·算法
Cosmoshhhyyy12 小时前
《Effective Java》解读第29条:优先考虑泛型
java·开发语言
一路往蓝-Anbo12 小时前
C语言从句柄到对象 (六) —— 继承与 HAL:父类指针访问子类数据
c语言·开发语言·stm32·嵌入式硬件·物联网