码迷,mamicode.com
首页 > 其他好文 > 详细

STL 小白学习(2) string

时间:2018-12-24 17:19:26      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:出现   学习   com   i++   pos   das   删除   比较   成员   

  1 #include <iostream>
  2 using namespace std;
  3 #include <string>
  4 
  5 
  6 //初始化操作
  7 void test01() {
  8     //初始化操作
  9     string s1;
 10     string s2(10, a);
 11     string s3("abc");
 12     string s4(s3);
 13 
 14     cout << s1 << endl;
 15     cout << s2 << endl;
 16     cout << s3 << endl;
 17     cout << s4 << endl;
 18 
 19 
 20 }    
 21 //赋值操作
 22 void test02() {
 23     //赋值操作
 24     string s1;
 25     string s2("appasd");
 26     s1 = "132456";
 27     cout << s1 << endl;
 28     s1 = s2;
 29     cout << s1 << endl;
 30     s1 = a;
 31     cout << s1 << endl;
 32 
 33     //成员方法 assign
 34     s1.assign("jsk");
 35     cout << s1 << endl;
 36      
 37 }
 38 
 39 //取值操作
 40 void test03() {
 41     //取值操作
 42     string s1 = "abashjdsa";
 43 
 44     //重载[]操作符
 45     for (int i = 0; i < s1.size(); i++) {
 46         cout << s1[i] << " ";
 47     }
 48     cout << endl;
 49 
 50     //也提供了成员方法
 51     for (int i = 0; i < s1.size(); i++) {
 52         cout << s1.at(i) << " ";
 53     }
 54     cout << endl;
 55 
 56     //区别
 57     //[] 访问越界直接挂
 58     //at(i) 抛出异常
 59     try {
 60         cout << s1.at(100)<<endl;
 61     }
 62     catch (...) {
 63         cout << "越界 Error...!" << endl;
 64     }
 65 
 66 }
 67 //拼接操作
 68 void test04() {
 69     //+= 重载
 70     string s = "123";
 71     cout << s << endl;
 72     s += "456";
 73     cout << s << endl;
 74     string s2 = "abc";
 75     s += s2;
 76     cout << s << endl;
 77     //append 成员函数重载
 78     string s3 = "23";
 79     s.append(s3);
 80     cout << s << endl;
 81     //等号重载
 82     string s4 = s + s3;
 83     cout << s4 << endl;
 84 
 85 }
 86 
 87 //string 查找与替换
 88 void test05() {
 89     string s = "fgabcdefg";
 90     //s.find 查找第一次出现的位置
 91     int pos = s.find("z");
 92     cout << "pos: " << pos << endl;//没有 return -1
 93     //s.rfind 查找最后一次出现的位置
 94     pos = s.rfind(g);
 95     cout << "pos: " << pos << endl;
 96     
 97 
 98 }
 99 //替换
100 void test06() {
101     //替换
102     string s = "abcdefg";
103     s.replace(0, 2, "1111"); //将0位置开始的2个 ab替换成1111
104     cout << s << endl;
105 }
106 //比较
107 void test07() {
108     //比较 相等返回0
109     string s1 = "abcd";
110     string s2 = "abc4";
111 
112     if (s1.compare(s2) == 0) {
113         cout << "相等" << endl;
114     }
115     else {
116         cout << "不相等!" << endl;
117     }
118 
119 }
120 //字串操作
121 void test08() {
122     string s = "abcsdsfsadas";
123     string substring = s.substr(1, 3);//取s的从1位置开始的3个字符的字串
124     cout << substring << endl;
125 
126 }
127 //插入 删除
128 void test09() {
129     string s = "asfasddfsa";
130     s.insert(1, "123");//在s[1]位置前插入123
131     cout << s << endl;
132     s = "1423999";
133     s.erase(0, 4);//删除 s[0]开始的4个字符
134     cout << s << endl;
135 }
136 int main() {
137     test09();
138 }

 

STL 小白学习(2) string

标签:出现   学习   com   i++   pos   das   删除   比较   成员   

原文地址:https://www.cnblogs.com/likeghee/p/10169455.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!