C转C++ 个人总结
# C转C++ 个人总结
1.使用C++的好处
2.using namespace std
3.cin和cout
#include<iostream> //必备的头文件
using namespace std; //std 的名称空间
int main(void)
{
int n;
cin >> n; //scanf("%d",&n);
cout << "Hello World!" << n++ <endl;
return 0;
}
#include<iostream> //必备的头文件
int main(void)
{
int n;
std::cin >> n; //scanf("%d",&n);
srd::cout << "Hello World!" << n++ <endl;
return 0;
}
结果:
4.头文件 -> 去掉.h直接在开头加c
eg:
#include<cstring>
#include<cmath>
5.变量声明 (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;
}
结果:
6.bool变量 (非0为true 0为false)
#include<iostream>
using namespace std;
int main(void)
{
bool flag = true;
bool flag2 = -1;
bool flag3= 0;
cout << flag << " " << flag2 << " " << flag3 << endl;
return 0;
}
结果:输出 1 1 0

7.const定义常量
#include<iostream>
using namespace std;
int main(void){
const int MAX = 200;
cout << MAX << endl;
return 0;
}
结果:输出 200

8.string类
#include<iostream>
using namespace std;
int main(void){
string s = "hello"; //定义
string s2 = " world!";
string s3 = s + s2; //拼接
//cin >> s; //只能输入一个单词
getline(cin,s); //得到一行
cout << s << endl;
cout << s.length() << endl; //s有多长
return 0;
}
#include<iostream>
using namespace std;
int main(void){
string s = "Hello World!";
cout << s <<endl;
string s_sub = s.substr(6,5);//s1 = s.substr(n,m); 从第n个字符开始取m个字符
string s_sub2 = s.substr(6);//s2 = s.substr(n);从第n个字符开始取后面全部
cout << s_sub << endl;
cout << s_sub2 <<endl;
return 0;
}
结果:
9.结构体
#include<iostream>
using namespace std;
struct stu{
string name;
int age;
};
int main(void)
{
stu a[10];
return 0;
}
10.引用&
#include<iostream>
using namespace std;
void c(int &a)
{
a += 1;
}
int main(void)
{
int a = 4;
c(a);
cout << a << endl;
return 0;
}
结果:
11.vector(可变数组)
头文件:#include
#include<iostream>
#include<vector>
using namespace std;
int main(void)
{
vector <int> v;
v.resize(10); //分配数组大小
for(int i=0;i<10;i++)
v[i] = i;
v.push_back(11); //末尾添加新的数据
for(int i=0;i<11;i++)
cout << v[i] <<" ";
//cout << v.size() <<endl;
return 0;
}
#include<iostream>
#include<vector>
using namespace std;
int main(void)
{
vector <int> v(10,2); //10个空间,每个空间数为2
//vector <int> v(10);等价于 vector <int> v(10,0);
for(int i=0;i<10;i++)
cout << v[i] <<" ";
return 0;
}
#include<iostream>
#include<vector>
using namespace std;
int main(void)
{
vector <int> v;
v.resize(10);
v.push_back(11);
//迭代器:自动把容器中的值都遍历一遍
for(auto p = v.begin();p != v.end();p++)
cout << *p <<" ";
return 0;
}
结果:

