C++学习记录1——C++基础

5 篇文章 0 订阅

一、C++基本语法

1.C++程序结构

//#include <bits/stdc++.h>
#include <iostream>
using namespace std;
 
int main()
{
   cout << "Hello World"; 
   cout << "Hello C++" << endl;

   int a;
   cin >> a;
   cout << a << "\n";
   return 0;
}
  • <bits/stdc++.h>:万能头文件
  • iostream,i:输入,o:输出,iostream:输入输出流
  • using namespace std; 告诉编译器使用 std 命名空间。命名空间是 C++ 中一个相对新的概念。
    std是个名称空间标示符,C++标准库中的函数或者对象都是在命名空间std中定义的,所以我们要使用标准函数库中的函数或对象都要使用std来限定。
  • cout << 输出 ;endl:结尾换行
  • cin >> 输入

cout:标准输出流对象,通过标准输出流类ostream创建

2.关于注释的补充

块注释符(//)是不可以嵌套使用的。

#if 0 ... #endif 属于条件编译,0 即为参数。

此外,我们还可以使用 #if 0 ... #endif 来实现注释,且可以实现嵌套,格式为:

#if 0
   code
#endif 

可以把#if 0改成 #if 1 来执行 code 的代码。

这种形式对程序调试也可以帮助,测试时使用#if 1 来执行测试代码,发布后使用 #if 0 来屏蔽测试代码。

#if 后可以是任意的条件语句。

下面的代码如果 condition 条件为 true 执行 code1 ,否则执行 code2。

#if condition
  code1
#else
  code2
#endif

3.关于万能头文件的说明

#include <bits/stdc++.h>几乎包含所有的可用到的C++库函数

1)头文件内容:

// C++ includes used for precompiling -*- C++ -*-
 
// Copyright (C) 2003-2013 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
 
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
 
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <Licenses - GNU Project - Free Software Foundation>.
 
/** @file stdc++.h
 *  This is an implementation file for a precompiled header.
 */
 
// 17.4.1.2 Headers
 
// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
 
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
 
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
 
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

2)使用方式:

在VS路径
C:\Program Files(x86)\Microsoft\VisualStudio\2019\Community\VC\Tools\MSVC\14.29.30133\include下新建bits文件夹,里面新建一个名为stdc++.h的头文件,把上面的stdc++.h源文件复制进去保存。

3)优缺点

优点:

  • 节约导入必要头文件的时间

缺点:

  • 不属于GNU C++库的标准头文件,在部分情况下可能会失败

  • 包含了许多不必要的东西,增加编译时间

  • 不是C++标准的一部分,不可移植,应该避免

  • 编译器每次编译翻译时都必须实际读取和分析每个包含的头文件

二、C++数据类型

注意与C数据类型的不同处

  1. 布尔类型(bool)True=1,False=0.
  2. 字符串类型(string)string占24字节。使用需要包含<string>头文件(VS中可不用)
  3. 宽字符类型(wchar_t)

wchar_t 实际上的空间是和 short int 一样。
typedef short int wchar_t;

4.枚举类型
是C++中的一种派生数据类型,它是由用户定义的若干枚举常量的集合。

如果一个变量只有几种可能的值,可以定义为枚举(enumeration)类型。所谓"枚举"是指将变量的值一一列举出来,变量的值只能在列举出来的值的范围内。

创建枚举,需要使用关键字 enum。枚举类型的一般形式为:

enum 枚举名{ 
     标识符[=整型常数], 
     标识符[=整型常数], 
... 
    标识符[=整型常数]
} 枚举变量;

如果枚举没有初始化, 即省掉"=整型常数"时, 则从第一个标识符开始。
例如,下面的代码定义了一个颜色枚举,变量 c 的类型为 color。最后,c 被赋值为 “blue”。

enum color { red, green, blue } c;
c = blue;

