面试题剑指做不下去,全是c++,总之先简单入门下,能看懂。
我是win下vs2013编程,之前一直下了vs没有用。今天拿来学习下c++
跟着这位b站up主学的,作个笔记。
文章目录
1.c++介绍
1.新建项目
文件-新建-项目-起好名字等
右键源文件-添加新项
属性-连接器-系统-子系统-下拉选择 控制台:console - ctrl +f5
2.标准库、注释、条件编译
报错
因为hello占6位,但是s2只给了2位所以会报错。
#define _CRT_SECURE_NO_WARNINGS //定义宏,必须放在开始,否则报错
#include <cstdio> //标准库
#include <cmath> //数学库
#include<cstring> //字符库
int main(){
printf("hello\n");
double x = 3.14;
printf("%lf %lf", sqrt(x), sin(x));//lf double类型的浮点数,sqrt开根号,sin求正弦
char s[10] = "hello"; //占6个字符 还有一个/0
puts(s);
char s2[16];
strcpy(s2, s);// strcpy 把s 复制给s2
puts(s2);
strcat(s2, "sdfsdf");// strcat 连接s2和sdfsdf
puts(s2);
printf("%d %d\n", strlen(s), strlen(s2));//strlen 查看大小
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio> //标准库
#include<cstring> //字符库
int main(){
char s[10]; //静态输入
strcpy(s, "hello");
puts(s);
}
如果不想让main中的执行,可以改为if 0 - else - endif
int main(){
# if 0
char s[10]; //静态输入
strcpy(s, "hello");
puts(s);
# endif
}
动态输入:
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio> //标准库
#include<cstring> //字符库
#include<malloc.h> //动态
int main(){
# if 0
char s[10]; //静态输入
strcpy(s, "hello");
puts(s);
# else
char *s = (char *)malloc(12 * sizeof(char)); // 动态输入,分配12个char大小的内存空间,最后再转化为char 的指针类型
strcpy(s, "hello world");
puts(s);
# endif
}
3.c++标准输入输出-名字空间
为了防止冲突,所以加上std::
1.#include c++标准输入输出流头文件
第一种方式:
#if 1
#include <iostream> //c++标准输入输出流头文件
int main(){
std::cout << "hello world!\n";
std::cout << 3+4;
return 0;
}
#endif
如果不加std::报错
第二种方式:
在一开始using std::cout;
后面就不用在声明
#if 1
#include <iostream> //c++标准输入输出流头文件
using std::cout;
int main(){
cout << "hello world!\n";
cout << 3+4;
return 0;
}
#endif
同理:std::endl
#if 1
#include <iostream> //c++标准输入输出流头文件
using std::cout;
int main(){
cout << "hello world!"<<std::endl; //std::endl相当于换行符
cout << 3+4;
return 0;
}
#endif
#if 1
#include <iostream> //c++标准输入输出流头文件
using std::cout;
using std::endl;
int main(){
cout << "hello world!"<<endl; //std::endl相当于换行符
cout << 3+4;
return 0;
}
#endif
第三种方式:
引入整个名字空间using namespace std;
#if 1
#include <iostream> //c++标准输入输出流头文件
using namespace std;
int main(){
cout << "hello world!"<<endl; //std::endl相当于换行符
cout << 3+4;
return 0;
}
#endif
输入流std::cin >>
#if 1
#include <iostream> //c++标准输入输出流头文件
using namespace std;
int main(){
cout << "hello world!"<<endl; //std::endl相当于换行符
cout << 3 + 4 << endl;
double radius;
std::cin >> radius; //标准输入流对象cin:输入值给radius,cin相当于键盘
cout << 3.14*radius*radius;
return 0;
}
#endif
小练习:
简单计算器
#if 1
#include<iostream>
using namespace std;
void help(){
cout << "====简单计算器=====\n";
cout << "请输入:左运算数 运算符 右运算符\n";
}
int main(){
while (1){ //while(1) 始终循环
help(); //显示帮助函数
double a, b;
char op;
cin >> a >> op >> b;
if (op == '+')
cout << a + b << endl;
if (op == '-')
cout << a - b << endl;
if (op == '*')
cout << a * b << endl;
if (op == '/')
cout << a / b << endl;
}
return 0;
}
#endif
2.#include文件输入输出流
文件输入输出流:
#if 1
#include<fstream>
#include<iostream>
#include<string>
using namespace std;
int main(){
ofstream oF("test.txt"); //ofstream输出文件流,文件的一个类,oF其参数
oF << 3.14 << " " << "hello world\n"; //输出 3.14 空格 hello world
oF.close();
return 0;
ifstream iF("test.txt"); //ifstream输入文件流
double d;
string str;
iF >> d >> str;
cout << d << " " << str << endl;
}
#endif
4.引用变量、引用形参
1.引用变量&
2.形参
#if 1
void swap(int x, int y){
int t = x;
x = y;
y = t;
}
#include <iostream>
using namespace std;
int main(){
int a = 3, b = 4;
cout << a << '\t' << b << endl;
swap(a, b);
cout << a << '\t' << b << endl;
}
#endif
这里 ab没有发生交换,交换的只有xy
解释如图:
#if 1
#include <iostream>
using namespace std;
void swap(int x, int y){
cout << x << '\t' << y<< endl;
int t = x;
x = y;
y = t;
cout << x << '\t' << y << endl;
}
int main(){
int a = 3, b = 4;
cout << a << '\t' << b << endl;
swap(a, b);
cout << a << '\t' << b << endl;
}
#endif
3.传递指针
为了交换ab,所以使用指针(c语言的写法,c++也可以这么写)
swap(&a,&b)&a,&b指的是a,b的地址,
void swap(int *x,int *y)指的是把a,b的地址写入
但是在swap函数里,*x,*y指的是 xy对应的地址指向的值,也就是ab对应的值。
#if 1
#include <iostream>
using namespace std;
void swap(int *x, int *y){
int t = *x;
*x = *y;
*y = t;
}
int main(){
int a = 3, b = 4;
cout << a << '\t' << b << endl;
swap(&a, &b); //&是取地址
cout << a << '\t' << b << endl;
}
#endif
4.引用形参
c++做法:引用形参,进行交换
#if 1
#include <iostream>
using namespace std;
void swap(int &x, int &y){ //&是引用
int t = x;
x = y;
y = t;
}
int main(){
int a = 3, b = 4;
cout << a << '\t' << b << endl;
swap(a, b);
cout << a << '\t' << b << endl;
}
#endif
5.函数的默认形参、函数重载
1.默认形参
// 默认形参1
#if 1
#include <iostream>
using namespace std;
void print(char ch, int n = 1){ //n是默认为1
for (int i = 0; i < n; i++) //i从0到n
cout << ch;
}
int main(){
print('*'); cout << endl;
print('*', 3); cout << endl;
print('*', 5); cout << endl;
}
#endif
// 默认形参2
#if 1
#include<iostream>
using namespace std;
int add(int x, int y = 2, int z = 3){ //默认形参必须靠右
return x + y + z;
}
int main(){
cout << add(5) << endl;
cout << add(5,7) << endl;
cout << add(5,7,9) << endl;
}
#endif
2.函数重载
函数同名时,int add(int x,int y)与double add(double x,double y)是两个不同的函数。
但是int add(int x,int y)与double add(int x,int y)是两个相同的函数,尽管返回类型不同,但是由于形参类型相同,所以是同一个函数。
//函数重载:函数名相同
#if 1
#include <iostream>
using namespace std;
int add(int x, int y = 2){
return x + y;
}
double add(double x, double y = 2.0){
return x + y;
}
int main(){
cout << add(5,3) << endl;
cout << add(5.3, 7.8) << endl;
}
#endif
为了消除歧义可以进行强制转换
6.函数模板
// 模板函数
#include <iostream>
#include<string>
using namespace std;
template<typename T>
T add(T x, T y = 2){
return x + y;
}
#if 1
int main(){
cout << add<int>(5, 3) << endl;
cout << add<double>(5.3, 7.8) << endl;
cout << add<string>("hello", "world") << endl;
cout << add<double>(5, 7.8) << endl;//产生歧义,也可以写成cout << add(double(5), 7.8) << endl;
cout<< add(5,3)<<endl; //编译器会自动推断模板类似
cout<<add(5.3,7.8)<<endl;
}
#endif