# 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.完结

随机推荐

  1. navicate的安装使用

    1 navicat概述 Navicat for MySQL 是管理和开发 MySQL 或 MariaDB 的理想解决方案. 这套全面的前端工具为数据库管理.开发和维护提供了一款直观而强大的图形界面. ...

  2. React后台管理系统 04 配置路径别名、全局样式设置、模块化scss

    ts中对于@符号指定的路径不支持,同时vite中也是不支持的,所以我们需要在vite.config.ts中进行指定配置,path是node中自带的一个模块这里爆红的原因是没有进行声明: 我们使用命令对 ...

  3. SpringBoot基础学习(番外9.1)Spring MVC或Spring Boot配置默认访问页面不生效?

    1.tomcat配置默认访问页面 进入 tomcat 的 conf 目录,编辑 web.xml 文件.在 <web-app></web-app> 添加默认访问页面. <w ...

  4. 解决连接MySQL,报错10061,系统错误5

    mysql登录不上去,报错10061,百度后得,mysql服务未启动.. 方法一.选择dos窗口命令行打开mysql 输入代码 net start mysql 报错,如图所示.系统错误 5 解决办法: ...

  5. ListView选中获取数据并弹出菜单项

    前言 作为一名Android小白,我在编写过程中,使用ListView列表,想要使用他来完成长按弹出菜单选项,并且还要进行事件操作,经过百度编程的经历后,终于成功完成.在此附上这块比较完整的代码,理论 ...

  6. 关于quartus II的导入以前的工程,QSF文件出现的错误的解决方案。

    在有时候打开以前的工程,或者别人做好的例程会遇到一些报错信息.具体报错信息如下: 报错信息语句行: 在文件QSF文件中有几行出错,显示错误读取,即不能打开工程.打开文件发现该几行的PIN 使能信号处于 ...

  7. Robot Framework 自动化测试随笔(二)

    二.Web自动化(1) 1.安装selenium2library库 pip install robotframework-selenium2library   2.指定报告的生成路径 在[Run]标签 ...

  8. while(1) ; CPU 会一直等下去吗

    标题答疑 不会,计算机从外部设备读取数据到存储器,每读取1word的数据,CPU对外设状态进行一次检查. 可能有小伙伴看到这就要退出了,别急,看下去或许会有收获. 首先解决第一个问题:一个字是多少字节 ...

  9. 如何将 dubbo filter 拦截器原理运用到日志拦截器中?

    业务背景 我们希望可以在使用日志拦截器时,定义属于自己的拦截器方法. 实现的方式有很多种,我们分别来看一下. 拓展阅读 java 注解结合 spring aop 实现自动输出日志 java 注解结合 ...

  10. 使用supervisor守护Prometheus进程

    使用supervisor守护Prometheus进程 目录 使用supervisor守护Prometheus进程 安装supervisor 安装Prometheus监控系统 配置supervisor ...