《算法笔记》之基础C/C++入门

开始进行算法笔记的学习,在此纪录下来,同时作为自己日后复习资料。

1.基本语法

#include <iostream>
using namespace std;
int main(){
  cout<<"hello"<<endl;
  return 0;
}

以上是最基础的一个代码端:

  引入头文件:#include <iostream>

  引入命名空间(相当于2个不同的人可以拥有同一件物品,并且使用的时候没有冲突):using namespace std

  main():相当于程序的入口

  cout/cin:C++中用于输出和输入,类似于C语言中的printf和scanf,后面详细介绍

2.C++中数据类型

#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'

变量的类型间是可以互相转换的,转换又分为自动转换和强制转换。

自动转换规则

  • 1、若参与运算量的类型不同,则先转换成同一类型,然后进行运算。
  • 2、转换按数据长度增加的方向进行,以保证精度不降低。如int型和long型运算时,先把int量转成long型后再进行运算。     a、若两种类型的字节数不同,转换成字节数高的类型     b、若两种类型的字节数相同,且一种有符号,一种无符号,则转换成无符号类型
  •  3、所有的浮点运算都是以双精度进行的,即使仅含float单精度量运算的表达式,也要先转换成double型,再作运算。
  •  4、char型和short型参与运算时,必须先转换成int型。
  •  5、在赋值运算中,赋值号两边量的数据类型不同时,赋值号右边量的类型将转换为左边量的类型。如果右边量的数据类型长度比左边长时,将丢失一部分数据,这样会降低精度

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++循环、判断、函数

  一、循环:

while 循环 当给定条件为真时,重复语句或语句组。它会在执行循环主体之前测试条件。
for 循环 多次执行一个语句序列,简化管理循环变量的代码。
do...while 循环 除了它是在循环主体结尾测试条件外,其他与 while 语句类似。
嵌套循环 您可以在 while、for 或 do..while 循环内使用一个或多个循环。

  一般来说,循环分为循环条件、循环体和结束条件。

  如果一个循环没有结束条件,那么可能变成死循环,死循环极其耗费内存,因此写代码一定记得加结束条件。

  递归类似于一个不断嵌套的循环,因此由递归体、结束时跳出的条件,但是递归由于嵌套太多,并且大多执行的都是重复的代码,因此推荐使用尾递归,个人感觉极大的减少了内存消耗。

  二、判断:

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语句规则:

  • switch 语句中的 expression 必须是一个整型或枚举类型,或者是一个 class 类型,其中 class 有一个单一的转换函数将其转换为整型或枚举类型。
  • 在一个 switch 中可以有任意数量的 case 语句。每个 case 后跟一个要比较的值和一个冒号。
  • case 的 constant-expression 必须与 switch 中的变量具有相同的数据类型,且必须是一个常量或字面量。
  • 当被测试的变量等于 case 中的常量时,case 后跟的语句将被执行,直到遇到 break 语句为止。
  • 当遇到 break 语句时,switch 终止,控制流将跳转到 switch 语句后的下一行。
  • 不是每一个 case 都需要包含 break。如果 case 语句不包含 break,控制流将会 继续 后续的 case,直到遇到 break 为止。
  • 一个 switch 语句可以有一个可选的 default case,出现在 switch 的结尾。default case 可用于在上面所有 case 都不为真时执行一个任务。default case 中的 break 语句不是必需的。

  三、函数

  函数定义:

    函数是一组一起执行一个任务的语句。每个 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类提供了一系列针对字符串的操作,比如:

  •  1. append() -- 在字符串的末尾添加字符
  •  2. find() -- 在字符串中查找字符串
  •  4. insert() -- 插入字符
  •  5. length() -- 返回字符串的长度
  •  6. replace() -- 替换字符串
  •  7. substr() -- 返回某个子字符串
  •  8. ...

  实例如下:

#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

个人刚刚开始写博客,有错误的地方麻烦指出,谢谢!

  

猜你喜欢

转载自www.cnblogs.com/Whgy/p/12162853.html