C转C++简易教程(基础篇)

前言

出于有些同学已经学习过c语言但不想投入太多时间重新学习c++,而c++与c有很多相似之处,故出此篇。

基本篇

1.使用C++刷算法的好处

  • 兼容C语言

  • 丰富的STL库

  • string十分好用

  • 时间上差一些(牺牲部分效率)

  • 万能头:#include<bits/stdc++.h>

#include<iostream>
		using namespace std;
	 int main(){
	 std::cout<<"Hello,world!";//使用标准命名空间中的cout 函数
	 return 0;//返回0,结束main函数,编译器一般会自动加上这一行一般可以省略
	 }

输出:Hello,world!
![[Pasted image 20250320210226.png]]

2.using namespace

using namespace std为必加开头
#include<iostream>
	using namespace std;
	 int main(void)
	 {
		 int n;
		 cin >> n;// =scanf("%d",&n);
		 cout <<"Meshddy!" << n++ << endl;//endl = "\n"可以通用
		 return 0;
	 }

运行结果
![[Pasted image 20250321103542.png]]

n++改为++n结果则为4
如果没有using namespace则需要把每个量前加std::
eg. std::cin,std::cout,std::endl

3.cin和cout

运算速度不如scanf和printf

如果实际运行时间超时可以考虑把cincout改为scanf和printf

4.头文件

#include<iostream>
#include<cstring>等价于c语言中的#include<string.h>
#include<cmath>等价于c语言中的#include<math.h>

c语言的库去掉.h前加c则变为c++库

5.变量声明

C++比起 C语言可以直接在for循环里面定义
#include<iostream>
using namespace std;
	 int main(void)
	 {
		 int n;
		 cin >> n;
		 for(int i=0;i<10;i++)
		 cout << n << " ";
		 
		 cout <<endl;
		 
		 for(int i=0;i<10;i++)
		 cout << n+1 << "";
		 
		 return 0;
	 }

输出结果:
![[Pasted image 20250321104816.png]]

6.bool变量

非0为true,0为false
#include<iostream>
using namespace std;

int main(void)
{
	bool flag = true;
	bool flag1 = -1;
	bool flag2 = 0;
	bool flag3 = false;
	
	cout << flag << " " << flag1 << " " << flag2 << " " << flag3 << endl;
	
	return 0;
}

输出结果
![[Pasted image 20250321110132.png]]

7.const定义变量

#include<iostream>
using namespace std;
//C语言中为 #define MAX 150 
int main(void)
{
	const int MAX = 150;
	cout << MAX << endl; 
	return 0;
}

输出结果为
![[Pasted image 20250321111451.png]]

8.string类

定义与拼接

string s = “hello”
s = s1 + s2

#include<iostream>
using namespace std;
	 int main(void)
	 {
		string s = "hello";
		string s2 = "world!";
		string s3 = s + s2;
		cout << s3 << endl;
		 return 0;
	 }

运行结果
![[Pasted image 20250321112102.png]]

输入(出)
  • cin >> s
  • cout << s << endl
#include<iostream>
using namespace std;
	 int main(void)
	 { 
	 string s = "hello";
		 cin >>s;
		cout << s << endl;
		 return 0;
	 }
  • getline(cin,s)想要通过cin输入中间有空格的不止一个单词的词时采用
处理
  • s.length()查询
    输入为hello world!
#include<iostream>
using namespace std;
	 int main(void)
	 {
		string s = "hello world!";
		
		getline(cin,s);
		cout << s << endl;
		cout << s.length() << endl;
		
		 return 0;
	 }

以下为输出
![[Pasted image 20250322185918.png]]

  • s1 =s.substr(n,m)或s1 = s.substr(n)
    查询指定位置的字符第n行第m个
#include<iostream>
using namespace std;

	 int main(void)
	 {
		string s = "hello world!";
		cout << s << endl;
		string s_sub = s.substr(1,2);
		cout << s_sub << endl;
		
		 return 0;
	 }

![[Pasted image 20250322190812.png]]

9.结构体

比起c语言c++可以省去struct

#include<iostream>
using namespace std;

struct stu{
	string name;
	int age;
};
	 int main(void)
	 {
		//struct stu a[10]; C语言中用法
		stu a[10];
		 return 0;
	 }

10.引用&和传值

int &a和int a
C++中引用和传值的做法可以理解为一种简化的c指针用法

#include<iostream>
using namespace std;

void c(int &a)
{
	a+=1;
}
	 int main(void)
	 {
		int a = 4;
		c(a);
		cout << a << endl;
		 return 0;
	 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值