C++字符數(shù)組、字符數(shù)組指針和string類
C++中字符串的表示方式有很多種,根據(jù)自己目前掌握的有三種:
- 字符數(shù)組;
- 字符數(shù)組指針;
- 標準庫
string
類;
上面幾種方式各有優(yōu)點和缺點,按照自己的觀點,如果處理的字符串的任務比較簡單,則使用前兩種方法所占用內存小,因而較為實用;如果需要進行字符串拼接和比較等功能,則使用string類比較合適,因為字符數(shù)組不含有處理函數(shù)。
1、字符數(shù)組和字符數(shù)組的指針
定義字符數(shù)組即使用char類型,字符數(shù)組的聲明和初始化例子如下:
char duckWords[5] = "Eat";
給字符數(shù)組定義指針的語法如下,字符數(shù)組名依舊表示首地址:
char *pointerWords = duckWords;
字符數(shù)組和字符數(shù)組指針的使用方式,和普通數(shù)組與普通數(shù)組指針的使用方式完全相同:
printf("%c \n", duckWords[5]); printf("%c \n", *pointerWords);
需要注意的是,字符串的以“\0”結尾,所以對于“Say it”這個字符串實際上含有7個字符,因為表示字符串結尾標志的”\0“是自動添加的。此外,字符串創(chuàng)建含有多種語法,比較重要的一點是初始化時可以不指定數(shù)組長度:
char duckName[6]={'D','a','v', 'i', 'd'}; char duckName[6]="David"; char duckName[] = "David";
2、標準庫string類
從面向對象的角度看,string類才是更符合字符串操作的。必須注意,string
是一個類而不是基本數(shù)據(jù)類型。
string類的功能主要體現(xiàn)在下面三個發(fā)面:
- 含有多個構造函數(shù),所以能采用多種方式進行初始化;
- 包含眾多的重載操作符;
- 多種用于字符串處理的成員函數(shù);
下面的第一個例子采用“+”運算符進行字符串拼接:
string duckName = "David"; string duckAge = " 12"; ?string duckDescribe = duckName + duckAge; ?/// 需要使用string.c_str()才能輸出完整字符串 ?printf("%s \n", duckDescribe.c_str());
第二個例子是使用string的成員函數(shù)length()進行字符串長度統(tǒng)計:
string duckName = "David"; printf("%d \n", duckName.length());
當然,string類重載的操作符和含有的成員函數(shù)還有很多,但是使用方法都是類似的,不屬于語法范疇,所以不做具體介紹。
3、補充
3.1C++自帶string類的常用方法
?#include<iostream> ? ?#include<string> ? ?using namespace std; ? ? ? ?int main() ? ?{ ? ? ? ?string str1 = "hello"; ? ? ? ?string* str2 = new string("hello"); ? ? ? string str3 = "world"; ? ? ? ?//獲取字符串長度 ? ? ?int length = str1.length(); ? ? ?cout << "調用str.length()函數(shù)獲取字符串長度:" << length << endl; ? ? ?cout << endl; ?? ?? ? ? ?//字符串連接 ? ? ? string str4 = str1 + str3; ? ? ? cout << "字符串連接結果:" << str4 << endl; ? ? ? cout << endl; ?? ?? ? ? ? //字符串比較 ? ? ? if (str1 < str3) ? ? ? ? ? cout << "字符串比較:" << "str1<str2" << endl; ? ? ? cout << endl; ? ? ? //獲取字符串的第一個字符 ? ? ? string::const_iterator it = str1.begin(); ? ? ? cout << *it << endl; ? ? ? cout << endl; ?? ?? ? ? ? //獲取字符串的最后一個字符 ? ? ?it = str1.end();//end是指向最后一個字符后面的元素,而且不能輸出,所以cout << *it << endl;這樣輸出會報錯 ? ? ? it--; ? ? ? cout << *it << endl; ? ? ? cout << endl; ? ?? ? ? //倒置串 ? ? ?reverse(str1.begin(), str1.end()); ? ?cout << "倒置串:" << str1 << endl; ? ? cout << endl; ? ? ? //字符串轉字符數(shù)組 ? ? ?//不推薦的用法,但是需要了解 ? ? string a = "abc123"; ? ? ?const char *b;//這里必須為const char *,不能用char *,不然下一句會報錯 ? ? ?b = a.c_str(); ? ? ?cout << "a:" << a << endl; ? ? ?cout << "b:" << b << endl; ? ? ?a = "asd456"; ? ? ?cout << "a:" << a << endl; ? ? ?cout << "b:" << b << endl; ? ? ? //推薦用法 ? ? ? string c = "abc123"; ? ? ?char *d = new char[20]; ? ? ? strcpy(d, c.c_str());//因為這里沒有直接賦值,所以指針類型可以不用const char * ? ? cout << "c:" << c << endl; ? ? ? cout << "d:" << d << endl; ? ? ?c = "asd456"; ? ? ? cout << "c:" << c << endl; ? ? cout << "d:" << d << endl; ? ? cout << endl; ?? ? ? ? //查找串 ? ? ?//find-從指定位置起向后查找,直到串尾 ? ? ? string st1("babbabab"); ? ? cout << st1.find('a') << endl;//1,默認從位置0(即第1個字符)開始查找 ? ? ?cout << st1.find('a', 2) << endl;//4 ? 在st1中,從位置2(b,包括位置2)開始,查找a,返回首次匹配的位置 ? ? ?cout << (st1.find('c', 0) == -1) << endl;//1? ? ? ? cout << (st1.find('c', 0) == 4294967295) << endl;//1 ? 兩句均輸出1,原因是計算機中-1和4294967295都表示為32個1(二進制) ? ? ?string st2("aabcbcabcbabcc"); ? ? ?str1 = "abc"; ? ? ?cout << st2.find(str1, 2) << endl;//6,從st2的位置2(b)開始匹配,返回第一次成功匹配時匹配的串(abc)的首字符在st2中的位置,失敗返回-1 ? ? ? cout << st2.find("abcdefg", 2, 3) << endl;//6 ? 取abcdefg得前3個字符(abc)參與匹配,相當于st2.find("abc", 2) ? ? ? ? //rfind-從指定位置起向前查找,直到串首 ? ? ?cout << st1.rfind('a', 7) << endl;//6 ?? ? ? ?//find_first_of-在源串中從位置pos起往后查找,只要在源串中遇到一個字符,該字符與目標串中任意一個字符相同,就停止查找,返回該字符在源串中的位置;若匹配失敗,返回-1 ? ? ? string str6("bcgjhikl"); ? ? ?string str7("kghlj"); ? ? ?cout << str6.find_first_of(str7, 0) << endl;//2,從str1的第0個字符b開始找,g與str2中的g匹配,停止查找,返回g在str1中的位置2 ? ? ? ? ? ?//find_last_of-與find_first_of函數(shù)相似,只不過查找順序是從指定位置向前 ? ? ?string str("abcdecg"); ? ? ?cout << str.find_last_of("hjlywkcipn", 6) << endl;//5,從str的位置6(g)開始向前找,g不匹配,再找c,c匹配,停止查找,返回c在str中的位置5 ? ? ?//find_first_not_of-在源串中從位置pos開始往后查找,只要在源串遇到一個字符,與目標串中的任意字符都不相同,就停止查找,返回該字符在源串中的位置;若遍歷完整個源串,都找不到滿足條件的字符,則返回-1 ? ? ?cout << str.find_first_not_of("kiajbvehfgmlc", 0) << endl;//3 ? 從源串str的位置0(a)開始查找,目標串中有a,匹配,..,找d,目標串中沒有d(不匹配),停止查找,返回d在str中的位置3 ?? ? ? ? //find_last_not_of-與find_first_not_of相似,只不過查找順序是從指定位置向前 ? ? ?cout << str.find_last_not_of("kiajbvehfgmlc", 6) << endl;//3 ? ? ? ?system("pause"); ? ? ?return 0; ? ?}
運行結果:
到此這篇關于C++字符數(shù)組、字符數(shù)組指針和string類的文章就介紹到這了,更多相關C++字符數(shù)組和string類內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!