# 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. Python 标准类库-并发执行之multiprocessing-基于进程的并行

    实践环境 Python3.6 介绍 multiprocessing是一个支持使用类似于线程模块的API派生进程的包.该包同时提供本地和远程并发,通过使用子进程而不是线程,有效地避开了全局解释器锁.因此 ...

  2. 学习websocket,原来这么简单

    简单介绍 websocket WebSocket是一种在TCP连接上进行全双工通信的协议. WebSocket通信协议于2011年被IETF定为标准. 然后WebSocket API也被W3C定为标准 ...

  3. Linux系统运维之zabbix配置tomcat监控

    一.介绍 半年前安装的zabbix监控,当时配合异地的测试人员给A项目做压力测试,主要监控项目部署的几台服务器的内存.CPU信息,以及后来网络I/O等,也没考虑JVM:最近闲下来,想完善下监控,故留此 ...

  4. MySQL 存储引擎 InnoDB 内存结构之缓冲池

    缓冲池是主存储器中的一个区域,在访问 table 和索引数据时InnoDB会对其进行缓存.缓冲池允许直接从内存中访问频繁使用的数据,从而加快处理速度.在专用服务器上,通常将高达 80% 的物理内存分配 ...

  5. Android BottomNavigation底部导航栏使用

    原文地址: Android BottomNavigation底部导航栏使用 - Stars-One的杂货小窝 基本使用 本文侧重点记录一些特殊的样式设置,所以基本使用这里就简单概述一下,详细图文可以去 ...

  6. GO网络编程(二)

    [[Go语言系列视频]老男孩带你21周搞定Go语言[全 242]] https://www.bilibili.com/video/BV1fD4y117Dg/?p=113&share_sourc ...

  7. CF1728A Colored Balls: Revisited题解

    去我的Blog观看 修改时间:2022/9/11修改了格式与标点 修改时间:2022/9/13修改了个别不严谨的语句 题目大意 有 \(n\) 种颜色的球,颜色为 \(i\) 的球为 \(cnt_i\ ...

  8. Vortex Indicator 构建交易策略

    更多精彩内容,欢迎关注公众号:数量技术宅,也可添加技术宅个人微信号:sljsz01,与我交流. 今天的文章,我们将为大家介绍一个与DMI(Directional Movement Index)类似,判 ...

  9. 一个从文件中过滤指定字符串的python3脚本

    from tabulate import tabulate plugin = [ ... ] plugin_v1 = [ ... ] filepath = "E:\\PycharmProje ...

  10. NOI2023 题解

    打的太 shaber 了,于是补补题. D1T1 扫描线. 首先我们可以容斥一下,答案为被一种操作覆盖到的减去被两种操作覆盖到的加上被三种操作覆盖到的. 首先考虑只被一种操作覆盖到的,这很简单,直接上 ...