c/c++的常用函数和STL使用
一个超好用的c++网站:http://www.cplusplus.com/reference/string/string/erase/
一、函数头中包含的函数
1.qsort函数对数组、结构体等进行排序
#include <stdlib.h>//必须用stdlib.h,没用.h不用namespace不行
参数:1待排序数组首地址 2数组中待排序元素数量 3各元素的占用空间大小 4指向函数的指针,用于确定排序的顺序
eg:
(1)重写cmp,固定参数,变化的只是比较内容
int cmp(const void *a,const void *b){
return (*(XianDuan*)b).xd_num - (*(XianDuan*)a).xd_num;
}
此处为降序,后者大于前者时,交换位置;a-b为升序
此处(*(XianDuan*)b).xd_num进行强转时,最前面的*不能丢,因为这里用的是指针而非引用,*b表示访问其值,另外:使用void* (void指针)类型,在给指针赋值时,必须把void指针强制转换为所需要类型的指针以保证赋值的正常进行。(void*见:https://www.cnblogs.com/sybil-hxl/p/10422649.html)
(2)调用qsort函数
qsort(xd,num,sizeof(struct XianDuan),cmp);
2.使用freopen输入重定向,输入数据将从in.txt文件中读取
freopen("G:/in.txt","r",stdin);
必须使用using namespace std;否则不能使用
3.C++的char数组与string对象相互转化
char t[100];
strcpy(t, s2.c_str());//函数头<string.h>或<cstdlib>
由于strtok方法参数只能是char[]和char*必须转化
参考:https://www.cnblogs.com/fnlingnzb-learner/p/6369234.html
4.int和string转化
long int strtol (const char* str, char** endptr, int base);
str:C-string beginning with the representation of an integral number.char型数组
endptr:
Reference to an object of type char*, whose value is set by the function to the next character in str after the numerical value.
This parameter can also be a null pointer, in which case it is not used.
base:
Numerical base (radix) that determines the valid characters and their interpretation.
If this is 0, the base used is determined by the format in the sequence (see above).
int len = strtol((v1[i].substr(t1+1,t2-t1-1)).c_str(),NULL,0);//字符串转为long int类型,函数头<cstdlib>
由于vc6.0实在太难用,只支持86%的C++,stoi等函数都不能用,只能用这个凑合。java多简单。
5.char转为string
这个,找不到好办法??
6.<algorithm>中函数
(1)void reverse (BidirectionalIterator first, BidirectionalIterator last);//起始地址,中值地址
vector<int> s;
for(int i=0;i<10;i++){
s.push_back(i+1);
}
reverse(s.begin(),s.end());
7.<string.h>/<cstring>中函数
(1)char * strrev(char * str);//字符数组表示的字符串翻转
char s[]="hello";
strrev(s);
8.<cstdlib>中函数
(1)char * itoa ( int value, char * str, int base );
整型转为字符数组表示的字符串,参数:整数,字符数组,进制,返回值:字符数组
int n = 123;
char a[10];
itoa(n,a,10);
(2)int atoi (const char * str);
字符数组表示的字符串转为整型,参数:字符数组,返回值:整型数据
char a[]=“1234”;
int n = atoi(a);
二、string类的函数
1.string& erase (size_t pos = 0, size_t len = npos);
pos:
Position of the first character to be erased.
If this is greater than the string length, it throws out_of_range.
Note: The first character in str is denoted by a value of 0 (not 1).
len:
Number of characters to erase (if the string is shorter, as many characters as possible are erased).
A value of string::npos indicates all characters until the end of the string.
2.size_t pos1 = s.find(";",index);
每次使用find函数时,不要忘记第二个参数(开始查询下标),这个每一轮查询都不一样,这个是查完整个字符串的保证
size_t,本质是unsigned int,用typedef起了个名
3.s.substr(pos1,pos2-pos1);
第一个参数是起始位置,第二个参数是长度
c/c++的常用函数和STL使用的更多相关文章
- 【算法专题】工欲善其事必先利其器—— 常用函数和STL
一. 常用函数 #include <stdio.h> int getchar( void ); //读取一个字符, 一般用来去掉无用字符 char *ge ...
- 常用函数和STL
#include <bits/stdc++.h> using namespace std; #define PI acos(-1.0) int main() { printf(" ...
- C++——STL之vector, list, deque容器对比与常用函数
STL 三种顺序容器的特性对比: vector 可变数组,内存空间是连续的,容量不会进行缩减.支持高效随机存取,即支持[]和at()操作.尾部插入删除效率高,其他位置插删效率较低: list 双向链表 ...
- STL algorithm 头文件下的常用函数
algorithm 头文件下的常用函数 1. max(), min()和abs() //max(x,y)和min(x,y)分别返回x和y中的最大值和最小值,且参数必须时两个(可以是浮点数) //返回3 ...
- STL之map与pair与unordered_map常用函数详解
STL之map与pair与unordered_map常用函数详解 一.map的概述 map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称 ...
- STL之vector常用函数笔记
STL之vector常用函数笔记 学会一些常用的vector就足够去刷acm的题了 ps:for(auto x:b) cout<<x<<" ";是基于范围的 ...
- oracle常用函数及示例
学习oracle也有一段时间了,发现oracle中的函数好多,对于做后台的程序猿来说,大把大把的时间还要学习很多其他的新东西,再把这些函数也都记住是不太现实的,所以总结了一下oracle中的一些常用函 ...
- 总结js常用函数和常用技巧(持续更新)
学习和工作的过程中总结的干货,包括常用函数.常用js技巧.常用正则表达式.git笔记等.为刚接触前端的童鞋们提供一个简单的查询的途径,也以此来缅怀我的前端学习之路. PS:此文档,我会持续更新. Aj ...
- [转]SQL 常用函数及示例
原文地址:http://www.cnblogs.com/canyangfeixue/archive/2013/07/21/3203588.html --SQL 基础-->常用函数 --===== ...
随机推荐
- 7. Reverse Integer Add to List★
题目内容: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 题目分 ...
- cin.get();cin.clear();cin.sync()
先看代码: #include<iostream> using namespace std; int main(){ int c,x; cout<<"输入大小" ...
- Android四大组件之Service --- 服务的生命周期
一旦在项目的任何位置调用了Context的startService() 方法,相应的服务就会启动起来,并回调onStartCommand() 方法.如果这个服务之前还没有创建过,onCreate() ...
- 杭电ACM1007
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- Java EE设计模式(主要简单介绍工厂模式,适配器模式和模板方法模式)
Java EE设计模式分为三种类型,共23种: 创建型模式:单例模式.抽象工厂模式.建造者模式.工厂模式.原型模式. 结构型模式:适配器模式.桥接模式.装饰模式.组合模式.外观模式.享元模式.代理模式 ...
- js /Date(1550273700000)/ 格式转换
self.FormatJsonDate = function (jsonStr) { var tmp = ""; if (jsonStr == null || jsonStr == ...
- nginx申请并配置免费https
你还在让你的网站裸奔在网络上吗?在这里我们将搭建免费版HTTPS,免费的,免费的,免费的,重要的事情说三遍,申请来源为letsencrypt, 超文本传输协议HTTP协议被用于在Web浏览器和网站服务 ...
- c++学习过程
作者本人也是一名初学者,我的QQ:2522929921,可以一起交流啊! 希望广大初学者能够一起进步: 1.掌握编程思维真的很重要!!!!***. 2.不能刻意记忆语法规则. 3.在循序渐进的项目实战 ...
- 实践作业4 Web测试(小组作业分配)
经过小组内的讨论之后,我们小组的工作分配如下: 张赛军.闫昊:阶段一,软件评测: 林俊旭:阶段二,用户调研: 张嘉焰:阶段三,得出结论: 许林龙:阶段四,分析: 王奎:阶段五,每日记录,并汇总整理小组 ...
- LimeSDR 无线信号重放攻击和逆向分析
原文链接:https://mp.weixin.qq.com/s/TBYKZR3n3ADo4oDkaDUeIA