⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 常用函数_李博.txt

📁 解决算法题目时常用的标准库函数。有详细的使用方法说明哦。
💻 TXT
字号:
常用函数
李博
一、ctype.h
Int isalnum( int c ) 测试c是否为数字或英文字母,当传入参数介于A~Z、a~z、0~9,返回非零值,否则返回0。
int isalpha( int c )  是否为英文字母,A~Z、a~z,返回值非零。
int isdigit( int c )  是否为数字,0~9返回值非零。
int isupper( int c )  是否为大写字母,A~Z返回值非零。
int islower( int c )  是否为小写字母,a~z返回值非零。
int isgraph( int c )  ASCII码为33~126时,返回值非零。
int isprint( int c)   ASCII码为32~126时,返回值非零。
int isspace ( int c )  c为空格时,返回值非零。
int toupper( int c )  将小写字母转为大写字母,如果成功,返回转换后的值,否则返回原值。
Int tolower( int c)  将大写字母转为小写字母,如果成功,返回转换后的值,否则返回原值。
二、math.h
double sin( double x )
double cos( double x )
double tan( double x )
double asin( double x )  结果介于-π2~π2之间。
double acos( double x )  结果介于0~π。
double atan( double x )  结果介于-π2~π2之间。
double atan2( double y, double x) 计算极角。结果介于-π~π之间。
double log( double x ) 以e为底。
double log10( double x ) 以10为底。
double pow( double x, double y ) 计算x^y。
double exp( double x ) 计算e^x。
double fabs( double x )
double ceil( double x ) 求出大于x的最小整数。
double floor( double x ) 求出小于x的最大整数。
double sqr( double x )
三、stdio.h
int sprintf(char buffer,const char format[,argument]…) 把结果输入到字符串中去。例:double a;char st[100];sprintf(st,”%.3lf”,a);
int sscanf(const char  buffer,const char format [,argument]…); 从字符串中读入数据。例:sscanf(st,”%ld %ld”,&a,&b);
int fgets(char st,int len,FILE f);  st是字符数组,len是长度(会读入len-1个字符),f是文件变量(如果是标准输入用stdin)。注意:会读入’n’。
四、stdlib.h
int atoi(const char string)  将字符串转换为整数。
long atol(const char string)  将字符串转换为长整型整数。
double atof(const char string)  将字符串转换为浮点型实数。
char itoa(int value, char string, int radix) 将value转换为以radix进制数并存到字符串。注:radix的范围为2~36。
void qsort( void base, size_t num, size_t width, int (compare )(const void elem1,const void elem2));
base 为要sort的起始数组
Num 为数组中元素的个数
width 为每个元素的byte数
compare 为用来比较的函数
elem1 与 elem2 为待比较元素。
例:对存有单词的word[max_word][max_len]按字典序排序。
    qsort(word,max_word,sizeof(char)max_len,com);
    int com(const void a,const void b)
    {
       return strcmp((char)a,(char) b);
}
srand()和rand() 用随机数格式:
   #includetime.h
   #includestdlib.h
   
   int main() {
     srand(time(0));主过程首部初始化
     … …
     rand()%size 得到0~size-1范围内的随机数,rand()得到0~65535的随机数
   }
五、string.h
int strlen(const char a);返回字符串的长度。
int strcmp(char a,char b); 按字典序比较两个字符串的大小。
int strcpy(char a,char b); 将b拷到a。
int strncpy(char a,char b,int n); 只考b的前n个字符。
int strcat(char a,char b);  将b加到a后面。
int strncat(char a,char b,int n);
char strstr(const char a,const char b);在a中寻找第一次出现b的位置,如果找到,返回在a中的指针,否则返回NULL。
char strupr(char a); 将a中所有字母转为大写。
char strlwr(char a); 将a中所有字母转为小写。
     char strset(char a,char x);将a中所有元素替换为x。
     char strnset(char a,char x,int n); 将a中前n个元素替换为x。

六、iostream.h
1.设置场宽 cout.width(int size);
2.对于空格填充 cout.fill(char x);
3.保留小数 cout.precision(int n);
            cout.setf(iosfixed);
4.读入一行 cin.getline(char st,int len); st中保存前len-1个字符。
5.从字符串中读数据
     #includestrstream.h
     istrstream strin(const char string);
     strin……; 
     会产生warning
     #includesstream.h
     istringstream strin(const char string);
     strin……;
     不会产生warning
七、STL
1.map
   
   #includemap
   using namespace std;
   
   typedef maptype1,type2 State;
   typedef Stateiterator iter;

   State prob;
   iter it; 用it指向的单元iter-first,type1类型;iter-second,type2类型。
   
   首先清空map:prob.clear();
   插入:prob[type1]=type2;
   查找:it=prob.find(type1); 
         if (it==prob.end()) 没找到
         else 找到了
   删除:prob.erase(const type1& k)删除关键值为k的。
         prob.erase(iter it)删除it指向的元素。
   如果type1是结构或类:
   struct Statep{
      数据成员;
      bool operator (const Statep& a) const {
        比较数据成员与a.数据成员的大小,小于返回true
        例:return xa.x;
      }
}
2.sort

   #includealgorithm
   using namespace std;

   bool com(const type& a,const type& b) {
      比较a、b,如果ab返回true
}

sort(a,a+len,com); a:待排序数组首指针 len:待排序数组长度
3.unique 必须先将a数组排好序
   
   #includealgorithm
   using namespace std;

   bool same(const type& a,const type& b) {
      如果a=b返回true
   }
    
   newlen=unique(a,a+len,same)-a; newlen,unique后数组的长度

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -