1、预处理器指示符
当嵌套包含文件时可以用如下代码避免重复编译。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #ifndef BOOKSTORE_H #define BOOKSTORE_H
#endif
|
一些预定义名字:
2、二维数组指针
二维数组 a[2][3]
指针表示
方法一:
int (*p)[3]=a
或int (*p)[3]=&a[0]
访问 a[i][j]
等价于 p[i][j]
或 *(*(p+i)+j)
方法二:
int *p=a[0]=&a[0][0]=*a
访问 a[i][j]
等价于 *(p+i*C+j)
或 *(*(p+i)+j)
3、pair 用法
类模板:template<class T1,class T2> struct pair
参数:T1是第一个值的数据类型,T2是第二个值的数据类型。
功能:pair将一对值(T1和T2)组合成一个值,这一对值可以具有不同的数据类型(T1和T2), 两个值可以分别用pair的两个公有函数first和second访问。
1 2 3 4 5 6 7
| pair<T1,T2> p1; pair<T1,T2> p2(v1,v2);//创建一个pair对象,元素的值分别为v1,v2。 make_pair(v1,v2); p1<p2; p1==p2; p1.first; p1.second;
|
可以预定义宏
1 2 3 4 5
| typedef pair<T1,T2> P;
P p1(v1,v2); P p = P(v1,v2); P p = make_pair(v1,v2);
|
可以通过tie
获取pair元素值,std::tie
位于tuple
头文件中
1 2 3 4 5 6
| #include <tuple> typedef pair<T1,T2> P; P p(v1,v2); T1 v3; T2 v4; tie(v3,v4)=p;
|
4、队列(queue)
std::queue
位于 queue
头文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include<queue> queue<T> Q;
T e=Q.front();
T e=Q.back();
bool b=Q.empty();
int s=Q.size();
Q.push(e);
Q.pop();
Q.emplace(e);
|
5、字符串(string)
std::string
位于string
头文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| #include<string>
str = "This is a string.";
char ch = str[0];
int l = str.length();
str += "A new string.";
b = (str <= "Another string."); b = (str >= "Another string."); b = (str == "Another string.");
int cpm = str.compare("Another string.");
string subs = str.substr(pos,count);
str.copy(s,count,pos);
pos = str.find(ch,pos);
cin>>string; cout<<string;
getline(cin,string);
int int1 = stoi(string); long long1 = stol(string); long long long_long1 = stoll(string); string str1 = to_string(int); string str1 = to_string(double);
|
6、映射(map)
std::map
在map
头文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #include<map> map<int,int> mp;
mp.clear();
mp.size();
mp[key]=value;
for(auto it = mp.begin();it!=mp.end();++it) { int key = it->first; int calue = it->second; }
auto it = mp.find(key); if (it!=mp.end()) else
|
7、cout用法
1、cout保留小数
1 2 3 4
| #include<iomanip>
cout<<fixed<<setprecision(2)<<b<<endl; cout<<setprecision(2)<<b<<endl;
|
8、自定义结构体(struct)
1、结构体变量构造函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| struct Student { char *name; int age; char *sex; Student(char *name,int age,char *sex) :name(name),age(age),sex(sex){} void print_info() { cout<<"name: "<<name<<endl; cout<<"age: "<<age<<endl; cout<<"sex: "<<sex<<endl; } };
Student stu = Student("张三",18,"男"); stu.print_info();
|
9、STL中的二分查找
1、binary_search
查找某个元素是否出现过。
1 2 3 4 5 6 7 8
| #include <algorithm>
int a[]={1,2,3,4,5,6,7,8};
bool f = binary_search(a,a+8,6);
vector<int> b={1,2,3,4,5,6,7,8}; bool f = binary_search(b.begin(),b.end(),4);
|
2、lower_bound
查找第一个大于等于某个元素的位置。如果没找到返回结束位置。
1 2 3 4 5 6 7 8
| #include <algorithm>
int a[]={1,2,3,4,5,6,7,8};
int pos = lower_bound(a,a+8,7)-a;
vector<int> b={1,2,3,4,5,6,7,8}; auto it= lower_bound(b.begin(),b.end(),5);
|
3、upper_bound
查找第一个大于某个元素的位置。如果没找到返回结束位置。
1 2 3 4 5 6 7 8
| #include <algorithm>
int a[]={1,2,3,4,5,6,7,8};
int pos = upper_bound(a,a+8,11)-a;
vector<int> b={1,2,3,4,5,6,7,8}; auto it= upper_bound(b.begin(),b.end(),5);
|
10、读取一整行
1 2 3
| #include<cstdio> char str[10]; scanf("%[^\n]",str);
|