C++
C++一、C++初识1、Hello World2、string类3、结构体4、引用与传参二、STL1、vector(可变数组)(list)2、set(集合) (自动排序的turple)3、map (键值对)(dict)4、stack(栈)5、queue(队列)6、unordered_map unordered_set (无序的键值对和无序的集合)三、位运算四、sort函数五、cctype六、For循环七、算法库1、swap() 交换算法2、sort() 算法3、lower_bound() / upper_bound() (二分查找)4、reverse() (反转)5、max() , min() 最大值最小值6、unique() 去重(结合earse)7、数学函数8、gcd() lcm() 最大公因数,最小公倍数
一、C++初识
1、Hello World
#include<iostream> using namespace std; int main(void){ int n; cin >> n; // cin 相当于scanf cout << "Hello World" << n++ << endl; // cout 相当于printf endl相当于\n return 0; }
如果没有using namespace std ,那么下面封装的方法需要用 std::cout 来使用
2、string类
我了个面向对象编程啊,看来之前学的Python没白学
#include<string> string s1 = "Hello"; string s2 = "World"; string s3 = s1 + S2; cin >> s; // 获取 cout << s << endl; s3.push_back('!'); // 增删 s3.pop_back(); s3.insert(5,' fucking '); cout getline(cin,s); // 获取一整行 cout << s <<endl; cout << s.lenth() <<endl; // s1 = s.substr(n,m); 从第n个字符开始,取后面的m个字符 // s1 = s.substr(n); 把第n个及以后的所有字符全部拷贝 string s_sub = s.substr(); string s = to_string(123.1); // 将数据转化为字符串 int a = stoi("123"); // string to int double b = stod("123.4"); // string to double
3、结构体
C语言可以省去struct
struct stu{ string name; int age; }; stu a[10];
4、引用与传参
#include<isostream> suing namespace std; // &a相当于指针,主函数的a被改变 void c(int &a){ a+=1; } // a 是形参,主函数a不变 void c(int a){ a+=1; } int main(void){ int a = 4; c(a); cout << a << endl; return 0; }
二、STL
1、vector(可变数组)(list)
#include<isostream> #include<vector> suing namespace std; int main(void){ vector <int> v; vector <int> v(10,2); // 分配了10个空间,每个空间填上2 vector <int> v(10); // 等价于全填 0 v.resize(10); // 分配十个空间,默认也填0 for (int i = 0;i<10;i++){ v[i] = i; } for (int i = 0;i<10;i++){ cout << v[i] << " " } v.push_back(11); // 加入一个11 // 迭代器,自动把vector内的所有元素迭代一遍,上面的for循环执行十次就停了 for (auto p = v.begin();p!=v.end();p++) // v.end() 实际上是指向最后一个元素的下一个位置的指针,p是一个指针 cout << *p << " "; vector <int> numbers = {10,20,30,40}; vector<int>::iterator it = numbers.begin(); // 使用迭代器删去“30” it += 2; numbers.erase(it); for(int num:numbers){ cout << num << endl; } it = numbers.begin(); it ++; numbers.insert(it , 25); // 在下标为1的地方插入一个25 return 0; }
2、set(集合) (自动排序的turple)
特点:元素互异,且元素会从小到大排序
#include<isostream> #include<set> using namespace std; int main(void){ set <int> a; s.insert(1); s.insert(2); s.insert(3); cout << (s.find(2) != s.end()) << endl; // s.find() 返回的是一个指针,没有找到返回的就是s.end() cout << (s.find(4) != s.end()) << endl; s.erase(1); // 删除 return 0; }
3、map (键值对)(dict)
map是键值对,它会自动将所有的键值对按照键从小到大排序
#include<isostream> #include<map> using namespace std; int main(void){ map <string,int> m; // 添加 m["hello"] = 2; // 排序是按照键的ASCII吗排序的 m["world"] = 3; // 访问 cout << "hello:" << m["hello"]; // 遍历 for(auto p = m.begin(); p != m.end();p++){ cout << p->first << ":" << p->second << endl; // p是结构指针了,p->first是结构里的键,p->second是结构里的值 } // 所有的容器都可以用.size() 方法获取长度 return 0; }
4、stack(栈)
不可遍历,先入后出
#include<isostream> #include<stack> using namespace std; int main{ stack <int> s; s.push(1); s.push(2); pop = s.pop(); cout << s.top() << endl; cout << s.size() << endl; return 0; }
5、queue(队列)
不可遍历,先入先出
#include<isostream> #include<queue> using namespace std; int main{ queue <int> s; s.push(1); s.push(2); // 访问队首 cout << s.front() << endl; // 访问队尾 cout << s.back() << endl; // pop s_pop = s.pop(); return 0; }
6、unordered_map unordered_set (无序的键值对和无序的集合)
省去了排序的过程,如果时间超时就可以用这俩
三、位运算
bitset类似一个字符数组,但是是从二进制的低位到高位排序,按照b[i]和b的输出方式截然相反
10010 b[0] = 0
#include<isostream> #include<bitset> using namespace std; int main{ bitset <5> b; // 五个二进制位,初始为0 bitset <5> b (19);// 最多为5位 bitset <8> c ("1101"); // 00001101 前面都补0 bitset <5> b(s,pos,n); // 从字符串s[pos]开始,n位长度(包括本位) cout << b << endl; for (int i = 0;i < b.size();i++){ cout << b[i] << endl; } //操作 cout << "是否有1:" << b.any() << endl; cout << "是否不存在1:" << b.none() << endl; cout << "1的个数:" << b.count() << endl; cout << "b中元素个数:" << b.size() << endl; cout << "下标为 0 的元素是不是1:" << b.test(0) << endl; b.flip(1); // 第1位取反 b.flip(); // 所有位取反 b.reset(); // 归零,同上 b.set(); // 变1,同上 cout << b << endl; // 转化为unsigned long 类型 unsigned long a = b.to_ulong(); cout << a <<endl; return 0; }
四、sort函数
# include<isostream> # include<algorithm> # include<vector> bool cmp(int x ; int y){ return x>y; // 如果返回值为真,那么x放在y前面,如果返回值为假,交换两个数 就变成了从大到小排序 ,如果是小于号就是从小到大 } int main(){ 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(),cmp); for(int i = 0 ; i < 10 ; i ++) cout << m[i] << " "; // 默认从小到大排序 return 0; }
# include<isostream> # include<algorithm> # include<vector> struct stu{ string name; int age; }; bool cmp(stu a ; stu b){ if (a.age != b.age) return a.age<b.age; else return a.name<b.name; } int main(){ stu s[3]; for(int i = 0 ; i < 3 ; i ++){ cin >> s[i].name >> s[i].age; } sort(s,s+3,cmp); for(int i = 0 ; i < 3 ; i ++){ cout << s[i],name << " " << s[i].age << endl; } return 0; }
五、cctype
char c = 'A'; cout << isalpha(c) << endl; // 字母? cout << islower(c) << endl; // 小写字母? cout << isupper(c) << endl; // 大写字母? cout << isalnum(c) << endl; // 字母or数字 cout << isspace(c) << endl; // space \n \t \r cout << tolower(c) << endl; // 转化为小写字母 cout << woupper(c) << endl; // 转化为大写字母
六、For循环
python中的类似
#include<iostream> #include<vector> #include<string> using namespace std; int main(){ int a[5] = {1}; str b = "Hello"; string s = to_string(123.1); // 将数据转化为字符串 int a = stoi("123"); // string to int double b = stod("123.4"); // string to double for ( int i : a) // 把 a 中的每个元素放进i , Python cout << i << " "; for ( auto i : b) // 把 a 中的每个元素放进i , Python cout << i << " "; return 0; }
可能出现的问题
[Dev C++ Error] range-based 'for' loops are not allowed in C++98 mode-CSDN博客
七、算法库
1、swap() 交换算法
int a = 1 , b = 2; cout << a << " " << b < endl; swap(a,b); cout << a << " " << b < endl;
2、sort() 算法
// pair 数据类型类似于一个结构体 bool cmp(pair<int,int> a,pair<int,int> b){ // 如果a的第二位小于b的第二位,则证明正确,我就不动他 if (a.second != b.second) // 第二位从小到大 return a.second < b.second; else // 第一位从大到小 return a.first > b.first } int main(){ vector<pair<int,int>> arr{{1,9},{2,9},{8,1}}; sort(arr.begin(),arr.end(),cmp) return 0 ; }
3、lower_bound() / upper_bound() (二分查找)
lower_bound() 寻找>=x的第一个元素的位置
upper_bound() 寻找>x的第一个元素的位置
#include <bits/stdc++.h> // 万能头文件 using namespace std; int main(){ vector <int> arr{0,1,1,1,8,9,9}; // 返回的是迭代器,转化成下标索引只要减去头迭代器就行了 int pos1 = lower_bound(arr.begin(),arr.end(),8) - arr.begin() ; // 返回的是大于等于8的下标索引 4 cout << pos1 << endl; int pos2 = upper_bound(arr.begin(),arr.end(),8) - arr.begin() ; // 返回的是大于8的下标索引 5 cout << pos2 << endl; // 如果找不到会显示尾迭代器的下标 if (pos == arr.size()) cout << "no" << endl; return 0; }
4、reverse() (反转)
vector <int> arr{0,1,1,1,8,9,9}; reverse(arr.begin(),arr.begin()+5); for(auto ele:arr) cout << ele << endl;
5、max() , min() 最大值最小值
cout << max({1,3,2,5,6,}) << endl;
6、unique() 去重(结合earse)
vector <int> arr{1,2,1,4,5,1,4}; sort(arr.begin(),arr.end()); // 1 1 2 4 4 4 5 // 去重以后会返回*的位置 1 2 4 5 * arr.erase(unique(arr.begin(),arr.end()),arr.end()); for (auto ele:arr) cout << ele << endl;
7、数学函数
8、gcd() lcm() 最大公因数,最小公倍数
int x = __gcd(8,12); // 4 int y = lcm(8,12); // 24 int gcd(int a,intb){ if(!b) return a; return gcd(b,a%b); } int lcm(int a,int b){ return a / gcd(a,b) * }