论安全萌新的自我修养

   :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

基础结构

#include <iostream> //包含iostream名字的头文件 作为一种包含功能函数、数据接口声明的载体文件,通常编译器通过头文件找到对应的函数库,把引用的函数实际内容导出来
					//<> 代表标准库的头文件 ""代表引用非标准款的头文件
					//这里的iostream 是C++d的输入输出流, 输入 cin  输出 count都是要用到的
using namespace std;//新的C++标准通常不会使用C的头文件命名.h的后缀格式,没有该格式文件依然可以用到命名空间,这里所以用的std的命名空间

//单独用std内的命名空间
//using std::count;
//using std::cin;
//using std::endl;

int main() // main() 是程序开始执行的地方
{  //{}标识函数的作用域
	cout << "Hello World" << endl; // 输出 Hello World
	//cout:输出流  <<:插入运算法把右边的信息插入到流中 enld:控制符可以进行换行
	return 0; //终止main函数,向调用进程返回值0
}
//	其他注释方法
/*   XXX  */
/*   XXX 
	* XXX
	*/

数据类型

布尔型bool
字符型char
整型int
浮点型float
双浮点double
无类型void
宽字符型wchar_t

转义符

\b	退格
\t	制表
\n	换行
\f	换页
\r	回车
\\	反斜线
\'	单引号
\"	双引号

面向过程&面向对象

面向过程:
以过程为中心
分析处解决问题的所需要步骤,用函数一步步实现,实际使用时候一个个调用即可
不符合人类思维习惯,使用机器方式去处理问题
程序 = 对象 + 对象 + ……

面向对象:
使用OOP技术(Object-Oriented Programming),代码模块只提供特定功能,相互是独立的,增加代码的重用性,便于维护开发更新

本质是设计并扩展自己的数据类型
对象 = 算法 + 数据结构

函数

//函数重载
void print(char const* teststring){
    printf("string",teststring);
}
void print(int testint){
    printf("int",testint);
}
int main(){
    print("hello");   
    print(111);
}

//参数默认值
void jiafa(int a = 1, int b = 10){
    //一些操作
}
int main(){
    add();   	1,10
    add(20);	20,10
    add(20,200)	20,200
}

命名空间

为变量、函数和其他声明提供了分离的作用域

假设A、B同时参与一个系统开发,创建同个叫t的全局变量,如果代码丢一起,t会出现重复定义的错误

如果自己定义相关命名空间,再把t变量放一起就不会出现报错

namespace bianA {
    namespace Nested {
        void hello()
        {
            printf("This is bianA::Nested::hello\n");
        }
    } // 结束嵌套的命名空间Nested
} // 结束命名空间bianA

namespace bianB {
    void hello()
    {
        printf("This is bianB::hello\n")
    }
}

void hello()
{
    printf("This is 全局 hello\n");
}

int main()
{
    // 在使用当前命令空间后,如果没有特别指定,就从“bianB”中取得所需的内容。
    using namespace bianB;

    hello(); // 显示“This is bianB::hello”
    bianA::Nested::hello(); // 显示“This is bianA::Nested::hello”
    ::hello(); // 显示“This is 全局 hello”
}

输入&输出

<<是流的插入运算符
>>是流提取运算符

cin		stdin(标准输入)
cout 	stdout(标准输出)
cerr	stderr(标准错误)

#include <iostream>
using namespace std;

// main() 是程序开始执行的地方
int main()
{
	int aaa;
	cout << "请输入一个数字:\n" << endl;
	cin >> aaa;
	cout << "你的数字是" << aaa << endl;
	cerr << "报错";
}

字符串

//常见拼接
#include <iostream>
using namespace std;

// main() 是程序开始执行的地方
int main()
{
	string A = "Hello";
	string B = "World";

	cout << A + B << endl;
	cout << A + "haha" << endl;
	B.append("111");
	cout << B << endl;
}

引用

引用是一种特殊的指针类型。可以当作某个已存在变量的另一个名字。
引用一旦被定义就不能重新赋值,并且不能被设置为空值。
使用引用时的语法与原变量相同

#include <iostream>
using namespace std;
int main()
{	int    a;
	double b;

	int& r = a;  // 声明引用变量
	double& s = b;

	a = 5;
	cout << "Value of a : " << a << endl;
	cout << "Value of a reference : " << r << endl;

	b = 5.55;
	cout << "Value of b : " << b << endl;
	cout << "Value of b reference : " << s << endl;

	return 0;
}

//进一步把引用当返回值
    //必须在定义函数时在函数名前加上&
    //用引用作函数的返回值的最大好处是在内存中不产生返回值的副本
#include <iostream>
using namespace std;
double values[] = { 1.1, 1.2, 1.3, 1.4, 1.5 };

double& setValues(int i)
{
    return values[i];   // 返回第 i 个元素的引用
}

// 要调用上面定义函数的主函数
int main()
{

    cout << "改变前的值" << endl;
    for (int i = 0; i < 5; i++)
    {
        cout << "values[" << i << "] = ";
        cout << values[i] << endl;
    } //原来的值

    setValues(1) = 2.2; // 改变第 2 个元素
    setValues(3) = 2.4;  // 改变第 4 个元素

    cout << "改变后的值" << endl;
    for (int i = 0; i < 5; i++)
    {
        cout << "values[" << i << "] = ";
        cout << values[i] << endl;
    }
    return 0;
}

//把引用作为参数,形参是引用变量,是实参一个变量,调用函数时候形参指向实参变量,通过形参引用可以改变实参的值
#include <iostream>
using namespace std;
void jiaohuan(int& x, int& y);//声明函数

int main()
{
	int a = 1;
	int b = 2;
	cout << "交换前,a 的值:" << a << endl;
	cout << "交换前,b 的值:" << b << endl;

	jiaohuan(a, b);
	cout << "交换后,a 的值:" << a << endl;
	cout << "交换后,b 的值:" << b << endl;
	return 0;
}

// 函数定义
void jiaohuan(int& x, int& y)
{
	int c;
	c = x; // 保存地址 x 的值
	x = y;    // 把 y 给 x
	y = c; // 把 保存的x 给 y  
	return;
}
posted on 2021-01-05 22:27  Yangsir34  阅读(84)  评论(0编辑  收藏  举报