默认情况下,第一个名称的值为 0,第二个名称的值为 1,第三个名称的值为 2,以此类推。但是可以给名称赋予一个特殊的值,添加一个初始值即可。例如,在下面的枚举中,green 的值为 5。

enum color { red, green=5, blue };

在这里,blue 的值为 6,默认情况下,每个名称都会比它前面一个名称大 1,但 red 的值依然为 0。

三、C++堆区开辟内存

使用new 数据类型开辟空间,返回该数据类型对应类型的指针。
使用关键字delete释放

int* p = new int(10);
delete p;

堆区开辟数组,返回数组首地址。

int* arr = new int[10]  //10元素
delete[] arr;  //释放数组时要加中括号[]

四、C++引用

1.引用语法

作用:给变量起别名。
&在C++中表示引用
数据类型 &别名 = 原名

int age=20;
int &a = age;

注意:1.引用必须初始化。int &a; //错误
2.引用初始化后就不可更改。

2.引用做函数参数

作用:利用引用让形参修饰实参。
优点:简化指针(地址传递)修改实参。

void swap(int &a,int &b)  //引用,相当于别名与原名一样,同时变化
{
	int temp=a;
	a=b;
	b=temp;
}

int main()
{
	int a = 10;
	int b = 20;
	swap(a,b);
	cout<<a<<endl;
	cout<<b<<endl;
}
结果a与b的值会交换

3.引用做函数返回值

注意:不要返回局部变量引用
函数返回值为引用,则函数调用可作为左值

int& test1()  //返回引用
{
	int a = 10;
	return a;
}

int main()
{
	int &ref = test();
	cout<<ref<<endl;  //第一次可打印出10,正确(编译器保留)
	cout<<ref<<endl;  //第二次之后丢失,打出乱码
}
int& test2()  //返回引用
{
	static int a = 10;  //改为静态变量
	return a;
}

int main()
{
	int &ref = test();
	cout<<ref<<endl;  
	cout<<ref<<endl;  //结果正常,为10
	test2() = 1000;  //函数调用作为左值
	cout<<ref<<endl;  
	cout<<ref<<endl;  //结果变为1000
}

4.引用的本质

引用的本质在C++内部实现是一个指针常量
引用本质实例

5.常量引用

作用:主要用来修饰形参,防止误操作。
const修饰形参,可防止形参改变实参

int main()
{
	int a = 10;
	//int & ref = 10;//错误,引用必须引一块合法空间
	
	//加const之后,编译器将代码修改 int temp = 10; int &ref = temp;
	const int & ref = 10;//正确,ref已不可修改

五、C++函数高级

1.函数的占位参数

void fun(int a, int)  //只写数据类型占位
{
	cout << "hello" <<endl;
}

int main()
{
	fun(110);
	return 0;
}

占位参数还可以有默认参数

void fun(int a, int  = 10)  
{
	cout << "hello" <<endl;
}

int main()
{
	fun(1);
	return 0;
}

2.函数重载

作用:函数名可以相同,声明功能类似的同名函数,提高复用率
函数重载满足条件:

  • 在同一作用域下
  • 函数名称相同
  • 函数参数类型不同 / 个数不同 / 顺序不同

PS:函数返回值不可以作为函数重载的条件(修改同名函数返回值报错)

注意:
1.引用作为函数重载条件

void fun(int &a)  
{
	cout << "fun(int & a) run." <<endl;
}

void fun(const int &a)  
{
	cout << "fun(const int & a) run." <<endl;
}

int main()
{
	int a = 1;
	fun(a);  //调用fun(int &a)
	fun(1);  //调用fun(const int &a)
	return 0;
}

2.函数重载碰到默认函数参数

应避免如下情况

void fun1(int a)  
{
	cout << "fun1(int a)" <<endl;
}

void fun1(int a, int b = 10)  
{
	cout << "fun1(int a, int b = 10)" <<endl;
}

int main()
{
	//fun(1);  //二义性,报错
	fun(1,10);  //正确
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值