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 基础-->常用函数 --===== ...
随机推荐
- 【转载】curl 模拟 GET\POST 请求,curl查看响应头 以及 curl post 上传文件
补充说明:curl查看响应头 curl -I "http://www.baidu.com"HTTP/1.1 200 OK #HTTP协议 HTTP 返回码Server: Tengi ...
- IntelliJ IDEA入门系列
1.Java Web之Helloworld配置 2.Java Web之Maven搭建Helloworld 3.Java Web之Spring MVC简单管理系统
- python字符串编码理解(转载)
(转载)字符编码和python使用encode,decode转换utf-8, gbk, gb2312 (http://www.cnblogs.com/jxzheng/p/5186490.html) A ...
- 0x80070522:客户端没有所需的特权的解决方法(win7,win10通过)
往C盘上粘贴文件的时候提示错误0x80070522:客户端没有所需的特权,解决方法如下: 一般情况下,我们思考的方向肯定是往用户权限方向的,但增加的用户是最高权限的管理员还是不可以.. 暂时还没有 ...
- Oracle学习DayOne(SQL初步)
一.DML.DDL.DCL SQL语句分为以下三种类型: DML: Data Manipulation Language 数据操纵语言DDL: Data Definition Language 数据定 ...
- 自学python之路(day1)
1. 下载和安装Pycharm文本编辑器,解释器Python3 2. 了解python2和python3的不同 如输出中文时,python2需要在代码前加 # -*- encoding:utf-8 - ...
- ansible 变量详解
定义变量的方法 1. 主机变量,在hosts文件中设置变量, [atlanta] host1 http_port= maxRequestsPerChild= host2 http_port= maxR ...
- VBA在WORD应用中如何将格式应用于选定内容
下列示例使用 Selection 属性将字符和段落格式应用于选定文本.使用 Font 属性获得字体格式的属性和方法,使用 ParagraphFormat 属性获得段落格式的属性和方法. Sub For ...
- 高性能JavaScript(1)
---------------------------------------------------------------------------------------------------- ...
- java 对同一个文件进行读写操作
同一个文件是不可以进行同时的读写的,因为我们写入文件会覆盖原文件的,如果这样,对于同一文件来来说,文件发生覆盖,无法进行下次读取 当然,对于两个不同的文件,可以一边读一边写的操作 题目:一个文本中存储 ...