目录
-
- 说明
- 1.名称空间using namespace std的解释
- 2.cin和cout输入输出
- 3.关于c++的头文件
- 4.C++的变量声明
- 5.C++特有的bool变量
- 6.C++特有的const定义常量
- 7.C++里超好用的string类
- 8.C++的结构体struct和C语言结构体的区别
- 9.C++STL之动态数组vector的使用
- 10.C++STL之集合set的使用
- 11.C++STL之映射map的使用
- 12.C++STL之stack的使用
- 13.C++STL之队列queue的使用
- 14.C++STL之unordered_map和unordered_set的使用
- 15.C++的位运算bitset
- 16.C++中的sort函数
- 17.C++中使用sort自定义cmp函数
- 18.关于cctype头文件里的一些函数
- 19.关于C++11的解释
- 20.C++11里面很好用的auto声明
- 21.C++11特性中的to_string
- 22.C++11特性中的stoi、stod
- 23.如何在Dev-Cpp中使用C++11中的函数
- 24.Cpp中保留小数的实现
说明
本教程大部分参考柳婼的教程《从放弃C语言到使用C++刷算法的简明教程v4.7》
1.名称空间using namespace std的解释
#include
int main () {
int n; std::cin>>n; std::cout<<"hello ,xuyuanzhi"<<n+1<< std::endl; return 0; }
2.cin和cout输入输出
cout << "hello, ⼩可爱~\n"; cout << n << endl;
cin和cout虽然使⽤起来更⽅便,但是输⼊输出的效率不如scanf和printf快,如果发现有题目超时了,可以换成scanf和printf再试一下
3.关于c++的头文件
#include
// 相当于C语⾔⾥⾯的#include
#include
// 相当于C语⾔⾥⾯的#include
#include
// 相当于C语⾔⾥⾯的#include
#include
// 相当于C语⾔⾥⾯的#include
4.C++的变量声明
C语⾔的变量声明⼀般都在函数的开头,但是C++在⾸次使⽤变量之前声明即可~(当然也可以都放在函数的开头),⽽且⼀般C语⾔⾥⾯会在for循环的外⾯定义i变量,但是C++⾥⾯可以在for循环内部定义~⽽且在for循环⾥⾯定义的局部变量,在循环外⾯就失效啦(就是脱离这个局部作⽤域就会查⽆此变量的意思),所以⼀个main函数⾥⾯可以定义好多次局部变量i,再也不⽤担⼼写的循环太多变量名i、j、k不够⽤啦~
#include
using namespace std; int main(){
int n; cin>>n; cout<<"hello,xuyuanzhi"<<n+1<<endl; int m; cin>>m; for(int i=0;i<n;i++){
//这个i只在for循环里面有用,出了这个for循环就相当于不见了 cout<<i; } cout<<endl; for(int i=0;i<m;i++){
cout<<i+2; } return 0; }
5.C++特有的bool变量
bool flag = true; bool flag2 = -2; // flag2为true bool flag3 = 0; // flag3为false
6.C++特有的const定义常量
const int a = ;
7.C++里超好用的string类
string s="hello c++";//赋值字符串 string s1=s; string s2=s+s1; //字符串拼接直接用+号就可以 string s3; cin>>s3;//读入字符串 cout<<s<<endl<<s1<<endl<<s2<<endl<<s3<<endl;//输出字符串
string s;//定义一个空字符串s getline(cin,s);//读取一行的字符串,包括空格 cout<<s<<endl; cout<<s.length();//输出字符串s的长度 return 0;
string中还有个很常⽤的函数叫做substr,作⽤是截取某个字符串中的⼦串,⽤法有两种形式:
string s2 = s.substr(4); // 表示从下标4开始⼀直到结束 string s3 = s.substr(5, 3); // 表示从下标5开始,3个字符
8.C++的结构体struct和C语言结构体的区别
struct stu {
int grade; float score; }; struct stu arr1[10]; // C语⾔⾥⾯需要写struct stu arr2[10];// C++⾥⾯不⽤写
void func(int &a) {
// 传⼊的是n的引⽤,相当于直接对n进⾏了操作,只不过在func函数 中换了个名字叫a a = 99; } int main() {
int n = 0; func(n); // n由0变成了99 }
void func(int a) {
// 传⼊的是0这个值,并不会改变main函数中n的值 a = 99; } int main() {
int n = 0; func(n);// 并不会改变n的值,n还是0 }
9.C++STL之动态数组vector的使用
#include
#include
using namespace std; int main(){
vector<int> v1;//定义一个vector v1,定义的时候没有分配大小 cout<<v1.size();//输出vector v1的大小,此处应该为0 return 0; }
vector<int> v(10); // 直接定义⻓度为10的int数组,默认这10个元素值都为0 // 或者 vector<int> v1; v1.resize(8); //先定义⼀个vector变量v1,然后将⻓度resize为8,默认这8个元素都是0 // 在定义的时候就可以对vector变量进⾏初始化 vector<int> v3(100, 9);// 把100⻓度的数组中所有的值都初始化为9 // 访问的时候像数组⼀样直接⽤[]下标访问即可~(也可以⽤迭代器访问,下⾯会讲~) v[1] = 2; cout << v[0];
不管是vector、stack、queue、map还是set都有很多好⽤的⽅法,这些⽅法都可以在http://www.cplusplus.com/官⽅⽹站中直接查询官⽅⽂档,上⾯有⽅法的讲解和代码示例~官⽅⽂档是刷题时候必不可少的好伙伴~⽐如进⼊官⽹搜索 vector ,就会出现vector拥有的所有⽅法,点进去⼀个⽅法就能看到这个⽅法的详细解释和代码示例~当然我们平时写算法⽤不到那么多⽅法啦,只有⼏个是常⽤的~以下是⼀些常⽤的vector⽅法:
#include
#include
using namespace std; int main() {
vector<int> a; // 定义的时候不指定vector的?? cout << a.size() << endl; // 这个时候size是0 for (int i = 0; i < 10; i++) {
a.push_back(i); // 在vector a的末尾添加?个元素i } cout << a.size() << endl; // 此时会发现a的size变成了10 vector<int> b(15); // 定义的时候指定vector的??,默认b??元素都是0 cout << b.size() << endl; for (int i = 0; i < b.size(); i++) {
b[i] = 15; } for (int i = 0; i < b.size(); i++) {
cout << b[i] << " "; } cout << endl; vector<int> c(20, 2); // 定义的时候指定vector的??并把所有的元素赋?个指定的值 for (int i = 0; i < c.size(); i++) {
cout << c[i] << " "; } cout << endl; for (auto it = c.begin(); it != c.end(); it++) {
// 使?迭代器的?式访问vector cout << *it << " "; } return 0; }
10.C++STL之集合set的使用
set是集合,⼀个set⾥⾯的各元素是各不相同的,⽽且set会按照元素进⾏从⼩到⼤排序~以下是set的常⽤⽤法:
#include
#include
using namespace std; int main(){
set<int> s;//定义一个空集合s s.insert(1);//向集合里面插入一个1; cout<<*(s.begin())<<endl;//输出集合s的第一个元素(前面的星号表示要对指针取值) for(int i=0;i<6;i++){
s.insert(i);//向集合s里面插入i } for(auto it=s.begin();it!=s.end();it++){
//用迭代器遍历集合s里面的每一个元素 cout<<*it<<" "; } cout<<endl<<(s.find(2)!=s.end())<<endl;//查找集合s中的值,如果结果等于s.end()表示未找到(因为s.end()表示s的最后?个元素的下?个元素所在的位置) cout << (s.find(10) != s.end()) << endl; // s.find(10) != s.end()表示能找到10这个元素 s.erase(1); // 删除集合s中的1这个元素 cout << (s.find(1) != s.end()) << endl; // 这时候元素1就应该找不到啦~ return 0; }
11.C++STL之映射map的使用
map是键值对,⽐如⼀个⼈名对应⼀个学号,就可以定义⼀个字符串string类型的⼈名为“键”,学号int类型为“值”,如 map
m; 当然键、值也可以是其它变量类型~map会⾃动将所有的键值对按照键从⼩到⼤排序,以下是map中常⽤的⽅法:
#include
#include #include
using namespace std; int main(){
map<string,int>m;//定义一个空的map m,键是string类型的,值是int类型的 m["hello"] = 2; // 将key为"hello", value为2的键值对(key-value)存入map中 cout << m["hello"] << endl; // 访问map中key为"hello"的value, 如果key不存在,则返回0 cout << m["world"] << endl; m["world"] = 3; // 将"world"键对应的值修改为3 m[","] = 1; // 设立一组键值对,键为"," 值为1 //用迭代器遍历,输出map中所有的元素,键?it->first获取,值?it->second获取 for (auto it = m.begin(); it != m.end(); it++) {
cout << it->first << " " << it->second << endl; } // 访问map的第一个元素,输出它的键和值 cout << m.begin()->first << " " << m.begin()->second << endl; // 访问map的最后一个元素,输出它的键和值 cout << m.rbegin()->first << " " << m.rbegin()->second << endl; // 输出map的元素个数 cout << m.size() << endl; return 0; return 0; }
12.C++STL之stack的使用
栈stack在头⽂件 #include 中,是数据结构⾥⾯栈~以下是常⽤⽤法:
#include
#include
using namespace std; int main() {
stack<int> s; // 定义⼀个空栈s for (int i = 0; i < 6; i++) {
s.push(i); // 将元素i压⼊栈s中 } cout << s.top() << endl; // 访问s的栈顶元素 cout << s.size() << endl; // 输出s的元素个数 s.pop(); // 移除栈顶元素 return 0; }
13.C++STL之队列queue的使用
队列queue在头⽂件 #include 中,是数据结构⾯的队列~以下是常⽤⽤法:
#include
#include
using namespace std; int main() {
queue<int> q; // 定义⼀个空队列q for (int i = 0; i < 6; i++) {
q.push(i); // 将i的值依次压⼊队列q中 } cout << q.front() << " " << q.back() << endl; // 访问队列的队⾸元素和队 尾元素 cout << q.size() << endl; // 输出队列的元素个数 q.pop(); // 移除队列的队⾸元素 return 0; }
14.C++STL之unordered_map和unordered_set的使用
unordered_map在头⽂件 #include 中,unordered_set在头⽂件
#include 中~
unordered_map和map(或者unordered_set和set)的区别是,map会按照键值对的键key进⾏排序
(set⾥⾯会按照集合中的元素⼤⼩进⾏排序,从⼩到⼤顺序),⽽unordered_map(或者
unordered_set)省去了这个排序的过程,如果偶尔刷题时候⽤map或者set超时了,可以考虑⽤
unordered_map(或者unordered_set)缩短代码运⾏时间、提⾼代码效率~⾄于⽤法和map、set
是⼀样的~
15.C++的位运算bitset
bitset⽤来处理⼆进制位⾮常⽅便。头⽂件是 #include ,bitset可能在PAT、蓝桥OJ中不常⽤,但是在LeetCode OJ中经常⽤到~⽽且知道bitset能够简化⼀些操作,可能⼀些复杂的问题能够直接⽤bitset就很轻易地解决~以下是⼀些常⽤⽤法:
#include
#include
using namespace std; int main() {
bitset<5> b("11"); //5表示5个⼆进位 // 初始化⽅式: // bitset<5> b; 都为0 // bitset<5> b(u); u为unsigned int,如果u = 1,则被初始化为10000 // bitset<5> b(s); s为字符串,如"1101" -> "10110" // bitset<5> b(s, pos, n); 从字符串的s[pos]开始,n位⻓度 for(int i = 0; i < 5; i++) cout << b[i]; cout << endl << b.any(); //b中是否存在1的⼆进制位 cout << endl << b.none(); //b中不存在1吗? cout << endl << b.count(); //b中1的⼆进制位的个数 cout << endl << b.size(); //b中⼆进制位的个数 cout << endl << b.test(2); //测试下标为2处是否⼆进制位为1 b.set(4); //把b的下标为4处置1 b.reset(); //所有位归零 b.reset(3); //b的下标3处归零 b.flip(); //b的所有⼆进制位逐位取反 unsigned long a = b.to_ulong(); //b转换为unsigned long类型 return 0; }
16.C++中的sort函数
sort函数在头⽂件 #include ⾥⾯,主要是对个数组进⾏排序(int arr[]数组或者vector数组都⾏),vector是容器,要⽤v.begin()和v.end()表示头尾;⽽int arr[]⽤arr表示数组的⾸地址,arr+n表示尾部~
#include
#include
#include
using namespace std; bool cmp(int a, int b) {
// cmp函数返回的值是bool类型 return a > b; // 从⼤到⼩排列 } int main() {
vector<int> v(10); for (int i = 0; i < 10; i++) {
cin >> v[i]; } sort(v.begin(), v.end());// 因为这⾥没有传⼊参数cmp,所以按照默认,v从⼩到 ⼤排列 int arr[10]; for (int i = 0; i < 10; i++) {
cin >> arr[i]; } sort(arr, arr + 10, cmp); // arr从⼤到⼩排列,因为cmp函数排序规则设置了从⼤到⼩ return 0; }
注意:sort函数的cmp必须按照规定来写,即必须只是 > 或者 <,⽐如:return a > b;或者return a < b;
⽽不能是 <= 或者 >= ,(实际上等于号加了也是毫⽆意义,sort是不稳定的排序),否则可能会出现
段错误~
17.C++中使用sort自定义cmp函数
sort默认是从⼩到⼤排列的,也可以指定第三个参数cmp函数,然后⾃⼰定义⼀个cmp函数指定排序规则~cmp最好⽤的还是在结构体中,尤其是很多排序的题⽬~⽐如⼀个学⽣结构体stu有学号和成绩两个变量,要求如果成绩不同就按照成绩从⼤到⼩排列,如果成绩相同就按照学号从⼩到⼤排列,那么就可以写⼀个cmp数组实现这个看上去有点复杂的排序过程:
#include
using namespace std; struct stu {
// 定义⼀个结构体stu,number表示学号,score表示分数 int number; int score; } bool cmp(stu a, stu b) {
// cmp函数,返回值是bool,传⼊的参数类型应该是结构体 stu类型 if (a.score != b.score) // 如果学⽣分数不同,就按照分数从⼤到⼩排列 return a.score > b.score; else // 如果学⽣分数相同,就按照学号从⼩到⼤排列 return a.number < b.number; } bool cmp(stu a, stu b) {
return a.score != b.score ? a.score > b.score : a.number < b.number; }
18.关于cctype头文件里的一些函数
刚刚在头⽂件那⼀段中也提到,cctype本质上是C语⾔标准函数库中的头⽂件 #include
,其实并不属于C++新特性的范畴,在刷PAT⼀些字符串逻辑题的时候也经常⽤到,但是很多⼈似乎不了解这个头⽂件中的函数,所以在这⾥单独提⼀下~可能平时我们判断⼀个字符是否是字⺟,可能会写:
char c; cin >> c; if (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z') {
cout << "c is alpha"; }
但是在cctype中已经定义好了判断这些字符应该所属的范围,直接引⼊这个头⽂件并且使⽤⾥⾯的函数判断即可,⽆需⾃⼰⼿写(⾃⼰⼿写有时候可能写错或者漏写~)
#include
#include
using namespace std; int main() {
char c; cin >> c; if (isalpha(c)) {
cout << "c is alpha"; } return 0; }
char c = 'A'; char t = tolower(c); // 将c字符转化为⼩写字符赋值给t,如果c本身就是⼩写字符也没有 关系~ cout << t; // 此处t为'a'
19.关于C++11的解释
C++11是2011年官⽅为C++语⾔带来的新语法新标准,C++11为C++语⾔带来了很多好⽤的新特性,⽐如auto、to_string()函数、stoi、stof、unordered_map、unordered_set之类的~现在⼤多数OJ都是⽀持C++11语法的,有些编译器在使⽤的时候需要进⾏⼀些设置才能使⽤C++11中的语法,否则可能会导致编译器上编译不通过⽆法运⾏,⽐如我曾经写过⼀篇博客《如何在Dev-Cpp中使⽤C++11中的函数》(在本教程末尾)这个是针对DEV-cpp编译器的,其他的编译器如果发现不⽀持也可以百度搜索⼀下让编译器⽀持C++11的⽅法~总之C++11的语法在OJ⾥⾯是可以使⽤的~⽽且很多语法很好⽤~以下讲解⼀些C++11⾥⾯常⽤的新特性~
20.C++11里面很好用的auto声明
auto是C++11⾥⾯的新特性,可以让编译器根据初始值类型直接推断变量的类型。⽐如这样:
auto x = 100; // x是int变量 auto y = 1.5; // y是double变量
当然这个在算法⾥⾯最主要的⽤处不是这个,⽽是在STL中使⽤迭代器的时候,auto可以代替⼀⼤⻓串的迭代器类型声明:
// 本来set的迭代器遍历要这样写: for(set<int>::iterator it = s.begin(); it != s.end(); it++) {
cout << *it << " "; } // 现在可以直接替换成这样的写法: for(auto it = s.begin(); it != s.end(); it++) {
cout << *it << " "; }
21.C++11特性中的to_string
to_string的头⽂件是 #include ,to_string最常⽤的就是把⼀个int型变量或者⼀个数字转化为string类型的变量,当然也可以转double、float等类型的变量,这在很多PAT字符串处理的题⽬中很有⽤处,以下是示例代码:
#include
#include
using namespace std; int main() {
string s1 = to_string(123); // 将123这个数字转成字符串 cout << s1 << endl; string s2 = to_string(4.5); // 将4.5这个数字转成字符串 cout << s2 << endl; cout << s1 + s2 << endl; // 将s1和s2两个字符串拼接起来并输出 printf("%s\n", (s1 + s2).c_str()); // 如果想⽤printf输出string,得加⼀ 个.c_str() return 0; }
22.C++11特性中的stoi、stod
使⽤stoi、stod可以将字符串string转化为对应的int型、double型变量,这在字符串处理的很多问题中很有帮助~以下是示例代码和⾮法输⼊的处理⽅法:
#include
#include
using namespace std; int main() {
string str = "123"; int a = stoi(str); cout << a; str = "123.44"; double b = stod(str); cout << b; return 0; }
23.如何在Dev-Cpp中使用C++11中的函数
如果想要在Dev-Cpp⾥⾯使⽤C++11特性的函数,⽐如刷算法中常⽤的stoi、to_string、unordered_map、unordered_set、auto这些,需要在设置⾥⾯让dev⽀持c++11~需要这样做~在⼯具-编译选项-编译器-编译时加⼊这个命令“-std=c++11”即可~
24.Cpp中保留小数的实现
头文件
#include
格式
cout<<fixed<<setprecision(3)<<num<<endl;
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/175668.html原文链接:https://javaforall.net
