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 基础-->常用函数 --===== ...
随机推荐
- Python 正则表达式相关问题
这几天学习python,写正则表达式相关代码如下: import re print(re.search(r'(?<=<(\w+)>).*(?=<\/\1>)'," ...
- js初学
1.学习一门编程语言需要记住知识点: 1.关键字. 2.标识符. 3.注释. 4.运算符. 5.常量和变量 . 6.语句. 7.函数 ...
- 配置jQuery环境
获取最新版jQuery 一.jq库类型说明 jQuery.js(开发版):完整无压缩,主要用于学习.开发和测试 jQuery.min.js(生产版):主要用于产品开发和项目 二.在页面引入 <s ...
- 【转载】IL指令集
转载自:http://www.cnblogs.com/knowledgesea/p/5461040.html 名称 说明 Add 将两个值相加并将结果推送到计算堆栈上. Add.Ovf 将两个整数相加 ...
- WINDOWS SERVER 2016 IE使用FLASH PLAYER的方法
Windows Server 2016出于安全的考虑,默认禁用了Flash Player.把Windows Server 2016作为日常操作系统的童鞋会发现,IE里完全没有Flash Player这 ...
- 使用spring框架,用xml方式进行bean装配出现“The fully qualified name of the bean's class, except if it serves...”
使用spring框架,用xml方式进行bean装配出现“The fully qualified name of the bean's class, except if it serves...”. 原 ...
- input搜索框:根据历史记录自动填充后,去除默认黄色背景
如果是纯色背景,直接通过box-shadow覆盖即可: input:-webkit-autofill { color: #333!important; -webkit-text-fill-color: ...
- matlab 小波工具箱
wavemenu --- >wavelet ---->wavelet packet1-D Matlab小波工具箱的使用1 转载▼ http://blog.sina.com.cn/s/blo ...
- python并发_进程_multiprocessing
多进程基础, 主要是用了 multiprocessing模块 : 在一个python进程中开启子进程,start方法和并发效果. import time from multiprocessing im ...
- Maven Gradle
场景:随着项目越来越规范,对构建工具的要求越来越高,我们从Maven转到了Gradle. 转自:http://www.infoq.com/cn/news/2011/04/xxb-maven-6-gra ...