c++

【C++基础】语法、数据类型

Posted by 卢小胖 on 2021-09-10
Estimated Reading Time 8 Minutes
Words 1.9k In Total
Viewed Times

基础语法

main函数:

每个单独的程序必须要有一个main函数,除非这个程序是一个库。

int main(){
return 0;
}

int代表返回类型为int数据类型,void代表无返回数据,可以省略void

iostream.h:

这玩意是基础的输入输出头文件,我们一般使用如下:

#include <iostream.h>

using namespace std;

使用.h是老式C++或者C的风格,在新的c++风格中,一般不适用.h后缀:

#include <iostream>

注释代码:

// test.cpp

输出log:

int i = 0;

std::cout << "test" << std::endl;
std::cout << i << std::endl;

获取输入:

    std::cin.get();

或者:
long distance;
cout << "输入距离:" <<endl;
cin >> distance;

你可以这样记:c-in,c-out,代表cpp的输入输出,在这里不建议使用printf,至于为什么我也不知道

名称空间:

有没有发现上面的输入输出很麻烦?那就对了

using namespace std

使用using指令可以将std名称空间中的所有名称都赋予这个类使用。

namespace是指标识符的各种可见范围。命名空间用关键字namespace 来定义。C++标准程序库中的所有标识符都被定义于一个名为std的namespace中,命名空间是C++的一种机制,用来把单个标识符下的大量有逻辑联系的程序实体组合到一起。此标识符作为此组群的名字。

比如打印输出完整语句如下:

std::cout<<std::hex<<3.4<<std::endl;

使用using namespace std:

using namespace std;
int main()
{
cout << "hello world" << endl;//此处也可以改为cout << "hello world\n";
return 0;
}
  • 这样命名空间std内定义的所有标识符都有效,就好像它们被声明为全局变量一样
  • 所谓命名空间,是一种将程序库名称封装起来的方法,它就像在各个程序库中立起一道道围墙。
  • 不建议在头文件中使用using namespace std,仅在CPP中使用。

函数变体:

和java中的重载一样,一个函数可以设置多个不同参数

使用多个函数:

自定义函数的时候需要定义头文件方法,using namespace std放在外部使得所有函数都能访问std成员。

#include <iostream>

using namespace std;

double getSum(double a, double b);


int main(){

double result = getSum(1.0,2.0);

cout<<result<<endl;

return 0;
}

double getSum(double a, double b){
double c = a + b;
return c;
}

基础数据类型

首先需要明确的是,cpp中的基础数据类型有哪些:
char
short
int
double
long
bool
float

还有一些组合类型:
long double
long long
long float
long int
short int

特殊标识符:
unsigned
signed
long
short

具体可以参考:https://www.w3cschool.cn/cpp/cpp-data-types.html

char只有一个字节,取值范围为2的8次方,-128~127,unsigned char代表0-255

short为2字节,int为4字节,long为8字节,float为4字节,double为8字节

signed和unsigned是什么意思?

  • signed是默认存在的,取值范围包括负值
  • unsigned标识无符号类型,简单来说就是不包括负值

比如int取值范围为-2的16次方到+2的16次方

unsigned int取值范围就是0到2的32次方

char默认情况不是有限定符,也不是无限定符,是由C++实现决定的。

如果在操作数据的时候超出了该取值范围,将会怎样?

如下定义了不同int类型的最大最小值:

// 基本数据类型展示

#include <iostream>
using namespace std;

int main(){


int i = INT_MAX;
int i_m = INT_MIN;
cout << i_m << "~" <<i << endl;

int i_8 = INT8_MAX;
int i_8_m = INT8_MIN;
cout << i_8_m << "~" << i_8 << endl;

int i_16 = INT16_MAX;
int i_16_m = INT16_MIN;
cout << i_16_m << "~" << i_16 << endl;

int i_32 = INT32_MAX;
int i_32_m = INT32_MIN;
cout << i_32_m << "~" << i_32 << endl;

unsigned int i_64 = INT64_MAX;
unsigned int i_64_m = INT64_MIN;
cout << i_64_m << "~" << i_64 << endl;


system("pause");
return 0;
}

