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";
}

此时执行代码后会报错

相关推荐
oioihoii1 小时前
C++对象生命周期与析构顺序深度解析
java·开发语言·c++
IMPYLH1 小时前
Lua 的 tonumber 函数
开发语言·笔记·后端·junit·游戏引擎·lua
xlq223221 小时前
24.map set(下)
数据结构·c++·算法
晚风吹长发1 小时前
初步了解Linux中文件描述符-fd
linux·运维·服务器·c++·开发·文件
It's now1 小时前
BeanRegistrar 的企业级应用场景及最佳实践
java·开发语言·spring
赖small强2 小时前
【Linux C/C++开发】Linux C/C++ 高效延迟崩溃分析:基于 mprotect 的内存陷阱技术 (Electric Fence)
linux·c语言·c++·mprotect·buffer overflow
毕设源码-赖学姐2 小时前
【开题答辩全过程】以 基于Java的小区物业管理系统APP的设计与实现为例,包含答辩的问题和答案
java·开发语言
繁华似锦respect2 小时前
C++ & Linux 中 GDB 调试与内存泄漏检测详解
linux·c语言·开发语言·c++·windows·算法
立志成为大牛的小牛2 小时前
数据结构——五十四、处理冲突的方法——开放定址法(王道408)
数据结构·学习·程序人生·考研·算法