1 #include <string> 2 #include <iostream> 3 #include <cstring> 4 #include <algorithm> 5 6 using namespace std; 7 8 int to_lower(int c) 9 { 10 if (isupper(c)) 11 { 12 return (c+32); 13 } 14 15 return c; 16 } 17 18 int to_upper(int c) 19 { 20 if(isupper(c)) 21 { 22 return (c-32); 23 } 24 return c; 25 } 26 27 int main() 28 { 29 // 初始化string 30 string s1="ACM"; 31 32 string s2("ACM"); 33 34 string s3=s2; 35 36 //string s4(10,"ACM"); 37 string s4(10,'A'); 38 // 第四種初始化方式,只能初始化單個字符 39 40 cout<<s1<<endl; 41 cout<<s2<<endl; 42 cout<<s3<<endl; 43 cout<<s4<<endl; 44 45 46 cout<<"陣列方式遍歷:"<<endl; 47 // 遍歷string 48 for(int i=0;i<=s1.length()+4;++i) 49 { 50 cout<<s1[i]; 51 } 52 cout<<endl; 53 // 用陣列方式遍歷,越界時,不會發生錯誤,不會拋出例外 54 55 56 cout<<"at:"<<endl; 57 // at方式遍歷 58 for(int i=0;i<s1.length();++i) 59 { 60 cout<<s1.at(i); 61 } 62 cout<<endl; 63 // 拋出例外 64 // 將上述回圈中,回圈終止條件小于改為小于等于例外如下: 65 // ACM 烐GACMterminate called after throwing an instance of 'std::out_of_range' 66 //what(): basic_string::at 67 68 69 // 迭代器遍歷 70 cout<<"迭代器遍歷:"<<endl; 71 for(string::iterator it=s1.begin();it!=s1.end();++it) 72 { 73 cout<<*it; 74 } 75 cout<<endl; 76 // 上述string::iterator迭代器可以看作是一個字符指標 77 // 注意,s1.end()不是指向字符的最后一個位置,而是指向字符最后一個位置后面的一個位置 78 79 80 81 // C_str 將string轉化為const char * 82 //char *p=s1.c_str(); 83 // 以上代碼報錯如下 84 //F:\acm\訓練日記\2020.1.27\program\t15.cpp|62|error: invalid conversion from 'const char*' to 'char*' [-fpermissive]| 85 // 可見c_str()將string型別轉化為了const char * 86 const char *s=s1.c_str(); 87 cout<<endl<<"s:"<<endl; 88 cout<<s<<endl<<endl; 89 // 可以直接用const char *賦值給string 90 string s5=s; 91 cout<<"s5:"<<endl; 92 cout<<s5<<endl; 93 // 也可以直接用char *賦值給string型別,方便好用 94 char *p="heiheihaha"; 95 string s6=p; 96 cout<<"s6:"<<s6<<endl; 97 // 其實這也是屬于上述構造建構式的范疇 98 99 100 // copy(buf,size,begin) 將begin開始的size個字符,拷貝到buf中,不會在buf的末尾,加額外的'\0' 101 // 注意越界問題,越界不會拋出例外 102 char buf[100]; 103 s1.copy(buf,3,0); 104 cout<<"buf"<<endl; 105 cout<<buf<<endl; 106 //string buf2; 107 //s1.copy(buf2,2,0); 108 //cout<<"buf2:"<<buf2<<endl; 109 // 以上代碼是錯誤的,buf必須為char*型別,不能是string型別 110 111 // 如下方法拼接,不改變s1 112 string s7=s1+s2; 113 cout<<"s1: "<<s1<<endl; 114 cout<<"s2: "<<s2<<endl; 115 cout<<"s7: "<<s7<<endl; 116 cout<<endl; 117 118 // 如下方法拼接,已改變s1 119 string s8=s1.append(s2); 120 cout<<"s1:"<<s1<<endl; 121 cout<<"s2:"<<s2<<endl; 122 cout<<"s8:"<<s8<<endl; 123 cout<<endl; 124 125 // 如不想改變的話,也可以 126 string s9; 127 s9.append(s1); 128 s9.append(s2); 129 cout<<"s1:"<<s1<<endl; 130 cout<<"s2:"<<s2<<endl; 131 cout<<"s9:"<<s9<<endl; 132 cout<<endl; 133 134 135 // find 136 int index=s1.find("ACM"); 137 cout<<index<<endl; 138 // 第二個引數不寫,默認為0 139 cout<<s1.find("ACM",0)<<endl; 140 cout<<s1.find("ACM",1)<<endl; 141 // 回傳第二個ACM出現的位置 142 143 cout<<s9<<endl; 144 // 回傳第一次出現ACM的首字符A的位置 145 cout<<s9.find_first_of("ACM")<<endl; 146 // 回傳最后一次出現ACM的末尾字符M的位置 147 cout<<s9.find_last_of("ACM")<<endl; 148 // 以上下標,都是從0開始的 149 150 // 如果沒有找到會回傳什么呢? 151 size_t num=string::npos; 152 int num1=num; 153 cout<<s9.find("ACMER")<<endl; 154 cout<<string::npos<<endl; 155 cout<<num<<endl; 156 // 你可能很奇怪上述輸出的值,讓我們看看用int型別來輸出 157 cout<<num1<<endl; 158 unsigned int num2=num; 159 cout<<num2<<endl; 160 // 實際上,這是個-1 161 // 只是因為用了較大范圍的型別(不準確,其實范圍一樣,只是將負數部分正數編碼了),才變成了那個很大的值 162 163 164 // 試試將字串中某一字串替換成另一字串 165 cout<<"s9:"<<s9<<endl; 166 int cur_index=s9.find("ACM"); 167 while(cur_index != string::npos) 168 { 169 s9.replace(cur_index,strlen("ACM"),"ACMER"); // 注意strlen函式要有頭檔案cstring 170 // 這一步保證了如果新字串中存在當前字符時,不會重復替換,甚至陷入死回圈 171 // 從已經被替換的字串后面開始 172 cur_index+=strlen("ACMER"); 173 cur_index=s9.find("ACM",cur_index); 174 } 175 cout<<"s9:"<<s9<<endl; 176 // replace,第一個引數為起始位置,第二個引數為要替換的字串有多少個字符,第三個引數為新字串 177 178 179 // 最后,是插入和洗掉操作 180 181 cout<<"s1:"<<s1<<endl; 182 //string::iterator it=find(s1.begin(),s1.end(),'a'); 183 //F:\acm\訓練日記\2020.1.27\program\t15.cpp|163|error: no matching function for call to 'find(std::basic_string<char>::iterator, std::basic_string<char>::iterator, char)'| 184 // 網上的一些字串函式,我這里好像不能用 185 // 暫時不知道原因,留作疑問,繼續深入學習 186 //cout<<*it<<endl; 187 s1.erase(s1.begin(),s1.begin()+3); 188 cout<<s1<<endl; 189 s1.insert(0,"ACMER"); 190 cout<<s1<<endl; 191 192 193 // 大小寫轉換 194 transform(s1.begin(),s1.end(),s1.begin(),to_lower); 195 cout<<s1<<endl; 196 transform(s1.begin(),s1.end(),s1.begin(),to_upper); 197 cout<<s1<<endl; 198 // 嗯?原來還要自己寫to_lower和to_upper嗎? 199 // 可能為了靈活些 200 // 畢竟是transfor,轉化是任意的,大家可以多嘗試嘗試 201 // string的基本內容,大概就這么多了,學一下,之后用起來方便點 202 203 return 0; 204 } 205 206 // 本機執行結果如下,由于前面嘗試越界,比較危險,可能引發各種問題 207 // 不過問題不大 208 /* 209 ACM 210 ACM 211 ACM 212 AAAAAAAAAA 213 陣列方式遍歷: 214 ACM 狍頸 215 at: 216 ACM 217 迭代器遍歷: 218 ACM 219 220 s: 221 ACM 222 223 s5: 224 ACM 225 s6:heiheihaha 226 buf 227 ACM 228 s1: ACM 229 s2: ACM 230 s7: ACMACM 231 232 s1:ACMACM 233 s2:ACM 234 s8:ACMACM 235 236 s1:ACMACM 237 s2:ACM 238 s9:ACMACMACM 239 240 0 241 0 242 3 243 ACMACMACM 244 0 245 8 246 4294967295 247 4294967295 248 4294967295 249 -1 250 4294967295 251 s9:ACMACMACM 252 s9:ACMERACMERACMER 253 s1:ACMACM 254 ACM 255 ACMERACM 256 acmeracm 257 acmeracm 258 259 Process returned 0 (0x0) execution time : 0.608 s 260 Press any key to continue. 261 */
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/71526.html
標籤:C++
上一篇:ACM常用STL容器
下一篇:C++讀入優化
