title: C++基础知识-2
date: 2019-09-05 21:32:25
categories:
-因本人才疏学浅,错误之处还请大家指正批评
本文资料来源
本文的目的是让自己复习下那些重点的知识和记录下一些学习的知识,所以知识整理的非常的碎片化.
预处理指令分为两种:
例如
#include <iostream>
#include "test.h"
命名空间的作用:命名空间的作用就是区别不同的函数的作用,C++是一个复杂的语言,同样的一个函数在不同的命名空间下就有着不同的含义,所以我们在使用这些函数时要提前指定命名空间
using namespace 命名空间;
using namespace std;
using std::cout;
using std::cout;
std命名空间里面的Cout是输出函数,我们常用的功能就是输出
语法
cout << "Hello";
//加一个字符串
cout<<endl;
//endl的作用就是换行和"\n"一样
1 只能使用字母,数字和下划线组成
2 不能以数字开头
3 不能使用C++关键字
以两个下划线或大写字母打头的名称被保留
以一个下划线打头的名称被保留
因为视频课里面,大部分的视频课我都是会的,所以略过不讲
只有一个
int a=(int)25.8; //这个是C语言里面强转的语法
int a=int(25.8); //这个是C++里面的标准语法
//都是可以的,C++都支持这两种方式
auto是自动赋值类型的关键字
例子如下
#include<iostream>
#include<string.h>
using namespace std;
int main(void) {
auto a = 10;
cout << a << endl;
return 0;
}
因为这数组大部分的初始化都和C相同,这里只讲一种和C不同的情况
int scoreArray[]{23,2,3};
//C++里面的数组的初始化和C的基本相同
//只不过,C++里面的可以省略=号
C++里面的字符串也可以使用C风格.
大部分都和C一样,这里略过
C++里面的字符串赋值是可以自动拼接的
例子如下:
#include<iostream>
#include<string.h>
using namespace std;
int main(void) {
char str[] = "Today I will learn C++"
".If I have extra time,I will learn the unreal."
"At the night I decided to read my Computer Book";
cout << str << endl;
return 0;
}
用cin读取字符时,不管是空格还是回车,他都会认为是输入的结束
为了避免这种情况,我们可以用cin.getline();cin.getline()基本格式为:
cin.getline(“对象名”,“最大的长度”);
例如
#include<iostream>
#include<string.h>
using namespace std;
int main(void) {
string A1 = "Hello", A2 = "You are Welcome",A;
A = A1 + A2;
cout << A << endl;
return 0;
}
//会输出HelloYou are Welcome
#include<iostream>
#include<array>
#include<string>
using namespace std;
int main() {
string siki = "siki";
string siki2 = "siki2";
cout << (siki == siki2) << endl;
}
一共有两种,默认初始化,和指定初始化
#include<iostream>
using namespace std;
struct Position {
float x;
float y;
float z;
};
int main() {
Position Friends;
Position PlayPos {};
//这种初始化默认为0
Friends.x = 10;
Friends.y = 10;
Friends.z = 10;
Position enemysPos[]{ {1,1,1},{2,4,7} };
}
#include<iostream>
using namespace std;
int main(void) {
int* p1 = NULL;
int* p2 = nullptr;//C++特有的空指针类型
void *p3;
int a = 10;
p3 = &a;
cout << *((int*)p3) << endl;
//p3虽然是空类型的指针,但是在使用之前要加上类型转换
return 0;
}
int* p = new int;
//为变量p开辟内存空间
*p = 100;
//为变量p赋值
cout << *p << endl;
//测试使用变量p
delete p;
//释放掉p的内存空间
#include<iostream>
using namespace std;
int main() {
int* p = new int[20]; //开辟一整块内存空间
delete[] p; //这才是删除数组的正确介绍
return 0;
}
通过模板来实现数组
#include<iostream>
#include<array>
using namespace std;
int main() {
array<int, 9> a1 = { 2,14,341 };
array<int, 9> a2; //这是array给数组赋初始值的模板
//array是可以直接赋值的,但是要求这两个对象的长度和类型是相同的
a2 = a1;
//通过array创建的数组和其他的数组的使用方法都差不多
}
#include<iostream>
#include<array>
#include<string>
using namespace std;
int main() {
int socres[] = { 1,2,3,4,5 };
for (int temp : socres) {
cout << temp << endl;
}
//把每一个socres
//里面的数据依次的赋值给temp
}
#include<iostream>
#include<array>
#include<string>
using namespace std;
int main() {
int a = 1, b = 2, c;
c=a > b ? a : b;
//a大于b吗?如果大于c=a,如果小于
//c=b;
cout << c << endl;
return 0;
}
声明 本文资料来源于网络和书籍,以上图文,贵在分享,版权归原作者以及原出处一切,内容为作者观念,并不代表本人附和其观念和对齐真实性负责,如伤害到您的利益,请您在评论下方留言
原文:https://www.cnblogs.com/a-small-Trainee/p/12400365.html