1. 关于C++头文件
C++的头文件一般是没有像C语言的 .h 这样的扩展后缀的,一般情况下C语言里面的头文件去掉 .h 然后在前面加个 c 就可以继续在C++文件中使用C语言头文件中的函数啦~比如:
1 #include <cmath> // 相当于C语言里面的#include <math.h>
2 #include <cstdio> // 相当于C语言里面的#include <stdio.h>
3 #include <cctype> // 相当于C语言里面的#include <ctype.h>
4 #include <cstring> // 相当于C语言里面的#include <string.h>
2. C++特有的bool变量
在C语言中用0、1表示 true、false。但是在C++中可以直接用true、false,一切非0数表示true,只有0表示false。
3.C++的结构体struct和C语言的结构体的区别
定义好结构体 stu 之后,使用这个结构体类型的时候,C语言需要写关键字 struct ,而C++里面可以省略不写:
1 struct stu {
2 int grade;
3 float score;
4 };
5 struct stu arr1[10]; // C语言里面需要写成struct
6 stu stu arr2[10];// C++里面不用写struct,直接写stu就好了~
4. C++中传值与传引用
记住一句话,传值相当于做了一份复制拷贝,而通过引用传递参数只有一份拷贝,而且能修改传入值。
5. C++ STL之动态数组vector(矢量)的使用
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v; //初始化未设置大小
cout << v.size() << endl;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
cout << v.size() << endl;
vector<int> v1(10); //初始化设置大小
cout << v1.size() << endl;
for (int j = 0; j < 10; ++j) {
v.push_back(j);
}
vector<int> v2(10, 2); //初始化设置大小10并且设置默认值为2
for (int k = 0; k < v2.size(); ++k) {
cout << v2[k] << " ";
}
cout << endl;
for (auto it = v2.begin(); it != v2.end(); it++) { //使用迭代器遍历
cout << *it << " ";
}
return 0;
}
6. C++ STL之集合set的使用
1 #include <iostream>
2 #include <vector>
3 #include <set>
4 using namespace std;
5
6 int main() {
7 set<int> s; //定义一个集合
8 s.insert(1); //向集合插入一个元素
9 cout << *(s.begin()) << endl; //输出集合的首元素(前面的星号表示对指针取值)
10 for (int i = 0; i < 6; ++i) {
11 s.insert(i); //向集合s里插入i
12 }
13 for (auto it = s.begin(); it != s.end(); ++it) { //用迭代器遍历集合s里面的面一个元素
14 cout << *it << " ";
15 }
16 cout << endl << (s.find(2) != s.end()) << endl; //使用find函数查找为2的元素,返回该指针。
17 // 如果查找失败返回末尾end指针
18 s.erase(1); //删除集合s中元素1
19 cout << endl << (s.find(1) != s.end()) << endl; //此时再查找元素1就查不到了
20 return 0;
21 }
7. C++ STL之映射map的使用
1 #include <map>
2 using namespace std;
3
4 int main() {
5 map<string, int> m; //定义一个空的map m,键是string类型,值是int类型
6 m["hello"] = 2; //将key为hello,value为2的键值对存入map中
7 cout << m["hello"] << endl; //访问map中键为"hello"的value,如果key不存在,则返回0
8 cout << m["world"] << endl;
9 m["world"] = 3; // 将"world"键对应的值修改为3
10 m[","] = 1; // 设立一组键值对,键为"," 值为1
11 // 用迭代器遍历,输出map中所有的元素,键用it->first获取,值用it->second获取
12 for (auto it = m.begin(); it != m.end(); it++) {
13 cout << it->first << " " << it->second;
14 }
15 //访问第一个元素,输出它的键和值
16 cout << m.begin()->first << " " << m.begin()->second << endl;
17 //访问最后一个元素,输出它的键和值
18 cout << m.rbegin()->first << " " << m.rbegin()->second << endl;
19 cout << m.size() << endl;
20 return 0;
21 }
8. C++ STL之栈stack的使用
1 #include <stack>
2 using namespace std;
3
4 int main() {
5 stack<int> s; // 定义一个空栈s
6 for (int i = 0; i < 6; i++) {
7 s.push(i); // 将元素i压入栈s中
8 }
9 cout << s.top() << endl; // 访问s的栈顶元素
10 cout << s.size() << endl; // 输出s的元素个数
11 s.pop(); // 移除栈顶元素
12 return 0;
13 }
9. C++ STL之队列queue的使用
1 #include <queue>
2 #include <iostream>
3 using namespace std;
4
5 int main() {
6 queue<int> q; // 定义一个空队列q
7 for (int i = 0; i < 6; i++) {
8 q.push(i); // 将i的值依次压入队列q中
9 }
10 cout << q.front() << " " << q.back() << endl; // 访问队列的队首元素和队尾元素
11 cout << q.size() << endl; // 输出队列的元素个数
12 q.pop(); // 移除队列的队首元素
13 return 0;
14 }
10. C++ STL之无序集unordered_set的使用
1 #include <unordered_set>
2 using namespace std;
3
4 int main() {
5 unordered_set<int> s1;
6 s1.insert(2);
7 s1.insert(3);
8 s1.insert(1);
9 for (auto it = s1.begin(); it != s1.end(); it++) { //unordered_set未按照字母顺序存储
10 cout << *it << " ";
11 }
12 cout << endl;
13 set<int> s2;
14 for (int i = 5; i > 0; --i) {
15 s2.insert(i);
16 }
17 for (auto it = s2.begin(); it != s2.end(); it++) { //set按照字母顺序存储
18 cout << *it << " ";
19 }
20 return 0;
21 }
11. C++ STL之无序映射unordered_map的使用
1 #include <unordered_map>
2 #include <iostream>
3 using namespace std;
4
5 int main() {
6 unordered_map<string, int> unmap;
7 unmap["Banana"] = 7;
8 unmap["Orange"] = 6;
9 unmap["Apple"] = 1;
10 for (auto it = unmap.begin(); it != unmap.end(); it++) {
11 cout << it->first << " ";
12 }
13 unmap.erase("Apple");
14 return 0;
15 }
12. C++的位运算bitset
1 #include <bitset>
2 #include <iostream>
3 using namespace std;
4
5 int main() {
6 bitset<5> b; //长度为5的初始值0的比特数
7 cout << b << endl;
8 // 初始化方式:
9 // bitset<5> b; 都为0
10 // bitset<5> b(u); u为unsigned int,如果u = 1,则输出b的结果为00001
11 // bitset<8> b(s); s为字符串,如"1101",则输出b的结果为00001101,在前面补0
12 // bitset<5> b(s, pos, n); 从字符串的s[pos]开始,n位⻓度
13 // 注意,bitset相当于一个数组,但是它是从二进制的低位到高位分别为b[0]、b[1]......的
14 // 所以按照b[i]方式逐位输出和直接输出b结果是相反的
15 for(int i = 0; i < 5; i++)
16 cout << b[i]; // 如果bitset<5> b("11"); 则此处输出11000(即正常二进制顺序的倒序)
17 cout << endl << b.any(); //b中是否存在1的二进制位
18 cout << endl << b.none(); //b中不存在1吗?
19 cout << endl << b.count(); //b中1的二进制位的个数
20 cout << endl << b.size(); //b中二进制位的个数
21 cout << endl << b.test(2); //测试下标为2处是否二进制位为1
22 b.set(4); //把b的下标为4处置1
23 b.reset(); //所有位归零
24 b.reset(3); //b的下标3处归零
25 b.flip(); //b的所有二进制位逐位取反
26 unsigned long a = b.to_ulong(); //b转换为unsigned long类型
27 return 0;
28 }
13. C++11里面很好用的auto声明
当然这个在算法里面最主要的用处不是这个,而是在STL中使用迭代器的时候, auto 可以代替一大⻓ 串的迭代器类型声明:
1 // 本来set的迭代器遍历要这样写:
2 for(set<int>::iterator it = s.begin(); it != s.end(); it++) {
3 cout << *it << " ";
4 }
5 // 现在可以直接替换成这样的写法:
6 for(auto it = s.begin(); it != s.end(); it++) {
7 cout << *it << " ";
8 }
水滴可以石穿,笨鸟可以先飞!原文链接: https://www.cnblogs.com/sheepcore/p/12362799.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/193563
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!