pos機(jī)小知識點,Cpp知識點系列

 新聞資訊2  |   2023-06-24 11:20  |  投稿人:pos機(jī)之家

網(wǎng)上有很多關(guān)于pos機(jī)小知識點,Cpp知識點系列的知識,也有很多人為大家解答關(guān)于pos機(jī)小知識點的問題,今天pos機(jī)之家(m.afbey.com)為大家整理了關(guān)于這方面的知識,讓我們一起來看下吧!

本文目錄一覽:

1、pos機(jī)小知識點

pos機(jī)小知識點

前言

記錄一些對字符串的理解。

接下來我所說的都是依賴于頭文件<string>的。

理論什么是字符串

字符串實際上是使用 null 字符 '\\0' 終止的一維字符數(shù)組。因此,一個以 null 結(jié)尾的字符串,包含了組成字符串的字符。

string與cstring有什么區(qū)別

<string>是C++標(biāo)準(zhǔn)庫頭文件,包含了擬容器class std::string的聲明(不過class string事實上只是basic_string<char>typedef),用于字符串操作。

<cstring>是C標(biāo)準(zhǔn)庫頭文件<string.h>的C++標(biāo)準(zhǔn)庫版本,包含了C風(fēng)格字符串(NULL即'\\0'結(jié)尾字符串)相關(guān)的一些類型和函數(shù)的聲明,例如strcmp、strchr、strstr等。

兩者最大區(qū)別在于:

string是新標(biāo)準(zhǔn),定義了namespace std;cstring定義中包含的是string.hstring中可以進(jìn)行+ = += >等運算,而cstring中不能進(jìn)行相關(guān)運算。什么是string類型的迭代器

聲明一個string類中特有的類型size_type變量,名稱為position

string::size_type position;

迭代器是一個變量,相當(dāng)于容器和操縱容器的算法之間的中介 , 實在理解困難認(rèn)為是個特別的int下標(biāo)也可以。

迭代器可以指向容器中的某個元素,通過迭代器就可以讀寫它指向的元素。從這一點上看,迭代器和指針類似。

//從類名獲取該對象string::npos;//從對象獲取該對象string tempstr;tempstr.npos;

這個npos是個特別的迭代器對象,可以理解成為string類型中特別的NULL,如果查找失敗或者遍歷完畢之后迭代器的值就是這個.

操作

首先引入頭文件,并且在命名空間中使用。

#include<string>using namespace std;Strings常用方法

方法名作用Operators操作符,可以用 ==, >, <, >=, <=, and !=比較字符串. 可以用 + 或者 += 操作符連接兩個字符串, 并且可以用[]獲取特定的字符append()在字符串的末尾添加文本(就是+=)compare()比較兩個字符串empty()如果字符串為空,返回真erase()刪除字符insert()插入字符length()返回字符串的長度Replace()替換字符size()返回字符串中字符的數(shù)量(結(jié)果等價于length)substr()返回某個子字符串swap()交換兩個字符串的內(nèi)容

如果可以使用自帶的方法去實現(xiàn)的話自然是比較好的,但是如果只能夠針對部分字符進(jìn)行復(fù)雜變化的話,就需要轉(zhuǎn)化成為char*類型的數(shù)組

構(gòu)造法

平常更多是用等號直接賦值,如果要求字符串中間有結(jié)尾符可以這樣聲明。

