第2張圖片代碼存在兩個問題

創(chuàng)新互聯(lián)建站長期為上千客戶提供的網(wǎng)站建設服務,團隊從業(yè)經(jīng)驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為大同企業(yè)提供專業(yè)的成都網(wǎng)站制作、網(wǎng)站設計、外貿(mào)網(wǎng)站建設,大同網(wǎng)站改版等技術服務。擁有十余年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
1、強行加了node head頭,再插入數(shù)據(jù)只能從head后面插,這其實是沒必要的
2、node head頭未經(jīng)初始化,無法保證其p_next是NULL還是垃圾數(shù)據(jù)(可能是垃圾數(shù)據(jù)),那么看下while循環(huán),如果head.p_next里的數(shù)據(jù)是垃圾的話,導至第一次while判斷句的temp-p_next不為NULL但也不是能使用的指針,然后進入while內(nèi)部將不能使用的指針賦值給temp,再進行while第二次判斷,由于temp是無效指針,運行temp-p_next必然崩潰
在C語言中,任何時候你都可以用其它類型的指針來代替void指針(在C++中同樣可以),或者用void指針來代替其它類型的指針(在C++中需要進行強制轉(zhuǎn)換),并且不需要進行強制轉(zhuǎn)換。例如,你可以把char *類型的指針傳遞給需要void指針的函數(shù)。
什么時候使用void指針?
當進行純粹的內(nèi)存操作時,或者傳遞一個指向未定類型的指針時,可以使用void指針。void指針也常常用作函數(shù)指針。
有些C代碼只進行純粹的內(nèi)存操作。在較早版本的C中,這一點是通過字符指針(char *)實現(xiàn)的,但是這容易產(chǎn)生混淆,因為人們不容易判斷一個字符指針究竟是指向一個字符串,還是指向一個字符數(shù)組,或者僅僅是指向內(nèi)存中的某個地址。
要想使用標準C++中string類,必須要包含
#include string// 注意是string,不是string.h,帶.h的是C語言中的頭文件
using std::string;
using std::wstring;
或
using namespace std;
下面你就可以使用string/wstring了,它們兩分別對應著char和wchar_t。
string和wstring的用法是一樣的,以下只用string作介紹:
string類的構造函數(shù):
string(const char *s); //用c字符串s初始化
string(int n,char c); //用n個字符c初始化
此外,string類還支持默認構造函數(shù)和復制構造函數(shù),如string s1;string s2="hello";都是正確的寫法。當構造的string太長而無法表達時會拋出length_error異常 ;
string類的字符操作:
const char operator[](int n)const;
const char at(int n)const;
char operator[](int n);
char at(int n);
operator[]和at()均返回當前字符串中第n個字符的位置,但at函數(shù)提供范圍檢查,當越界時會拋出out_of_range異常,下標運算符[]不提供檢查訪問。
const char *data()const;//返回一個非null終止的c字符數(shù)組
const char *c_str()const;//返回一個以null終止的c字符串
int copy(char *s, int n, int pos = 0) const;//把當前串中以pos開始的n個字符拷貝到以s為起始位置的字符數(shù)組中,返回實際拷貝的數(shù)目
string的特性描述:
int capacity()const; //返回當前容量(即string中不必增加內(nèi)存即可存放的元素個數(shù))
int max_size()const; //返回string對象中可存放的最大字符串的長度
int size()const; //返回當前字符串的大小
int length()const; //返回當前字符串的長度
bool empty()const; //當前字符串是否為空
void resize(int len,char c);//把字符串當前大小置為len,并用字符c填充不足的部分
string類的輸入輸出操作:
string類重載運算符operator用于輸入,同樣重載運算符operator用于輸出操作。
函數(shù)getline(istream in,string s);用于從輸入流in中讀取字符串到s中,以換行符'\n'分開。
string的賦值:
string operator=(const string s);//把字符串s賦給當前字符串
string assign(const char *s);//用c類型字符串s賦值
string assign(const char *s,int n);//用c字符串s開始的n個字符賦值
string assign(const string s);//把字符串s賦給當前字符串
string assign(int n,char c);//用n個字符c賦值給當前字符串
string assign(const string s,int start,int n);//把字符串s中從start開始的n個字符賦給當前字符串
string assign(const_iterator first,const_itertor last);//把first和last迭代器之間的部分賦給字符串
string的連接:
string operator+=(const string s);//把字符串s連接到當前字符串的結尾
string append(const char *s); //把c類型字符串s連接到當前字符串結尾
string append(const char *s,int n);//把c類型字符串s的前n個字符連接到當前字符串結尾
string append(const string s); //同operator+=()
string append(const string s,int pos,int n);//把字符串s中從pos開始的n個字符連接到當前字符串的結尾
string append(int n,char c); //在當前字符串結尾添加n個字符c
string append(const_iterator first,const_iterator last);//把迭代器first和last之間的部分連接到當前字符串的結尾
string的比較:
bool operator==(const string s1,const string s2)const;//比較兩個字符串是否相等
運算符"","","=","=","!="均被重載用于字符串的比較;
int compare(const string s) const;//比較當前字符串和s的大小
int compare(int pos, int n,const string s)const;//比較當前字符串從pos開始的n個字符組成的字符串與s的大小
int compare(int pos, int n,const string s,int pos2,int n2)const;//比較當前字符串從pos開始的n個字符組成的字符串與s中
//pos2開始的n2個字符組成的字符串的大小
int compare(const char *s) const;
int compare(int pos, int n,const char *s) const;
int compare(int pos, int n,const char *s, int pos2) const;
compare函數(shù)在時返回1,時返回-1,==時返回0
string的子串:
string substr(int pos = 0,int n = npos) const;//返回pos開始的n個字符組成的字符串
string的交換:
void swap(string s2); //交換當前字符串與s2的值
string類的查找函數(shù):
int find(char c, int pos = 0) const;//從pos開始查找字符c在當前字符串的位置
int find(const char *s, int pos = 0) const;//從pos開始查找字符串s在當前串中的位置
int find(const char *s, int pos, int n) const;//從pos開始查找字符串s中前n個字符在當前串中的位置
int find(const string s, int pos = 0) const;//從pos開始查找字符串s在當前串中的位置
//查找成功時返回所在位置,失敗返回string::npos的值
int rfind(char c, int pos = npos) const;//從pos開始從后向前查找字符c在當前串中的位置
int rfind(const char *s, int pos = npos) const;
int rfind(const char *s, int pos, int n = npos) const;
int rfind(const string s,int pos = npos) const;
//從pos開始從后向前查找字符串s中前n個字符組成的字符串在當前串中的位置,成功返回所在位置,失敗時返回string::npos的值
int find_first_of(char c, int pos = 0) const;//從pos開始查找字符c第一次出現(xiàn)的位置
int find_first_of(const char *s, int pos = 0) const;
int find_first_of(const char *s, int pos, int n) const;
int find_first_of(const string s,int pos = 0) const;
//從pos開始查找當前串中第一個在s的前n個字符組成的數(shù)組里的字符的位置。查找失敗返回string::npos
int find_first_not_of(char c, int pos = 0) const;
int find_first_not_of(const char *s, int pos = 0) const;
int find_first_not_of(const char *s, int pos,int n) const;
int find_first_not_of(const string s,int pos = 0) const;
//從當前串中查找第一個不在串s中的字符出現(xiàn)的位置,失敗返回string::npos
int find_last_of(char c, int pos = npos) const;
int find_last_of(const char *s, int pos = npos) const;
int find_last_of(const char *s, int pos, int n = npos) const;
int find_last_of(const string s,int pos = npos) const;
int find_last_not_of(char c, int pos = npos) const;
int find_last_not_of(const char *s, int pos = npos) const;
int find_last_not_of(const char *s, int pos, int n) const;
int find_last_not_of(const string s,int pos = npos) const;
//find_last_of和find_last_not_of與find_first_of和find_first_not_of相似,只不過是從后向前查找
文章名稱:append函數(shù)c語言 append函數(shù)r語言
轉(zhuǎn)載注明:http://chinadenli.net/article20/hihsjo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、面包屑導航、商城網(wǎng)站、關鍵詞優(yōu)化、手機網(wǎng)站建設、電子商務
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)