开始进行算法笔记的学习,在此纪录下来,同时做为本身往后复习资料。html
1.基本语法ios
#include <iostream>
using namespace std;
int main(){
cout<<"hello"<<endl;
return 0;
}算法
以上是最基础的一个代码端:express
引入头文件:#include <iostream>数组
引入命名空间(至关于2个不一样的人能够拥有同一件物品,而且使用的时候没有冲突):using namespace stdapp
main():至关于程序的入口ide
cout/cin:C++中用于输出和输入,相似于C语言中的printf和scanf,后面详细介绍函数
2.C++中数据类型oop
#include<iostream> using namespace std; int main() { cout << "type: \t\t" << "************size**************"<< endl; cout << "bool: \t\t" << "所占字节数:" << sizeof(bool); cout << "\t最大值:" << (numeric_limits<bool>::max)(); cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl; cout << "char: \t\t" << "所占字节数:" << sizeof(char); cout << "\t最大值:" << (numeric_limits<char>::max)(); cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl; cout << "signed char: \t" << "所占字节数:" << sizeof(signed char); cout << "\t最大值:" << (numeric_limits<signed char>::max)(); cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl; cout << "unsigned char: \t" << "所占字节数:" << sizeof(unsigned char); cout << "\t最大值:" << (numeric_limits<unsigned char>::max)(); cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl; cout << "wchar_t: \t" << "所占字节数:" << sizeof(wchar_t); cout << "\t最大值:" << (numeric_limits<wchar_t>::max)(); cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl; cout << "short: \t\t" << "所占字节数:" << sizeof(short); cout << "\t最大值:" << (numeric_limits<short>::max)(); cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl; cout << "int: \t\t" << "所占字节数:" << sizeof(int); cout << "\t最大值:" << (numeric_limits<int>::max)(); cout << "\t最小值:" << (numeric_limits<int>::min)() << endl; cout << "unsigned: \t" << "所占字节数:" << sizeof(unsigned); cout << "\t最大值:" << (numeric_limits<unsigned>::max)(); cout << "\t最小值:" << (numeric_limits<unsigned>::min)() << endl; cout << "long: \t\t" << "所占字节数:" << sizeof(long); cout << "\t最大值:" << (numeric_limits<long>::max)(); cout << "\t最小值:" << (numeric_limits<long>::min)() << endl; cout << "unsigned long: \t" << "所占字节数:" << sizeof(unsigned long); cout << "\t最大值:" << (numeric_limits<unsigned long>::max)(); cout << "\t最小值:" << (numeric_limits<unsigned long>::min)() << endl; cout << "double: \t" << "所占字节数:" << sizeof(double); cout << "\t最大值:" << (numeric_limits<double>::max)(); cout << "\t最小值:" << (numeric_limits<double>::min)() << endl; cout << "long double: \t" << "所占字节数:" << sizeof(long double); cout << "\t最大值:" << (numeric_limits<long double>::max)(); cout << "\t最小值:" << (numeric_limits<long double>::min)() << endl; cout << "float: \t\t" << "所占字节数:" << sizeof(float); cout << "\t最大值:" << (numeric_limits<float>::max)(); cout << "\t最小值:" << (numeric_limits<float>::min)() << endl; cout << "size_t: \t" << "所占字节数:" << sizeof(size_t); cout << "\t最大值:" << (numeric_limits<size_t>::max)(); cout << "\t最小值:" << (numeric_limits<size_t>::min)() << endl; cout << "string: \t" << "所占字节数:" << sizeof(string) << endl; // << "\t最大值:" << (numeric_limits<string>::max)() << "\t最小值:" << (numeric_limits<string>::min)() << endl; cout << "type: \t\t" << "************size**************"<< endl; return 0; }
结果显示学习
type: ************size************** bool: 所占字节数:1 最大值:1 最小值:0 char: 所占字节数:1 最大值: 最小值:? signed char: 所占字节数:1 最大值: 最小值:? unsigned char: 所占字节数:1 最大值:? 最小值: wchar_t: 所占字节数:4 最大值:2147483647 最小值:-2147483648 short: 所占字节数:2 最大值:32767 最小值:-32768 int: 所占字节数:4 最大值:2147483647 最小值:-2147483648 unsigned: 所占字节数:4 最大值:4294967295 最小值:0 long: 所占字节数:8 最大值:9223372036854775807 最小值:-9223372036854775808 unsigned long: 所占字节数:8 最大值:18446744073709551615 最小值:0 double: 所占字节数:8 最大值:1.79769e+308 最小值:2.22507e-308 long double: 所占字节数:16 最大值:1.18973e+4932 最小值:3.3621e-4932 float: 所占字节数:4 最大值:3.40282e+38 最小值:1.17549e-38 size_t: 所占字节数:8 最大值:18446744073709551615 最小值:0 string: 所占字节数:24 type: ************size**************
3.定义变量以及类型
定义变量:
int d = 3, f = 5; // d 和 f 的声明 int d = 3, f = 5; // 定义并初始化 d 和 f byte z = 22; // 定义并初始化 z char x = 'x'; // 变量 x 的值为 'x'
变量的类型间是能够互相转换的,转换又分为自动转换和强制转换。
自动转换规则:
4.做用域
局部变量:局部变量声明后其声明周期仅仅在定义的该局部空间内有效(即{ }之间)
int main () { // 局部变量声明 int a, b; int c; // 实际初始化 a = 10; b = 20; c = a + b; return 0; }
全局变量:在整个定义域内均有效果,直至程序结束才消亡
#include <iostream> using namespace std; // 全局变量声明 int g;
形式参数:用于传递参数,将函数以外的数值传递进函数内部
小总结:
在程序中,局部变量和全局变量的名称能够相同,可是在函数内的局部变量与全局变量是两个独立的变量,互不影响。
当变量间出现重名的状况下,做用域小的屏蔽做用域大的。
存储在静态数据区的变量会在程序刚开始运行时就完成初始化,也是惟一的一次初始化(static)。
5.C++运算符
主要讲&、|、!,其中&&与||具备短路特性。
短路特性:即当二者之间进行&&或者||运算的时候,若是其中一者已经明确错误或者正确,则后面不在运行。
先判断左边的值是否为真。
若是为假,那么整个表达式毫无疑问也为假。
若是为真,那就还须要判断右值,才能知道整个式子的值。
这个时候判断右值的过程就起了一个if的做用,能够利用这个过程判断右边表达式是否为真。
举例说明:
&用法:只有2个都为1,那么结果是1,不然为0:1&1=1,1&0=0,0&0=0,0&1=0;
|用法:只要有一个是1,那么结果为1,不然为0:1&1=1,1&0=1,0&0=0,0&1=1;
6.C++循环、判断、函数
1、循环:
while 循环 | 当给定条件为真时,重复语句或语句组。它会在执行循环主体以前测试条件。 |
for 循环 | 屡次执行一个语句序列,简化管理循环变量的代码。 |
do...while 循环 | 除了它是在循环主体结尾测试条件外,其余与 while 语句相似。 |
嵌套循环 | 您能够在 while、for 或 do..while 循环内使用一个或多个循环。 |
通常来讲,循环分为循环条件、循环体和结束条件。
若是一个循环没有结束条件,那么可能变成死循环,死循环极其耗费内存,所以写代码必定记得加结束条件。
递归相似于一个不断嵌套的循环,所以由递归体、结束时跳出的条件,可是递归因为嵌套太多,而且大多执行的都是重复的代码,所以推荐使用尾递归,我的感受极大的减小了内存消耗。
2、判断:
if 语句 | 一个 if 语句 由一个布尔表达式后跟一个或多个语句组成。 |
if...else 语句 | 一个 if 语句 后可跟一个可选的 else 语句,else 语句在布尔表达式为假时执行。 |
嵌套 if 语句 | 您能够在一个 if 或 else if 语句内使用另外一个 if 或 else if 语句。 |
switch 语句 | 一个 switch 语句容许测试一个变量等于多个值时的状况。 |
嵌套 switch 语句 | 您能够在一个 switch 语句内使用另外一个 switch 语句。 |
判断感受主要注意的就是switch语句,由于通常对于switch语句都不太熟悉。
switch语句:
switch(expression){ case constant-expression : statement(s); break; // 可选的 case constant-expression : statement(s); break; // 可选的 // 您能够有任意数量的 case 语句 default : // 可选的 statement(s); }
switch语句规则:
3、函数
函数定义:
函数是一组一块儿执行一个任务的语句。每一个 C++ 程序都至少有一个函数,即主函数 main() ,全部简单的程序均可以定义其余额外的函数。
能够把代码划分到不一样的函数中。如何划分代码到不一样的函数中是由您来决定的,但在逻辑上,划分一般是根据每一个函数执行一个特定的任务来进行的。
函数声明告诉编译器函数的名称、返回类型和参数。函数定义提供了函数的实际主体。
格式:
return_type function_name( parameter list )
{
body of the function
}
Lambda函数:
// 定义简单的lambda表达式 auto basicLambda = [] { cout << "Hello, world!" << endl; }; // 调用 basicLambda(); // 输出:Hello, world!
上述是一个Lambda函数小例子:
[ ] 是 lambda 引出符。编译器根据该引出符判断接下来的代码是不是 lambda 函数。
{ }:函数体。内容与普通函数同样,不过除了可使用参数以外,还可使用全部捕获的变量
7.C++数组、字符串
数组:
静态 int array[100]; 定义了数组 array,并未对数组进行初始化
静态 int array[100] = {1,2}; 定义并初始化了数组 array
动态 int* array = new int[100]; delete []array; 分配了长度为 100 的数组 array
动态 int* array = new int[100]{1,2}; delete []array; 为长度为100的数组array而且初始化前两个元素
字符串:
定义字符串:char str[11] = "Hello";
string类提供了一系列针对字符串的操做,好比:
实例以下:
#include <iostream> #include <string> using namespace std; int main() { //定义一个string类对象 string http = "www.runoob.com"; //打印字符串长度 cout<<http.length()<<endl; //拼接 http.append("/C++"); cout<<http<<endl; //打印结果为:www.runoob.com/C++ //删除 int pos = http.find("/C++"); //查找"C++"在字符串中的位置 cout<<pos<<endl; http.replace(pos, 4, ""); //从位置pos开始,以后的4个字符替换为空,即删除 cout<<http<<endl; //找子串runoob int first = http.find_first_of("."); //从头开始寻找字符'.'的位置 int last = http.find_last_of("."); //从尾开始寻找字符'.'的位置 cout<<http.substr(first+1, last-first-1)<<endl; //提取"runoob"子串并打印 return 0; }
8.C++基本输入输出
基本上使用iostream这个头文件
标准输出流cout:
#include <iostream> using namespace std; int main( ) { char str[] = "Hello C++"; cout << "Value of str is : " << str << endl; } 结果:Value of str is : Hello C++
标准输入流cin:
#include <iostream> using namespace std; int main( ) { char name[50]; cout << "请输入您的名称: "; cin >> name; cout << "您的名称是: " << name << endl; } 结果: 请输入您的名称: cplusplus 您的名称是: cplusplus
我的刚刚开始写博客,有错误的地方麻烦指出,谢谢!