void stringTestConstruct() {    string str =string("12345 \\0 54321", 13);//不等價于str="12345 \\0 54321";    cout << str<<endl;    string str1 = "12345 \\0 54321";    cout << str1;}

void stringTestAppend() { string str = "Hello"; str.append(3, '!');//等價于str+="!!!" ; cout << str;}

初學(xué)時,沒考慮太多,后來才發(fā)現(xiàn)在添加'\\0'字符時有奇效。

void stringTestAppend2() {    string str="abc";    str.append("\\0 def",5);//等價于 str =string("abc\\0 def", 8);    cout << str;}

image-20221108135309114

compare

compare()比較時逐字符比較的,一旦能比較出結(jié)果,就不再比較了。

雖然有多個重載,但是只需要理解,在this字符串與傳入的比較字符串str一部分(至于是哪一部分用起點和長度來評定)之間進(jìn)行的比較即可。

 int compare( const basic_string &str ); int compare( const char *str ); int compare( size_type index, size_type length, const basic_string &str ); int compare( size_type index, size_type length, const basic_string &str, size_type index2,size_type length2 ); int compare( size_type index, size_type length, const char *str, size_type length2 );

在str之前的參數(shù)是修飾this,在str之后的參數(shù)是修飾str的

返回值情況小于零this < str(自己弱小就認(rèn)''負(fù)'')零this == str大于零this > str(自己強(qiáng)大就囂''正'')

void stringTestCompare() {    string s1 = "abandon";    string s2 = "about";    int b = s1.compare(s2);//直接比較,s1小于s2,故返回-1    int c = s1.compare(2, 4, s2);//s1下標(biāo)為2的字符a開始的4個字符ando和s2進(jìn)行比較。ando大于s2故返回1    cout << c << endl;    int d = s1.compare(2, 4, s2, 1, 3);    cout << d << endl;//s1下標(biāo)為2的字符a開始的4個字符ando和s2下標(biāo)為1的字符b開始的3個字符bou比較。前者小,故返回-1。    string s3 = "abc";    string s4 = "abc";    int e = s3.compare(s4);//相等返回0    cout << e << endl;}erase

也有參數(shù)為迭代器的函數(shù),在下面介紹。

basic_string &erase( size_type index = 0, size_type num = npos );刪除從index索引開始的num個字符, 返回刪減后的字符串。其中index默認(rèn)從0開始,num默認(rèn)是最大值。

void stringTestErase() {    string s("1234567890123456789012345");//總共25個字符    cout << s << endl;    cout << s.erase(10, 6) << endl;//從下標(biāo)第10的字符開始刪除,一共刪除6個。    cout << s.erase(10) << endl;//刪除下標(biāo)為10的字符及之后的 ==保留下標(biāo)小于10的字符    cout << s.erase();//清空字符串}

image-20201126200020627

insert

basic_string &insert( size_type index, const basic_string &str );basic_string &insert( size_type index, const char *str );basic_string &insert( size_type index1, const basic_string &str, size_type index2, size_type num);basic_string &insert( size_type index, const char *str, size_type num );basic_string &insert( size_type index, size_type num, char ch );在字符串的位置index插入字符串str;在字符串的位置index插入字符串str的子串(從index2開始,長num個字符);在字符串的位置index插入字符串str的num個字符;在字符串的位置index插入num個字符ch的拷貝字符串。

其實理解起來就是,在本字符串的index位置,插入?yún)?shù)中的字符串的一部分(至于是哪一部分

用起點和長度來評定)。

void stringTestInsert() {    string str = "abc";    str.insert(1, "123");    /*在下標(biāo)為1的位置,插入字符串123全部*/    cout << str << endl;    str = "abc";    str.insert(1, "123", 1, 1);    /*在下標(biāo)為1的位置,插入字符串123的一部分,并且從下標(biāo)1開始,長度為1的一段*/    cout << str << endl;    str = "abc";    str.insert(1, "123", 2);    /*在下標(biāo)為1的位置,插入字符串123的一部分,并且從下標(biāo)0開始,長度為2的一段*/    cout << str << endl;    str = "abc";    str.insert(1, 2, 'z');    /*在下標(biāo)為1的位置,插入2個字符z*/    cout << str;}

image-20221108141757159

length

其實不用糾結(jié)在獲取長度的時候是用size還是length,至少在GCC6的情況下,返回的都是同一個字段_M_string_length。

void stringTestLength() {    //其實后面都是返回的同一個字段_M_string_length。    string s("1234567890123456789012345");//總共25個字符    cout << s.size()  << endl;//輸出25    cout << s.length()  << endl;//輸出25}replace

basic_string &replace( size_type index, size_type num, const basic_string &str );basic_string &replace( size_type index1, size_type num1, const basic_string &str, size_type index2,size_type num2 );basic_string &replace( size_type index, size_type num, const char *str );basic_string &replace( size_type index, size_type num1, const char *str, size_type num2);basic_string &replace( size_type index, size_type num1, size_type num2, char ch );

其實理解起來就是,在將本字符串的index位置長度為num的字符串,替換成參數(shù)中的字符串的一部分(至于是哪一部分用起點和長度來評定)。

一般用起來的時候,更多是和find函數(shù)一起,實現(xiàn)查找替換的作用。然而find函數(shù)是涉及到了迭代器,所以這里只是簡單示例一下。有興趣可以直接下翻。

void stringTestReplace() {    string str="1234567890";    str.replace(0,9,"ABC");    /*把下標(biāo)從0開始、長度為2的一段字符串,替換為ABC*/    cout << str<<endl;    str="1234567890";    str.replace(str.find('8'),3,"ABC");    /*把從字符8開始、長度為2的一段字符串,替換為ABC*/    cout << str;}substr

  basic_string substr( size_type index, size_type num = npos ); 

截取本字符串從index開始之后的num個字符,沒錯就是跟上面的刪除差不多!

特別的,用程序形象的看一眼效果:

void stringTestSubstr() {    string s("123456789012345678901234567890");    cout << s << endl;    printf("0s\", s.substr(21).c_str());    cout << s.erase(21) << endl;}

image-20201126222643653

swap

 void swap( basic_string &str );

這個函數(shù)的作用主要就是將兩個字符串的全部元素進(jìn)行交換。

void stringTestSwap() {    string first( "This comes first" );    string second( "And this is second" );    first.swap( second );    cout << first << endl;    cout << second << endl;}

image-20201126222955616

如果只是針對部分元素,可以用insertreplace進(jìn)行操作。

轉(zhuǎn)char*

方法名作用c_str()將字符串以C字符數(shù)組的形式返回copy(*str, num, index )將內(nèi)容復(fù)制為一個字符數(shù)組data()返回內(nèi)容的字符數(shù)組形式

c_str( )

 const char *c_str();

返回一個const臨時指針,指向以\\0結(jié)尾的字符數(shù)組,應(yīng)該使用strcpy()函數(shù)等來操作。

void stringTestC_str() {    //數(shù)組    char c[20];    string s="1234";    strcpy(c,s.c_str());//注意strcpy函數(shù)是在cstring頭文件中的    cout<<c;    //指針    string str = "hello";    const char* p1 = str.c_str();//加const    char * p2=(char*)str.c_str();//或者是強(qiáng)制轉(zhuǎn)換}data( )

const char *data();

照常來說是字符串內(nèi)容外,**不附加結(jié)束字符'\\0'**。

有資料顯示,兩者是應(yīng)該有區(qū)別的c_str()有結(jié)束符,而data()沒有結(jié)束符。

image-20201126172130028

因為我這個編譯環(huán)境的問題,在dev的環(huán)境中兩個方法,其實都是返回的_M_data()方法而已的。

copy(*str,num,index )

拷貝自己的num個字符到str中(從索引index開始)。返回值是拷貝的字符個數(shù)

size_type copy( char *str, size_type num, size_type index );

由于還需要手動加結(jié)束符。

void stringTestCopy() {    string str = "hmmm";    char p[10];    str.copy(p, 3, 0);    /*把從0開始、長度為3的字符串,復(fù)制到字符數(shù)組p之中*/    p[3] = '\\0';//注意手動加結(jié)束符?。?!    cout << p;}迭代器

方法名作用begin()返回一個迭代器,指向第一個字符end()返回一個迭代器,指向字符串的末尾。(最后一個字符的下一個位置)erase()刪除字符find()在字符串中查找字符find_first_of()查找第一個與value中的某值相等的字符find_first_not_of()查找第一個與value中的所有值都不相等的字符find_last_of()查找最后一個與value中的某值相等的字符find_last_not_of()查找最后一個與value中的所有值都不相等的字符get_allocator()返回配置器insert()插入字符rbegin()返回一個逆向迭代器,指向最后一個字符rend()返回一個逆向迭代器,指向第一個元素的前一個位置replace()替換字符rfind()查找最后一個與value相等的字符(逆向查找)

find

把str的全部字符當(dāng)作整體進(jìn)行查找.

 size_type find( const basic_string &str, size_type index ); size_type find( const char *str, size_type index ); size_type find( const char *str, size_type index, size_type length ); size_type find( char ch, size_type index );返回str在字符串中第一次出現(xiàn)的位置(從index開始查找)。如果沒找到則返回string::npos,返回str在字符串中第一次出現(xiàn)的位置(從index開始查找,長度為length)。如果沒找到就返回string::npos,返回字符ch在字符串中第一次出現(xiàn)的位置(從index開始查找)。如果沒找到就返回string::npos

代碼:

void stringiteratorTestFind() {    string s("qwe123qwee123qweqwe1");    string flag = "123";    string::size_type position = 0;    int i = 1;    while ((position = s.find(flag, position)) != string::npos) {        cout << "position  " << i << " : " << position << endl;        position++;        i++;    }}

image-20201126230709115

find_first_of

 size_type find_first_of( const basic_string &str, size_type index = 0 ); size_type find_first_of( const char *str, size_type index = 0 ); size_type find_first_of( const char *str, size_type index, size_type num ); size_type find_first_of( char ch, size_type index = 0 );

跟上面的參數(shù)相似,重點理解find的即可。

特別注意

find_first_of 函數(shù)和find函數(shù)最大的區(qū)別就是,如果在str1中查找str2時,如果str1中含有str2中的任何字符,就會查找成功,而find則只有全部相同才會查找成功.

比如:

void stringIteratorTestFind_first_of () {    string s("qwe123qwee123qweqwe1");    string flag="123";    string::size_type position=0;    int i=1;    while((position=s.find_first_of(flag,position))!=string::npos) {        cout<<"第"<<i<<"次匹配 : "<<position<<endl;        position++;        i++;    }}

與上面的例子區(qū)別只是更換了一個函數(shù)而已,但是結(jié)果完全不同。

image-20221108150441682

至于其他的not,last的變名函數(shù)則是顧名思義即可。

rfind

反向查找子字符串

 size_type rfind( const basic_string &str, size_type index ); size_type rfind( const char *str, size_type index ); size_type rfind( const char *str, size_type index, size_type num ); size_type rfind( char ch, size_type index );

當(dāng)正向查找與反向查找得到的位置不相同說明子串不唯一。

erase

 iterator erase( iterator pos ); iterator erase( iterator start, iterator end );刪除pos指向的字符, 返回指向下一個字符的迭代器,刪除從start到end的所有字符, 返回一個迭代器,指向被刪除的最后一個字符的下一個位置insert

 iterator insert( iterator i, const char &ch ); void insert( iterator i, size_type num, const char &ch ); void insert( iterator i, iterator start, iterator end );在迭代器i表示的位置前面插入一個字符ch,在迭代器i表示的位置前面插入num個字符ch的拷貝,在迭代器i表示的位置前面插入一段字符,從start開始,以end結(jié)束.replace

 basic_string &replace( iterator start, iterator end, const basic_string &str ); basic_string &replace( iterator start, iterator end, const char *str ); basic_string &replace( iterator start, iterator end, const char *str, size_type num ); basic_string &replace( iterator start, iterator end, size_type num, char ch );用str中的字符替換本字符串中的字符,迭代器start和end指示范圍用str中的num個字符替換本字符串中的內(nèi)容,迭代器start和end指示范圍,用num個ch字符替換本字符串中的內(nèi)容,迭代器start和end指示范圍.感謝

參考書籍《C++語言程序設(shè)計(第4版)》(鄭莉,董淵)

C++string的compare()比較函數(shù)

感謝現(xiàn)在的好奇,為了能成為更好的自己。

以上就是關(guān)于pos機(jī)小知識點,Cpp知識點系列的知識,后面我們會繼續(xù)為大家整理關(guān)于pos機(jī)小知識點的知識,希望能夠幫助到大家!

轉(zhuǎn)發(fā)請帶上網(wǎng)址:http://m.afbey.com/newsone/73010.html

你可能會喜歡:

版權(quán)聲明:本文內(nèi)容由互聯(lián)網(wǎng)用戶自發(fā)貢獻(xiàn),該文觀點僅代表作者本人。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如發(fā)現(xiàn)本站有涉嫌抄襲侵權(quán)/違法違規(guī)的內(nèi)容, 請發(fā)送郵件至 babsan@163.com 舉報,一經(jīng)查實,本站將立刻刪除。