本蒟蒻对C/C++归纳知识以及万能头文件的使用

头文件
#include < .h> 和 #include " .h"
<>先去系统目录中找头文件,如果没有在到当前目录下找。所以像标准的头文件 stdio.h、stdlib.h等用这个方法。
而""首先在当前目录下寻找,如果找不到,再到系统目录中寻找。 这个用于include自定义的头文件,让系统优先使用当前目录中定义的。

const 和define 的区别
其实有时候我也分不清这二者的区别…
define只是用作文本替换,他的生命周期存在于编译期,存在在代码段,无数据类型。
const 存在于数据段,并在堆栈上分配了空间,他可以被调用传递,也有数据类型。

const 作用
1、定义不可改变的常量,防止误改动,可以通过指针(地址)修改常量的值

2、const修饰函数形式参数:
void fnu(A a,A b){}//效率低
void fnu(A const &a,A const &b){}
A为自定义的一个类,第一个函数在调用时需要复制参数a、b,临时对象的复制构造析构都需要消耗时间。
而第二个采用了引用操作,提高了效率,加const可以保证不改变a,b。
3、const修饰函数返回值:返回值智能杯赋值给加const修饰的同类型指针。
const char *Getchar(void){};
char *ar=Getchar();//false
const char *br=Getchar();//correct,因此br不可被修改
4、const修饰类成员函数:若该成员函数并不对数据成员进行修改, 应尽可能将该成员函数声明为const成员函数。**若将成员函数声明为const,则不允许通过其修改类的数据成员。

class Screen {
    public:
        char get() const;
};
char Screen :: get() const {
    return _screen[_cursor];
} 

typedef
参考文章:

任何声明变量的语句前面加上typedef之后,原来是变量的都变成一种类型。不管这个声明中的标识符号出现在中间还是最后.
typedef int NUM;
NUM a = 10; // 也可写成NUM (a)=10;
NUM(b) = 12; // 也可写成`NUM b = 12;

cin.getline//他可以接受空格并输出!!
cin.getline(接收字符串的变量,接收字符个数,结束字符)
当第三个参数省略时,系统默认为’\0’ (回车键),因此在默认情况下getline可以接受空格并输出。

#include 
using namespace std;
main ()
{
char m[20];
cin.getline(m,5);
cout<<m<<endl;
}

strcat(a,b):字符串连接函数//把字符串b连接到字符串a后面
strcpy(a,b):字符串复制函数//把字符串b复制到字符串a中(a被替换成b)
strcmp(a,b):字符串比较函数
strlen(a,b):字符串长度函数//返回字符串的长度
swap(a,b):交换两个数的值

C/C++输出时如果想输出小数:
C:
float a;
print( “%.2f”, a);
输出小数点后两位有效数字
C++:

#include 
#include 

using namespace std;

int main()
{
    float a = 0.123;
    cout << a << " without setting" << endl;


    cout << setprecision(5) <<a << " without fixed" << endl;


    cout.setf( ios::fixed );
    cout << setprecision(5) << a << " with fixed" << endl;


    cout.setf(ios::fixed);
    cout.precision(5);
    cout << a << " with fixed" << endl;
}

输出结果为:  
0.123 without setting
0.123 without fixed
0.12300 with fixed
0.12300 with fixed

setprecision()控制输出流,所以要写在cout << xxxxxxxxxxxxx < cout.precision可以提前控制输出精度
两种方法都可以设置精度大小

fixed是设置补零,如果不想补零,可以关闭fixed设定

cout.unsetf(ios::fixed);  

三元运算符:
举个简单的例子:return a>b?a:b;
表示:如果zhidaoa大于b 那么返回a
若a小于或等于b 则返回b

floor和ceil和fix和round
使用floor函数。floor(x)返回的是小于或等于x的最大整数。
如: floor(10.5) == 10 floor(-10.5) == -11
使用ceil函数。ceil(x)返回的是大于x的最小整数。
如: ceil(10.5) == 11 ceil(-10.5) ==-10

floor()是向负无穷大舍入,floor(-10.5) == -11;
ceil()是向正无穷大舍入,ceil(-10.5) == -10

fix
朝零方向取整,如fix(-1.3)=-1; fix(1.3)=1;
floor
朝负无穷方向取整,如floor(-1.3)=-2; floor(1.3)=1;
ceil
朝正无穷方向取整,如ceil(-1.3)=-1; ceil(1.3)=2;
round
四舍五入到最近的整数,如round(-1.3)=-1;round(-1.52)=-2;round(1.3)=1;round(1.52)=2

接下来是我最喜欢用的万能头文件
(主要我喜欢偷懒。。。嘿嘿嘿,不想去想他们的头文件是啥,下面教你们去安装)
#include包含了目前c++所包含的所有头文件!!!!
先来对比一下:

#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
using namespace std;

int main(){    
    return 0;
}

再来看简化(偷懒)之后:

#include
using namespace std;

int main(){
    return 0;
}

是不是简单干净多了
安装步骤:
1、找到资源管理器
2、在解决方案上点击右键进入属性
3、复制有关于include文件夹的路径
例如我的vs下的这个文件路径是:D:\vs2013\VC\atlmfc\include
进入这个文件夹后,手动添加一个bits的文件夹,然后添加bits/stdc++.h头文件,可以自己用vs写一个,也可以在网上下
然后重启VS就可以了

该文件夹下所包含的头文件

// C
#ifndef _GLIBCXX_NO_ASSERT
#include 
#endif
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#if __cplusplus >= 201103L
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#endif

// C++
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#if __cplusplus >= 201103L
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#endif

这个包含了大多数的头文件,有一写例如cpp#include
并不存在,但是这个大大减少了我们的代码量

不过这个头文件也并不是适用于所有的oj
在国内oj中,poj,hdu 不支持这个函数,这几个oj的编译器问题,其他国外的oj,还有台湾的OJ都支持,CF,Topcoder也都支持。
对于牛客网,leetcode等oj网站还是比较适用的
在我平时做题时也可以使用万能头文件,南师大OJ系统上也支持的,像我经常使用的洛谷刷题也是支持的

你可能感兴趣的:(本蒟蒻对C/C++归纳知识以及万能头文件的使用)