输出:
-2147483648~2147483647
-128~127
-32768~32767
-2147483648~2147483647
0~4294967295

我们测试下数据类型超出取值范围是什么情况:

int i = INT_MAX;
int i_m = INT_MIN;
cout << i_m << "~" <<i << endl;
输出最小值和最大值:-2147483648~2147483647

//最大值+1,最小值-1
i++;
i_m--;

cout << i_m << "~" <<i << endl;
输出:2147483647~-2147483648

可以看到数据的取值范围向是一个闭环,对于无符号类型也是一样的。

const限定符:

kotlin中生成编译器不可修改常量:const val TEST = 1

c++中也有const限定符,用法:


#include <iostream>
using namespace std;

const int Month = 1;
const int arr[3] = {1,2,3};
#define NUM 5;

int main(){

const int Weekend = 2;

cout<<Month<<endl<<Weekend<<endl;

for (size_t i = 0; i < 3; i++)
{
cout << arr[i] << endl;
}


system("pause");
return 0;
}

传统C语言中使用#define定义:

#define NUM 5;

但是在C++中不再建议使用

浮点数:

cpp中可以用E表示法来显示浮点类型,比如

float i = 12E3;

float j = 12E-3;

float k = -12E3;

float l = 3.14E-10;
  • i表示12乘以10的3次方
  • j表示12除以10的3次方
  • k表示负值12乘以10的3次方

typedef 声明:

定义别名,和kotlin中的typealias类型别名有点相似,只不过在kotlin中很少用到。

typedef int HAHHA;

int main(){

HAHHA i = 123;
cout<<i<<endl;

system("pause");
return 0;
}

枚举enum

enum Color {red,blue,green};


int main(){
Color color = red;
cout<<color<<endl;
return 0;
}

这时候color输出为0,因为枚举类默认值为0,依次递增,也可以自定义值。

字符串:
可以用char[]来表示字符串:

char site[7] = {'R', 'U', 'N', 'O', 'O', 'B', '\0'};
char name[] = {"hahahahahah"};
char tets[7];

cout << site << endl;
cout << name <<endl;

strcpy(tets,site);
cout << tets <<endl;

使用string:

加入头文件:
#include <cstring>

string title;
cout <<"输入title:"<<endl;
cin>>title;
cout << title <<endl;

字符串操作:

// 字符串赋值拼接操作

// 复制char
strcpy(tets,site);
cout << tets <<endl;

//获取长度
int length = strlen(name);
cout << length <<endl;

int length2 = title.size();
cout << length2 <<endl;

//拼接
cout << strcat(site,name) << endl;
string id = "1234";
string result = name + id;
cout << result <<endl;

struct

创建一个结构体:

struct People
{
int id;
string name;
float weight;
};

创建:

People people;
people.id = 1;
people.name = "xiaoming";
people.weight = 130.0f;

c++11之后可以简写:

// c++11
People test {
2,"hah",120.0f
};

可以创建默认对象:

struct People
{
int id;
string name;
float weight;
} people {
1,"xlu",140.0f
};

结构数组:

//结构数组
People peopleArr[2] = {
{
1,"xlu",140.0f
},
{
1,"xlu",140.0f
}
};

共用体:

共用体包含多种类型成员,但同时只能使用一种

union ONE
{
/* data */
int i;
float j;
double ids[10];
};

int main(){

ONE one;
one.i = 123;
cout << one.i <<endl;

one.j = 1.0f;
cout << one.i <<endl; //one.i将会丢失
cout << one.j <<endl;

system("pause");
return 0;
}

配合结构体使用:

struct people
{
string name;
union TEST
{
int id;
float id_f;
} test;

};

也可以设置为匿名共用体:

struct PEOPLE2
{
string name;
union {
int id;
float id_f;
};
};