c++常用函数索引

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. 字典函数mapunordered_mapmultimap
size()判断字典个数
empty()判断字典是否为空

Note:

  mapunordered_map 的区别在于前者的key值是有序的,后者是无序的;

  mapmutilmap 的区别在于前者的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
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Python中,字符串是不可变的序列,为了操作字符串,我们可以使用一些常用字符串函数。下面是一些常用字符串函数: 1. len(str):返回字符串的长度,即字符的个数。 2. str.lower():将字符串中的所有字符转换为小写字母,并返回转换后的字符串。 3. str.upper():将字符串中的所有字符转换为大写字母,并返回转换后的字符串。 4. str.capitalize():将字符串的第一个字符转换为大写字母,其他字符转换为小写字母,并返回转换后的字符串。 5. str.title():将字符串中的每个单词的首字母转换为大写字母,并返回转换后的字符串。 6. str.isalpha():检查字符串是否只包含字母,并返回布尔值。 7. str.isdigit():检查字符串是否只包含数字,并返回布尔值。 8. str.isalnum():检查字符串是否只包含字母和数字,并返回布尔值。 9. str.startswith(sub):检查字符串是否以子字符串sub开头,并返回布尔值。 10. str.endswith(sub):检查字符串是否以子字符串sub结尾,并返回布尔值。 11. str.replace(old, new):将字符串中的所有旧子串替换为新子串,并返回替换后的字符串。 12. str.strip():去除字符串首尾的空格或指定的字符,并返回去除后的字符串。 13. str.split(sep):将字符串按照指定的分隔符sep分割成多个子字符串,返回一个列表。 14. str.join(iterable):将可迭代对象中的元素按照指定的字符串连接起来,并返回连接后的字符串。 15. str.find(sub):在字符串中查找子字符串sub的第一个索引,若找不到则返回-1。 这些函数可以帮助我们对字符串进行常见的操作和处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值