首页 > 代码库 > 第17章 string基本字符序列容器
第17章 string基本字符序列容器
/* 第17章 string基本字符序列容器 17.1 string技术原理 17.2 string应用基础 17.3 本章小结*/// 第17章 string基本字符序列容器// 17.1 string技术原理 --------------------------------------------------------------------------------------// 17.2 string应用基础 --------------------------------------------------------------------------------------//259#include <string>#include <iostream>int main(void){ using namespace std; string s1; s1.push_back(‘a‘); s1.push_back(‘b‘); s1.push_back(‘c‘); cout << "打印s1: " << s1 << endl; // char *cArray = "efgh"; string s2(cArray); cout << "打印s2: " << s2 << endl; //字符串的"+"操作 cout << "打印s1+s2: " << s1 + s2 << endl; //字符串s1后添加字符串s2 cout << "append后,打印s1: " << s1.append(s2) << endl; //在s1的第2个字符位置前插入字符‘8‘ string::iterator i; i = s1.begin(); i++; s1.insert(i, ‘8‘); cout << "insert后,打印s1: " << s1 << endl; //字符串的"+="操作 s1 += s2; cout << "s1+=s2后,打印s1: " << s1 << endl; return 0;}//260 遍历字符#include <string>#include <iostream>int main(void){ using namespace std; char *cArray = "hello, world!"; string s(cArray); //数组方式 for(unsigned int j = 0; j < s.size(); j++) cout << "第" << j << "个字符: " << s[j] << endl; //迭代器方式 string::reverse_iterator ri, rend; rend = s.rend(); cout << "反向打印字符" << endl; for(ri = s.rbegin(); ri != rend; ri++) cout << *ri << ‘ ‘; cout << endl; return 0;}//261 erase#include <string>#include <iostream>int main(void){ using namespace std; char *cArray = "a12345678b"; string s(cArray); s.erase(s.begin()); cout << s << endl; //打印出12345678b // s.erase(s.begin() + 3, s.end() - 2); cout << s << endl; //打印出1238b // s.erase(0, 2); cout << s << endl; //打印38b // s.clear(); return 0;}//262 replace#include <string>#include <iostream>int main(void){ using namespace std; char *cArray1 = "hello,boy!"; string s(cArray1); // s.replace(6, 3, "gril"); //boy替换为girl cout << s << endl; //打印hello gril! // s.replace(10, 1, 1, ‘.‘); //将"hello gril!"的‘!‘替换为‘.‘ cout << s << endl; //打印hello gril. // s.replace(s.begin(), s.begin() + 5, "good morning"); cout << s << endl; //打印good morning girl. return 0;}//263, find#include <string>#include <iostream>int main(void){ using namespace std; //0123456789012345678901234 string s("dog bird chicken bird cat"); //字符串查找 cout << s.find("bird") << endl; //打印4 cout << (int)s.find("pig") << endl; //打印-1 //字符查找 cout << s.find(‘i‘, 7) << endl; //打印11 //从字符串的末尾开始查找字符串 cout << s.rfind("bird") << endl; //打印17 //从字符串的末尾开始查找字符 cout << s.rfind(‘i‘) << endl; //打印18 //查找第1个属于某子串的字符 cout << s.find_first_of("13r98") << endl; //找到字符r,打印6 //查找第1个不属于某字符串的字符 cout << s.find_first_not_of("dog bird 2006") << endl; //找到字符c,打印9 //查找最后一个属于某子串的字符 cout << s.find_last_of("13r98") << endl; //字符r,打印19 //查找最后一个不属于某字符串的字符 cout << s.find_last_not_of("tea") << endl; //字符c,打印22 return 0;}//263-264, compare#include <string>#include <iostream>int main(void){ using namespace std; string s1("abcdef"); string s2("abc"); cout << s1.compare("abcdef") << endl; //相等,打印0 cout << s1.compare(s2) << endl; //s1 > s2,打印1 cout << s1.compare("abyz") << endl; //s1 < "abyz",打印-1 // cout << s1.compare(0, 3, s2) << endl; //s1的前3个字符==s2,打印0 return 0;}//264#include <string>#include <iostream>int main(void){ using namespace std; string s; cout << s.empty() << endl; //空字符串,返回1 s += "1234567"; cout << s.empty() << endl; //不为空,返回0 cout << s.size() << endl; //7个字符,返回7 cout << s.length() << endl; //返回7 // const char *cArray = s.c_str(); cout << cArray[2] << endl; //返回字符3 return 0;}// 17.3 本章小结 --------------------------------------------------------------------------------------
TOP
第17章 string基本字符序列容器
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。