版權(quán)歸原作者所有,如有侵權(quán),請聯(lián)系我們

[科普中國]-string.h

科學(xué)百科
原創(chuàng)
科學(xué)百科為用戶提供權(quán)威科普內(nèi)容,打造知識科普陣地
收藏

C語言標(biāo)準(zhǔn)庫中一個常用的頭文件,在使用到字符數(shù)組時需要使用。string .h 頭文件定義了一個變量類型、一個宏和各種操作字符數(shù)組的函數(shù)。

簡單介紹C語言里面關(guān)于字符數(shù)組的函數(shù)定義的頭文件,常用函數(shù)有strlen、strcmp、strcpy等等,更詳細(xì)的可以到include文件夾里面查看該文件。

版本內(nèi)容string.h在c語言和c++語言中都被廣泛的使用,但是具體情況不是很一樣。由于傳統(tǒng)的C++脫胎于C,所以傳統(tǒng)C++中于C語言中對本詞條的用法差不多,經(jīng)過美國標(biāo)準(zhǔn)化組織修改標(biāo)準(zhǔn)化后的標(biāo)準(zhǔn)C++中,定義則是大不相同。

傳統(tǒng) C++其中包含的引用頭文件如下:

#include //設(shè)定插入點

#include //字符處理

#include //定義錯誤碼

#include //浮點數(shù)處理

#include //文件輸入/輸出

#include //參數(shù)化輸入/輸出

#include //數(shù)據(jù)流輸入/輸出

#include //定義各種數(shù)據(jù)類型最值常量

#include //定義本地化函數(shù)

#include //定義數(shù)學(xué)函數(shù)

#include //定義輸入/輸出函數(shù)

#include //定義雜項函數(shù)及內(nèi)存分配函數(shù)

#include //字符串處理

#include //基于數(shù)組的輸入/輸出

#include //定義關(guān)于時間的函數(shù)

#include //寬字符處理及輸入/輸出

#include //寬字符分類

標(biāo)準(zhǔn) C++其中包括的頭文件如下(同上的不再注釋)

#include //STL 通用算法

#include //STL 位集容器

#include

#include

#include

#include

#include //復(fù)數(shù)類

#include

#include

#include

#include

#include //STL雙端隊列容器

#include //異常處理類

#include

#include //STL 定義運算函數(shù)(代替運算符)

#include

#include //STL 線性列表容器

#include //STL 映射容器

#include

#include //基本輸入/輸出支持

#include //輸入/輸出系統(tǒng)使用的前置聲明

#include

#include //基本輸入流

#include //基本輸出流

#include //STL 隊列容器

#include //STL 集合容器

#include //基于字符串的流

#include //STL堆棧容器

#include //標(biāo)準(zhǔn)異常類

#include //底層輸入/輸出支持

#include //字符串類

#include //STL 通用模板類

#include //STL動態(tài)數(shù)組容器

#include

#include

using namespace std;

C99 增加#include //復(fù)數(shù)處理

#include //浮點環(huán)境

#include //整數(shù)格式轉(zhuǎn)換

#include //布爾環(huán)境

#include //整型環(huán)境

#include //通用類型數(shù)學(xué)宏

疑問解答c++中 string與string.h 的作用和區(qū)別

答:一般在C++的庫中,對于一個舊的,也就是帶“.h”擴(kuò)展名的庫文件(比如iostream.h),在新標(biāo)準(zhǔn)后的標(biāo)準(zhǔn)庫中都有一個不帶“.h”擴(kuò)展名的與之相對應(yīng),區(qū)別除了后者的好多改進(jìn)之外,還有一點就是后者的東東都塞進(jìn)了“std”名字空間中。

但唯獨string特別。

問題在于C++要兼容C的標(biāo)準(zhǔn)庫,而C的標(biāo)準(zhǔn)庫里碰巧也已經(jīng)有一個名字叫做“string.h”的頭文件,包含一些常用的C字符串處理函數(shù)。

這個頭文件跟C++的string類半點關(guān)系也沒有,所以 并非 的“升級版本”,他們是毫無關(guān)系的兩個頭文件。

c++ 中包括哪些函數(shù)?