12.set
set是集合,它里面的元素各不相同,而且元素会按照从小到大排序
#include<iostream>
#include<set>
using namespace std;
int main(void)
{
set <int> s;
s.insert(1);
s.insert(2);
s.insert(3);
for(auto p=s.begin();p!=s.end();p++)
{
cout << *p << " ";
}
cout << endl;
cout << (s.find(2) != s.end()) <<endl;
cout << (s.find(4) != s.end()) <<endl;
s.erase(1); // 删除1
cout << (s.find(1) != s.end()) <<endl; //是否存在1
return 0;
}
结果:
13.map(键值对)
#include<iostream>
#include<map>
using namespace std;
int main(void)
{
map <string,int> m;
/*
map是键值对,它会自动将所有的键值对从小到大排序
*/
m["hello"] = 4;
m["world"] = 3;
m["iloveShanxi"] = 5;
m["ha"] = 6;
//cout << "hello: " <<m["hello"] << endl; //已知元素进行输出
for(auto p=m.begin();p!=m.end();p++)//p 结构体指针
cout << p->first << ": " << p->second << endl;//
cout << "map的长度为:" << m.size() <<endl;
return 0;
}
结果:
14.stack(栈)
#include<iostream>
#include<stack>
using namespace std;
int main(void)
{
stack <int> s;
s.push(1);
s.push(2);
s.push(3);
s.pop();
s.push(5);
/*
栈只能获取到栈顶元素,不能将其自动遍历
*/
cout << s.top() << endl; //获取栈顶元素
cout << "栈的长度是:" << s.size() << endl; //获取长度
return 0;
}
结果:
15.queue(队列)
#include<iostream>
#include<queue>
using namespace std;
int main(void)
{
queue <int> s;
for(int i=1;i<=10;i++)
s.push(i);
cout << "队首为:" << s.front() << endl << "队尾为:" << s.back() <<endl;
s.pop();//出队
cout << "队首为:" << s.front() << endl << "队尾为:" << s.back() <<endl;
s.push(11);//入队
cout << "队首为:" << s.front() << endl << "队尾为:" << s.back() <<endl;
cout << s.size() <<endl;
return 0;
}
结果:
16.unordered_map和unordered_set
#include<iostream>
#include<unordered_map>
#include<unordered_set>
using namespace std;
int main(void)
{
unordered_map <string,int> m;
unordered_set <int> s;
s.insert(1);
s.insert(7);
s.insert(5);
m["hello"] = 1;
m["world"] = 2;
m["ha"] = 3;
m["hlkwe"] = 4;
m["hlkwt"] = 5;
for(auto p=s.begin();p!=s.end();p++)
cout << *p << endl;
for(auto p=m.begin();p!=m.end();p++)
cout << p->first << " " << p->second <<endl;
return 0;
}
结果:
输出是没有规律的(经过哈希表实现)
17.bitset 位运算
#include<iostream>
#include<bitset>
using namespace std;
int main(void)
{
bitset <5> b(19);
cout << b << endl;
for(int i=0;i<b.size();i++)
cout << b[i] << " ";
cout << endl;
cout << "是否有1:" << b.any() << endl;//b中是否有二进制位
cout << "是否不存在1:" << b.none() << endl;//b不存在1吗?
cout << "1的个数:" << b.count() << endl;//b中1的个数
cout << "b中元素个数" << b.size() <<endl;//b中元素的个数
cout << "下标为 i 的元素是不是1:" << b.test(0) << endl;//下标i处是不是1
b.flip(1);//第i位取反
cout << b << endl;
unsigned long a = b.to_ulong();
cout << a << endl;
return 0;
}
结果:
17.bitset 补充
string m = "0110101";
bitset <5> b(m,0,5);
//bitset <5> b("01101");
//输出01101
18.sort 函数
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(void)
{
vector <int> m(10);
for(int i=10;i>0;i--)
m[i]=10-i;
for(int i=0;i<10;i++)
cout << m[i] << " ";
cout << endl;
sort(m.begin(),m.end());//m.end()数组最后一个元素的下一个地址 [)
for(int i=0;i<10;i++)
cout << m[i] << " ";
return 0;
}
结果:
19.使用sort自定义cmp函数
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
/*
cmp如果返回值为真,那么x放在y前面(返回值为假时,交换两个数)
*/
bool cmp(int x,int y)
{
return x>y;
}
int main(void)
{
vector <int> v;
for(int i=1;i<=10;i++)
v.push_back(i);
sort(v.begin(),v.end(),cmp);//从大到小排序
for(auto p=v.begin();p!=v.end();p++)
cout << *p << " ";
cout << endl;
return 0;
}
结果:
20.cctype头文件
#include<iostream>
#include<cctype>
using namespace std;
int main(void)
{
char c = 'A';
cout << "isalpha:" << isalpha(c) << endl;//字母?
cout << "islower:" << islower(c) << endl;//小写字母?
cout << "isupper:" << isupper(c) << endl;//大写字母?
cout << "isalnum:" << isalnum(c) << endl;//字母or数字?
cout << "isspace:" << isspace(c) << endl;//space \t \r \n?
char s = tolower(c);//转为小写字母
cout << s << endl;
char s1 = toupper(c);//转为大写字母
cout << s1 << endl;
return 0;
}
结果:
21.c++11的新特性
22.auto声明
#include<iostream>
#include<vector>
using namespace std;
int main(void)
{
auto x = 19;//初始化,可以让编译器根据初始值直接推断变量的类型
auto y = 1.8;
cout << x << " " << y <<endl;
vector <int> a(10,1);//迭代器
for(auto p=a.begin();p!=a.end();p++)
cout << *p << " ";
return 0;
}
23.基于范围的for循环
#include<iostream>
#include<vector>
using namespace std;
int main(void)
{
int a[5] = {1};//1 0 0 0 0
for(int i:a)/*for(int &i:a)改变原来的值
for(auto i:a)
*/
{
i++; //并没有改变原来的值
}
for(int i:a)
cout << i << " ";
cout << endl;
return 0;
}
#include<iostream>
#include<vector>
using namespace std;
int main(void)
{
vector <int> b(10,1);
for(int i:b)
cout << i << " ";
cout << endl;
return 0;
}
结果:
24.to_string
#include<iostream>
#include<string>
using namespace std;
int main(void)
{
string s = to_string(123.1);//将数字转化为字符变量
cout << s << endl;
printf("%s\n",s.c_str());
return 0;
}
结果:
25.stoi stod
#include<iostream>
#include<string>
using namespace std;
int main(void)
{
int a = stoi("123");//将字符串转化成int型
cout << a-1 << endl;
double b = stod("12.34");//将字符串转化成double型
cout << b-1;
return 0;
}
26.Dev C++设置C++ 11
27.完结
随机推荐
- 前端仿京东、天猫底部购物工具栏toolsBar、购物车栏、底部悬浮栏
快速实现 前端仿京东.天猫底部购物工具栏toolsBar.购物车栏.底部悬浮栏, 详情请访问uni-app插件市场地址:https://ext.dcloud.net.cn/plugin?id=1255 ...
- sql server注入rce实践
背景:在漏洞挖掘中,合理的利用sql注入,可以把注入转换成rce,使一个高危漏洞变成严重漏洞.在红蓝对抗中,利用注入rce,实现内网横向移动.笔者基于漏洞挖掘和红蓝对抗上遇到的sql server注入 ...
- 技术速览|Meta Llama 2 下一代开源大型语言模型
AI 使用大型语言模型(LLM)来理解和生成自然语言.LLM 可以从大量文本中学习并创建有关各种主题的文本,并可以完成比如编写代码.生成歌词.总结文章等任务.但有些 LLM 相关课程成本高昂且封闭,而 ...
- .NET程序的 GDI句柄泄露 的再反思
一:背景 1. 讲故事 上个月我写过一篇 如何洞察 C# 程序的 GDI 句柄泄露 文章,当时用的是 GDIView + WinDbg 把问题搞定,前者用来定位泄露资源,后者用来定位泄露代码,后面有朋 ...
- MariaDB start 报错:mysql-bin.index' not found (Errcode: 2) (Errcode: 13)
问题是修改配置log-bin=/data/mysql/binlog/mysql-bin后出现的. 报错:Errcode: 2 mkdir -p /data/mysql/binlog ## 和正常的DB ...
- 趣图|代码重构前vs重构后
前言 很多程序员对自己写的代码平时很随心所欲,但当有一天让他维护他人的代码,他就会抓狂,很容易激发他体内重构的瘾.(大多数程序员审阅完别人代码后,先会忍不住吐槽一番,然后会忍不住想重构一把,) 在我看 ...
- Qt 生成应用程序(二)软件多图标与文件操作
目录 关联某种文件的默认打开方式 assoc ftype 解决方案 设置文件默认图标 应用软件添加多个图标 综合方法 嘿,各位Qt桌面应用开发的同学们(应该Qt大部分应用场景就是这个吧),上一篇文章中 ...
- Oracle 11g手工建库
搭建环境 1.建立相应的目录 mkdir /u01/app/oracle/oradata/test1 mkdir /u01/app/oracle/fast_recovery_area/test1 mk ...
- tensorflow.js 对视频 / 直播人脸检测和特征点收集
前言: 这里要介绍的是 Tensorflow.js 官方提供的两个人脸检测模型,分别是 face-detection 和 face-landmarks-detection.他们不但可以对视频中的人间进 ...
- 新一代开源流数据湖平台Apache Paimon入门实操-上
@ 目录 概述 定义 核心功能 适用场景 架构原理 总体架构 统一存储 基本概念 文件布局 部署 环境准备 环境部署 实战 Catalog 文件系统 Hive Catalog 创建表 创建Catalo ...