日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

std::string删除首字符

發布時間:2023/12/9 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 std::string删除首字符 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

查了下std::string的使用手冊,才知道string刪除字符只有erase成員方法,但是這個方法提供了3個重載函數,如下:

string& erase ( size_t pos = 0, size_t n = npos ); iterator erase ( iterator position ); iterator erase ( iterator first, iterator last ); Erase characters from stringErases a part of the string content, shortening the length of the string.The characters affected depend on the member function version used:string& erase ( size_t pos = 0, size_t n = npos ); Erases a sequence of n characters starting at position pos. Notice that both parameters are optional: with only one argument, the function deletes everything from position pos forwards, and with no arguments, the function deletes the entire string, like member clear. iterator erase ( iterator position ); Erases the character referred by the iterator position. Only one character is affected. iterator erase ( iterator first, iterator last ); Erases all the characters between first and last. Parameters pos Position within the string of the first character to be erased. If the position passed is past the end of the string, an out_of_range exception is thrown. n Amount of characters to be removed. It may remove less characters if the end of the string is reached before the n characters have been erased. The default value of npos indicates that all the characters until the end of the string should be erased. size_t is an unsigned integral type. position Iterator of the member type string::iterator referring to a signle character in the string to be removed. first Iterator of the member type string::iterator referring to the first character in a sequence of characters within the string to be erased. last Iterator of the member type string::iterator referring to the last character in the sequence of characters within the string to be erased. Return Value For the member that returns a string&, the function returns *this. For the remaining members, the function returns an iterator of member type string::iterator referring to the character that now occupies the position of the first character erased, or, if no such character exists, returns end().Example // string::erase #include <iostream> #include <string> using namespace std;int main () {string str ("This is an example phrase.");string::iterator it;// erase used in the same order as described above:str.erase (10,8);cout << str << endl; // "This is an phrase."it=str.begin()+9;str.erase (it);cout << str << endl; // "This is a phrase."str.erase (str.begin()+5, str.end()-7);cout << str << endl; // "This phrase."return 0; }Basic template member declaration ( basic_string<charT,traits,Allocator> ) typedef typename Allocator::size_type size_type; basic_string& erase( size_type pos = 0, size_type n = npos);iterator erase ( iterator p );iterator erase ( iterator first, iterator last );See also string::clear Clear string (public member function) string::replace Replace part of string (public member function) string::insert Insert into string (public member function) string::assign Assign content to string (public member function)

---------------------------------------------------------------------------------------------------

因此,刪除首字符可用的方法有:

1. ?str.erase(0, 1);

2. str.erase(str.begin());

3. str.erase(str.begin(), str.begin()+1) ; ? ? ? ? ? ? ?// 此法估計不會有人用了

4. 還能想到的應該是取子串方法了!



總結

以上是生活随笔為你收集整理的std::string删除首字符的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。