答:常用函數(shù)如下:

strlen求字符串長度

strcmp比較2個字符串是否一樣

strcat字符串連接操作

strcpy字符串拷貝操作

strncat字符串連接操作(前n個字符)

strncpy字符串拷貝操作(前n個字符)

strchr查詢字串

strstr 查詢子串

函數(shù)用法下面為string.h文件中函數(shù)的詳細(xì)用法,附加實例:

strcpy函數(shù)名:strcpy

功 能:拷貝一個字符串到另一個

用 法:char *strcpy(char *destin, char *source);

程序例:

#include#includeint main(void){char string[10];char*str1="abcdefghi";strcpy(string,str1);printf("%s\n",string);return 0;}strncpy函數(shù)名:strncpy

原型:char *strncpy(char *dest, char *src,size_tn);

功能:將字符串src中最多n個字符復(fù)制到字符數(shù)組dest中(它并不像strcpy一樣遇到NULL才停止復(fù)制,而是等湊夠n個字符才開始復(fù)制),返回指向dest的指針。

strcat

函數(shù)名:strcat

功 能:字符串拼接函數(shù)

用 法:char *strcat(char *destin, char *source);

程序例:

#include #includevoid main() { char destination[25]; char*blank="",*c="C++",*Borland="Borland"; strcpy(destination,Borland); strcat(destination,blank); strcat(destination,c); printf("%s\n",destination); }strchr函數(shù)名:strchr

功 能: 在一個串中查找給定字符的第一個匹配之處

用 法:char *strchr(char *str, char c);

程序例:

#include#includeint main(void){char string[15];char*ptr,c='r';strcpy(string,"Thisisastring");ptr=strchr(string,c);if(ptr)printf("Thecharacter%cisatposition:%d\n",c,ptr-string);elseprintf("Thecharacterwasnotfound\n");return 0;}strcmp函數(shù)名:strcmp

功 能:串比較

用 法:int strcmp(char *str1, char *str2);

看Asic碼,str1>str2,返回值 > 0;兩串相等,返回0

程序例:

#include#includeint main(void){char*buf1="aaa",*buf2="bbb",*buf3="ccc";int ptr;ptr=strcmp(buf2,buf1);if(ptr>0)printf("buffer2isgreaterthanbuffer1\n");elseprintf("buffer2islessthanbuffer1\n");ptr=strcmp(buf2,buf3);if(ptr>0)printf("buffer2isgreaterthanbuffer3\n");elseprintf("buffer2islessthanbuffer3\n");return 0;}strnicmp函數(shù)名:strnicmp

功 能:將一個串中的一部分與另一個串比較, 不管大小寫

用 法:intstrnicmp(char *str1, char *str2, unsigned maxlen);

程序例:

#include#includeint main(void){char*buf1="BBB",*buf2="bbb";int ptr;ptr=strnicmp(buf2,buf1);if(ptr>0)printf("buffer2isgreaterthanbuffer1\n");if(ptr0)printf("buffer2isgreaterthanbuffer1\n");if(ptr0)printf("buffer2 is greater than buffer1\n");if(ptr0)printf("buffer2 is greater than buffer1\n");elseprintf("buffer2 is less than buffer1\n");ptr=strncmp(buf2,buf3,3);if(ptr>0)printf("buffer2isgreaterthanbuffer3\n");elseprintf("buffer2islessthanbuffer3\n");return 0;}strncpy函數(shù)名:strncpy

功 能:串拷貝

用 法:char *strncpy(char *destin, char *source, int maxlen);

程序例:

#include#includeint main(void){char string[10];char*str1="abcdefghi";strncpy(string,str1,3);string[3]='\0';printf("%s\n",string);return 0;}strnicmp函數(shù)名:strnicmp

功 能:不注重大小寫地比較兩個串

用 法:int strnicmp(char *str1, char *str2, unsigned maxlen);

程序例:

#include#includeint main(void){char*buf1="BBBccc",*buf2="bbbccc";int ptr;ptr=strnicmp(buf2,buf1,3);if(ptr>0)printf("buffer2isgreaterthanbuffer1\n");if(ptr