logo资料库

C++字符串处理大集合.docx

第1页 / 共11页
第2页 / 共11页
第3页 / 共11页
第4页 / 共11页
第5页 / 共11页
第6页 / 共11页
第7页 / 共11页
第8页 / 共11页
资料共11页,剩余部分请下载后查看
strlen(st); strcat(st1,st2); strncat(st1,st2,n); n 表示连接上 st2 的前 n 个给 st1,在最后不要加'\0'。 strcmp(st1,st2); strncmp(st1,st2,n); 把 st1,st2 的前 n 个进行比较。 1. rember this 2. 3. strncpy(a,b,5); 4. a[5]='\0'; 5. 6. char a[10]; 7. memset(a,'#',sizeof(a)); 8. a[10]='\0'; 9. 10. 刚开始学 C/C++时,一直对字符串处理函数一知半解,这里列举 C/C++字符串处理函数 11. 12. ,希望对初学者有一定的帮助。 13. 14. C: 15. 16. char st[100]; 17. 1. 字符串长度 18. 19. 20. 2. 字符串比较 21. 22. 23. 24. 3. 附加 25. 26. 27. 28. 4. 替换 29. 30. 31. 32. 5. 查找 33. 34. 35. 36. 37. 38. C++: 39. 40. 41. string str; 42. 1. 字符串长度 43. 44. strcpy(st1,st2); strncpy(st1,st2,n); n 表示复制 st2 的前 n 个给 st1,在最后要加'\0'。 where = strchr(st,ch) where = strspn(st1,st2); 查找字符串。 where = strstr(st1,st2); ch 为要找的字符。 len = str.length(); len = str.size();
可以直接比较 也可以: str1.compare(str2); str1.compare(pos1,len1,str2,pos2,len2); 值为负,0 ,正。 nops 长度到完。 str1 += str2; 或 str1.append(str2); str1.append(str2.pos2,len2); str2 = str1.substr(); str2 = str1.substr(pos1); str2 = str1.substr(pos1,len1); string a=s.substr(0,4); //获得字符串 s 中 从第 0 位开始的长度为 4 的字 45. 46. 2. 字符串比较 47. 48. 49. 50. 51. 52. 53. 3. 附加 54. 55. 56. 57. 58. 59. 4. 字符串提取 60. 61. 62. 63. 64. 65. 符串 66. 67. 68. 5. 字符串搜索 69. 70. 71. 72. 73. 6. 插入字符串 74. 75. 76. 77. 78. 79. 符。 80. 81. 7. 替换字符串 82. 83. 84. 85. 8. 删除字符串 86. 87. 88. where = str1.find(str2); where = str1.find(str2,pos1); pos1 是从 str1 的第几位开始。 where = str1.rfind(str2); 从后往前搜。 不是赋值语句。 str1.insert(pos1,str2); str1.insert(pos1,str2,pos2,len2); str1.insert(pos1,numchar,char); numchar 是插入次数,char 是要插入的字 str1.replace(pos1,str2); str1.replace(pos1,str2,pos2,len2); str.erase(pos,len) str.clear();
//用 c 字符串 s 初始化 //用 n 个字符 c 初始化 swap(str1,str2); char *cstr = "Hello"; string str1; cstr = cstr; string str2(cstr); 89. 9. 交换字符串 90. 91. 92. 10. C --> C++ 93. 94. 95. 96. 97. 98. 对于 ACMer 来说,C 的字符串处理要比 C++的方便、简单,尽量用 C 的字符串处理函数。 99. 100.C++中 string 类常用算法 101.string 类的构造函数: 102. 103.string(const char *s); 104. 105.string(int n,char c); 106. 107.此外,string 类还支持默认构造函数和复制构造函数,如 string s1;string 108. 109.s2="hello";都是正确的写法。当构造的 string 太长而无法表达时会抛出 110. 111.length_error 异常 112. 113.string 类的字符操作: 114. 115.const char &operator[](int n)const; 116. 117.const char &at(int n)const; 118. 119.char &operator[](int n); 120. 121.char &at(int n); 122. 123.operator[]和 at()均返回当前字符串中第 n 个字符的位置,但 at 函数提供范围检查, 124. 125.当越界时会抛出 out_of_range 异常,下标运算符[]不提供检查访问。 126. 127.const char *data()const;//返回一个非 null 终止的 c 字符数组 128. 129.const char *c_str()const;//返回一个以 null 终止的 c 字符串 130. 131.int copy(char *s, int n, int pos = 0) const;//把当前串中以 pos 开始的 n 个字符 132.
//返回当前字符串的大小 //返回当前字符串的长度 //当前字符串是否为空 //返回 string 对象中可存放的最大字符串的长度 //返回当前容量(即 string 中不必增加内存即可存放的元 133.拷贝到以 s 为起始位置的字符数组中,返回实际拷贝的数目 134. 135.string 的特性描述: 136. 137.int capacity()const; 138. 139.素个数) 140. 141.int max_size()const; 142. 143.int size()const; 144. 145.int length()const; 146. 147.bool empty()const; 148. 149.void resize(int len,char c);//把字符串当前大小置为 len,并用字符 c 填充不足的 150. 151.部分 string 类的输入输出操作: 152. 153.string 类重载运算符 operator>> 154. 155.操作。 156. 157.函数 getline(istream &in,string &s);//用于从输入流 in 中读取字符串到 s 中,以换 158. 159.行符'\n'分开。 160. 161. 162. 163.string 的赋值: 164. 165.string &operator=(const string &s);//把字符串 s 赋给当前字符串 166. 167.string &assign(const char *s);//用 c 类型字符串 s 赋值 168. 169.string &assign(const char *s,int n);//用 c 字符串 s 开始的 n 个字符赋值 170. 171.string &assign(const string &s);//把字符串 s 赋给当前字符串 172. 173.string &assign(int n,char c);//用 n 个字符 c 赋值给当前字符串 174. 175.string &assign(const string &s,int start,int n);//把字符串 s 中从 start 开始的 176. //用于输入,同样重载运算符 operator<<用于输出
//把 c 类型字符串 s 连接到当前字符串结尾 177.n 个字符赋给当前字符串 178. 179.string &assign(const_iterator first,const_itertor last);//把 first 和 last 迭 180. 181.代器之间的部分赋给字符串 182. 183. 184. 185.string 的连接: 186. 187.string &operator+=(const string &s);//把字符串 s 连接到当前字符串的结尾 188. 189.string &append(const char *s); 190. 191.string &append(const char *s,int n);//把 c 类型字符串 s 的前 n 个字符连接到当前 192. 193.字符串结尾 194. 195.string &append(const string &s); 196. 197.string &append(const string &s,int pos,int n); //把字符串 s 中从 pos 开始的 n 个 198. 199.字符连接到当前字符串的结尾 200. 201.string &append(int n,char c); 202. 203.string &append(const_iterator first,const_iterator last);//把迭代器 first 和 204. 205.last 之间的部分连接到当前字符串的结尾 206. 207. 208. 209.string 的比较: 210. 211.bool perator==(const string &s1,const string &s2)const;//比较两个字符串是 212. 213.否相等 214. 215.运算符">","<",">=","<=","!="均被重载用于字符串的比较; 216. 217.int compare(const string &s) const;//比较当前字符串和 s 的大小 218. 219.int compare(int pos, int n,const string &s)const;//比较当前字符串从 pos 开始 220. //同 operator+=() //在当前字符串结尾添加 n 个字符 c
221.的 n 个字符组成的字符串与 s 的大小 222. 223.int compare(int pos, int n,const string &s,int pos2,int n2)const;//比较当 224. 225.前字符串从 pos 开始的 n 个字符组成的字符串与 s 中 pos2 开始的 n2 个字符组成的字符串 226. 227.的大小 228. 229.int compare(const char *s) const; 230. 231.int compare(int pos, int n,const char *s) const; 232. 233.int compare(int pos, int n,const char *s, int pos2) const; 234. 235.compare 函数在>时返回 1,<时返回-1,==时返回 0 236. 237.string 的子串: 238. 239.string substr(int pos = 0,int n = npos) const;//返回 pos 开始的 n 个字符组成的 240. 241.字符串 string 的交换: 242. 243.void swap(string &s2); 244. 245. 246. 247.string 类的查找函数: 248. 249.int find(char c, int pos = 0) const;//从 pos 开始查找字符 c 在当前字符串的位置 250. 251.int find(const char *s, int pos = 0) const;//从 pos 开始查找字符串 s 在当前串 252. 253.中的位置 254. 255.int find(const char *s, int pos, int n) const;//从 pos 开始查找字符串 s 中前 n 256. 257.个字符在当前串中的位置 258. 259.int find(const string &s, int pos = 0) const;//从 pos 开始查找字符串 s 在当前 260. 261.串中的位置 262. 263.//查找成功时返回所在位置,失败返回 string::npos 的值 264. //交换当前字符串与 s2 的值
265.int rfind(char c, int pos = npos) const;//从 pos 开始从后向前查找字符 c 在当前 266. 267.串中的位置 268. 269.int rfind(const char *s, int pos = npos) const; 270. 271.int rfind(const char *s, int pos, int n = npos) const; 272. 273.int rfind(const string &s,int pos = npos) const; 274. 275.//从 pos 开始从后向前查找字符串 s 中前 n 个字符组成的字符串在当前串中的位置,成 276. 277.功返回所在位置,失败时返回 string::npos 的值 278. 279.int find_first_of(char c, int pos = 0) const;//从 pos 开始查找字符 c 第一次出 280. 281.现的位置 282. 283.int find_first_of(const char *s, int pos = 0) const; 284. 285.int find_first_of(const char *s, int pos, int n) const; 286. 287.int find_first_of(const string &s,int pos = 0) const; 288. 289.//从 pos 开始查找当前串中第一个在 s 的前 n 个字符组成的数组里的字符的位置。查找 290. 291.失败返回 292. 293.string::npos 294. 295.int find_first_not_of(char c, int pos = 0) const; 296. 297.int find_first_not_of(const char *s, int pos = 0) const; 298. 299.int find_first_not_of(const char *s, int pos,int n) const; 300. 301.int find_first_not_of(const string &s,int pos = 0) const; 302. 303.//从当前串中查找第一个不在串 s 中的字符出现的位置,失败返回 string::npos 304. 305.int find_last_of(char c, int pos = npos) const; 306. 307.int find_last_of(const char *s, int pos = npos) const; 308.
int n) const; 309.int find_last_of(const char *s, int pos, int n = npos) const; 310. 311.int find_last_of(const string &s,int pos = npos) const; 312. 313.int find_last_not_of(char c, int pos = npos) const; 314. 315.int find_last_not_of(const char *s, int pos = npos) const; 316. 317.int find_last_not_of(const char *s, int pos, 318. 319.int find_last_not_of(const string &s,int pos = npos) const; 320. 321.//find_last_of 和 find_last_not_of 与 find_first_of 和 find_first_not_of 相似,只 322. 323.不过是从后向前查找 324. 325. 326. 327.string 类的替换函数: 328. 329.string &replace(int p0, int n0,const char *s);//删除从 p0 开始的 n0 个字符,然 330. 331.后在 p0 处插入串 s 332. 333.string &replace(int p0, int n0,const char *s, int n);//删除 p0 开始的 n0 个字 334. 335.符,然后在 p0 处插入字符串 s 的前 n 个字符 336. 337.string &replace(int p0, int n0,const string &s);//删除从 p0 开始的 n0 个字符, 338. 339.然后在 p0 处插入串 s 340. 341.string &replace(int p0, int n0,const string &s, int pos, int n);//删除 p0 开 342. 343.始的 n0 个字符,然后在 p0 处插入串 s 中从 pos 开始的 n 个字符 344. 345.string &replace(int p0, int n0,int n, char c);//删除 p0 开始的 n0 个字符,然后 346. 347.在 p0 处插入 n 个字符 c 348. 349.string &replace(iterator first0, iterator last0,const char *s);//把[first0 350. 351.,last0)之间的部分替换为字符串 s 352.
分享到:
收藏