c++ 常用函数索引
函数 | 功能 |
---|---|
abs() 、labs() 、fabs() | 分别为整形、长整形、浮点型取对绝对值;其他类型同理加上合适的后缀或者前缀 (注意引用相应的头文件 #include <math.h>) |
clock() | 计算程序运行时间 |
isdigit() | 判断一个字符是否为数字 |
pow(x,y) | 幂乘: x y x^y xy (头文件:#include <math.h> |
system("pause") | 阻塞窗口,按任意键继续(头文件:#include <stdlib.h>) |
log() | 取对数函数(头文件:#include <math.h>) |
STL:
1. 字符串函数 | string (头文件:#include <string> ) |
---|---|
+ | 字符串拼接 |
(str,pos,n) | 字符串截取(从pos位置截取str 字符串的n个字符) |
2. 字典函数 | map 、unordered_map 、multimap |
---|---|
size() | 判断字典个数 |
empty() | 判断字典是否为空 |
Note:
map
和 unordered_map
的区别在于前者的key值是有序的,后者是无序的;
map
和 mutilmap
的区别在于前者的key值是不可以重复的,后者是可以重复的;
map
中所有元素都是pair <key,value>
,访问key (pair.first)
,访问value (pair.second)
,pair
中第一个元素为key
(键值),起到索引作用,第二个元素为value
(实值)
3. 队列函数 | queue |
---|---|
size() | 判断队列个数 |
empty() | 判断队列是否为空 |
pop() | 从对头移除第一个元素 |
push() | 从队尾添加元素 |
back() | 返回最后第一个元素 |
front | 判断第一个元素 |
Note:
Queue是一种先进先出(First In First Out,FIFO)的数据结构,它有两个出口:
-
队列容器允许从一端新增元素,从另一端移除元素;
-
队列中只有队头和队尾才可以被外界使用,因此队列不允许有遍历行为。
4. 栈 | stack |
---|---|
size() | 判断栈的大小 |
empty() | 判断栈是否为空 |
pop() | 从栈顶移除第一个元素 |
push() | 向栈顶添加元素 |
top() | 返回栈顶元素 |
Note:
stack
是一种先进后出(First In Last Out,FILO)的数据结构,它只有一个出口。
1.判断一个字符是否为数字 isdigit
int main() {
char c = '3';
if (isdigit(c)) {
cout<<c<<"是数字"<<endl;
}
else cout<<c<<"不是数字"<<endl;
return 0;
}
结果:3是数字
2.幂乘: x y x^y xy
#include <math.h>
int main() {
int x = 2 , y = 3;
int z = pow(x,y);
cout << z;
}
结果:
8
3.绝对值:x-abs()
int a = -1;
long int b =-32412341334;
float c = -1.234;
double d = -4.13;
cout << abs(a) <<endl;
cout << labs(b) <<endl;
cout << fabs(c) <<endl;
cout << fabs(d) <<endl;
结果:
1
32412341334
1.234
4.13
4.:阻塞窗口:system("pause")
void main(){
cout << "pause" << endl;
system("pause");
}
5.:计算程序运行时间:clock()
void main() {
clock_t start, stop;//clock_t为一个代表时间的数据类型
double duration;//单位:秒
start = clock();//clock()函数得到从程序开始运行到该语句运行时经过的时钟打点数
for (int i = 0; i < 123456789; i++) {
int test = i;
}
stop = clock();
duration = ((double)(stop - start)) / CLK_TCK;//CLK_TCK是一个常数,表示机器时钟每秒走的时钟打点数
cout << "duration:" << duration << endl;
}
结果:
duration:0.311
6.:对数函数:log()
int main() {
double a=9,b=10;
int c=4;
int d = log(a)/log(2); // log(a)/log(b) 以b为底,a为幂次方;
double e = log(a)/log(2);
cout << log(a) <<endl; //以e为底
cout << log(exp(a)) <<endl;
cout << log10(b) <<endl; //以10为底
//注意d、e两种数据类型定义
cout << d << endl;
cout << e << endl;
return 0;
结果:
2.19722
9
1
3
3.16993
}
STL:
1. String(字符串)
字符串拼接:+
string str1 = "我";
str1 += "爱玩游戏";
cout << "str1 = " << str1 << endl;
str1 += ':';
cout << "str1 = " << str1 << endl;
string str2 = "LOL DNF";
str1 += str2;
cout << "str1 = " << str1 << endl;
结果:
str1 = 我爱玩游戏
str1 = 我爱玩游戏:
str1 = 我爱玩游戏:LOL DNF
字符串截取:(str,pos,n)
string s = "abcdefg";
string temp(s,0,3);
cout << temp ;
结果:
abc
2. map(字典)
判断字典大小:size()
int main() {
map<int, char> m;
m.insert(pair<int, char>(1, 'a'));
m.insert(pair<int, char>(3, 'b'));
m.insert(pair<int, char>(2, 'c'));
for(auto i:m){
cout<<i.first <<":"<< i.second <<endl;
}
cout <<"数组大小为:"<< m.size() <<endl;
return 0;
}
结果:
1:a
2:c
3:b
数组大小为:3
判断字典是否为空:empty()
map<int, char> m;
m.insert(pair<int, char>(1, 'a'));
m.insert(pair<int, char>(3, 'b'));
m.insert(pair<int, char>(2, 'c'));
for(auto i:m){
cout<<i.first <<":"<< i.second <<endl;
}
cout <<"数组是否为空:"<< m.empty() <<endl;
return 0;
结果:
1:a
2:c
3:b
数组是否为空:0
3. queue(队列)
int main() {
queue<int> numQueue;
//push:添加元素
numQueue.push(1);
numQueue.push(2);
numQueue.push(3);
numQueue.push(4);
//pop:弹出元素,返回值类型为 void
numQueue.pop();
//front:输出队首元素
cout << "队首元素为:"<<numQueue.front()<<endl;
//back:输出队尾元素
cout << "队尾元素为:" << numQueue.back()<<endl;
//size:输出队列大小
cout << "队列的大小为:"<< numQueue.size() << endl;
//empty: 判断队列是否为空
cout << "队列是否为空:" << numQueue.empty() << endl;
return 0;
}
结果:
队首元素为:2
队尾元素为:4
队列的大小为:3
队列是否为空:0
4. stack(栈)
int main() {
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
cout<<"the size of s: " << s.size()<<endl;
s.pop();
cout<<"the size of s: " << s.size()<<endl;;
cout << "the top number of s: " << s.top()<<endl;;
cout << "is empty: " <<s.empty()<<endl;
return 0;
}
结果:
the size of s: 3
the size of s: 2
the top number of s: 2
is empty: 0