菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
233
0

STL - string

原创
05/13 14:22
阅读数 32125

STL基本原理

案例一:构建一个简易容器

 1 #include<iostream>
 2 
 3 using namespace std;
 4 
 5 //功能:查找一个容器中某元素个数
 6 int _find(int* start, int* end, int num)
 7 {
 8     int count = 0;
 9     while (start != end)
10     {
11         if (*start == num)
12         {
13             ++count;
14         }
15         ++start;
16     }
17     return count;
18 }
19 
20 int main()
21 {
22     int _array[] = { 0,2,9,1,2,3,2 };
23     /*int _array[6];
24     int j = 0;
25     for (auto& _element:_array)
26     {
27     _element = j*j;
28     ++j;
29     }
30     int* end = &(_array[sizeof(_array)/sizeof(int) - 1]);
31     cout << "*end = " << *end << endl;*/
32 
33     int* _begin = _array;//指针指向首元素
34     int* _end = &(_array[sizeof(_array) / sizeof(int)]);
35     int count;
36     count = _find(_begin, _end, 2);
37     cout << "count = " << count << endl;
38     system("pause");
39     return 1;
40 }

 

 

 

 

  1 // string不同考虑越界问题,内存清理;所以在C++中建议你不要用char*,改用string
  2 // string是一个类
  3 
  4 #include<string>
  5 #include<iostream>
  6 
  7 using namespace std;
  8 
  9 void test02()
 10 {
 11     cout << " test02():" << endl;
 12     string strA = "shiruiyu";
 13     string strB;
 14 
 15     //成员函数 [] 遍历【越界不提醒!】
 16     for (int i = 0; i < strA.size(); i++)
 17     {
 18         cout << strA[i];
 19     }
 20     cout << endl;
 21     // 操作符  at 遍历【越界抛出异常】
 22     for (int i = 0; i < strA.size(); i++)
 23     {
 24         cout << strA.at(i);
 25     }
 26     cout << endl;
 27     try
 28     {
 29         //cout << strA.at(10) << endl;//越界 抛出异常 打印
 30         //cout << strA[10] << endl;//越界 不抛出异常
 31     }
 32     catch (const std::exception&)
 33     {
 34         cout << "越界!" << endl;
 35     }
 36 }
 37 
 38 //重载符
 39 void test04()
 40 {
 41     cout << " test04():" << endl;
 42     string s = "shi";
 43     string s2;
 44     s += s;
 45     cout << s << endl; // shishi
 46     string s3 = "rui";
 47     s.append(s3);
 48     cout << s << endl;// shishirui
 49 
 50     string s4 = "12345";
 51     string s5 = "678";
 52     s4.append(s5, 2, 1);//一般前面表示起始下标,后面表示个数(包含起始位置)
 53     cout << s4 << endl; // 123458
 54 }
 55 
 56 //查找
 57 void test05()
 58 {
 59     cout << " test05():" << endl;
 60     string s = "123454321";
 61     int pos = s.find("3");
 62     cout << "第一次出现位置:pos = " << pos <<endl;
 63     int _pos = s.find("5", 3);//4
 64     int __pos = s.find("454", 0,2);//从 0开始查找 “454”的前2个字符“45”第一次出现的位置
 65     int pos_ = s.rfind("3");
 66     cout << "最后一次出现位置:pos_ = " << pos_ << endl;
 67 }
 68 
 69 //替换
 70 void test06()
 71 {
 72     cout << " test06():" << endl;
 73     string s = "shiruiyu";
 74     s.replace(0, 3, "zhang");
 75     cout << "s = " << s.c_str() << endl;//zhangruiyu
 76 }
 77 //比较
 78 void test07()
 79 {
 80     cout << " test07():" << endl;
 81     string s1 = "abce";
 82     string s2 = "abcd";
 83     if (s1.compare(s2)== 0)
 84     {
 85         cout << "字符串相等!" << endl;
 86     }
 87     else if (s1.compare(s2) == 1)
 88     {
 89         cout << "s1 > s2" << endl;
 90     }
 91     else
 92     {
 93         cout << "s1 < s2" << endl;
 94     }
 95 }
 96 // 子串操着 和 删除
 97 void test08()
 98 {
 99     cout << " test08():" << endl;
100     string s = "shiruiyu";
101     //子串
102     string mysub =  s.substr(1, 3);
103     cout << mysub.c_str() << endl; // hir
104 
105     //删除
106     s.erase(0, 2);//一般前面表示起始下标,后面表示个数(包含起始位置)
107     cout << "删除前面两个s = " << s.c_str() << endl;// iruiyu
108     s.insert(2, "2222");
109     cout << "插入到第2个元素前面s = " << s.c_str() << endl;// ir2222uiyu
110 }
111 
112 int main()
113 {
114     test02();
115     test04();
116     test05();
117     test06();
118     test07();
119     test08();
120     return 1;
121 }

 find的例子:

//代码来自   http://www.cplusplus.com/reference/string/string/find/
// 这个网站可以在线编译
 1 // string::find
 2 #include <iostream>       // std::cout
 3 #include <string>         // std::string
 4 
 5 int main()
 6 {
 7     std::string str("There are two needles in this haystack with needles.");
 8     std::string str2("needle");
 9 
10     // different member versions of find in the same order as above:
11     std::size_t found = str.find(str2);
12     if (found != std::string::npos)
13         std::cout << "first 'needle' found at: " << found << '\n';
14     //可用于重复查找
15     found = str.find("needles are small", found + 1, 6);// 从 found + 1 位置开始查找 needle 出现的位置
16     if (found != std::string::npos)
17         std::cout << "second 'needle' found at: " << found << '\n';
18 
19     found = str.find("haystack");
20     if (found != std::string::npos)
21         std::cout << "'haystack' also found at: " << found << '\n';
22 
23     found = str.find('.');
24     if (found != std::string::npos)
25         std::cout << "Period found at: " << found << '\n';
26 
27     // let's replace the first needle:
28     str.replace(str.find(str2), str2.length(), "preposition");// 这个替换功能很有趣
29     std::cout << str << '\n';
30 
31     return 0;
32 }

 

 1 // string::find
 2 #include <iostream>       // std::cout
 3 #include <string>         // std::string
 4 #include<algorithm>
 5 #include<vector>
 6 using namespace std;
 7 int main()
 8 {
 9     std::string str("abc acc abc");
10     // 查找某字符出现次数
11     // <1> 直接遍历
12     vector<int> positions;
13     for (int i = 0; i < str.size(); i++)
14     {
15         if (str[i] == 'b')
16         {
17             positions.push_back(i);
18         }
19     }
20     // <2> 利用find成员方法
21     vector<int> positions_;
22     std::size_t found = 0;
23     while (true)
24     {
25         found = str.find("c", found, 1); // 第二次从“ acc abc”查找;应为下面 found++
26         if (found == std::string::npos)
27         {
28             break;
29         }
30         cout << "found = " << (found) << endl;
31         positions_.push_back(found);
32         found++;
33     }
34     
35 
36     return 0;
37 }

 

 


 1 // 参考: http://www.cnblogs.com/Pillar/p/4206452.html
 2 #include<string>
 3 #include<iostream>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     char* chars = "shiruiyu"; //对于 sizeof(chars)/sizeof(char)) new 的时候 +1 //遍历的时候不用
 9     // <1> char* 转 string
10     string  string_ = chars;// 深度拷贝
11     chars = "hehe";
12     cout << chars << endl; // hehe
13     cout << string_ << endl; // shiruiyu
14     // <2> string 转char*
15     const char* chars_ = new char[string_.size() + 1];
16     string string__ = "china";
17     chars_ = string__.data(); // 必须是const类型
18     cout << chars_ << endl; // china
19     return 1;
20 }

 

 

 // string char* char 之间的转换

 1 #include<iostream>
 2 #include<string>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     string str1 = "shiruiyu";
 9     char* chars1 = new char[str1.size()]; // 接收string
10     // string 转 char*
11     strcpy(chars1, str1.c_str());
12     cout << "chars1 = " << chars1 << endl;  // char* 可以直接打印
13     // char* 转 string
14     string str2 = chars1;
15 
16     // string 转 char
17     char chars2[] = "";  // 接收string
18     strcpy(chars2, str1.c_str());
19     cout << "chars2 = " << chars2 << endl;
20     // char 转 string
21     string str3 = chars2;
22     return 1;
23 }

 【问题:sizeof 与 length(string的成员函数) 区别?】

见我博客:https://www.cnblogs.com/winslam/p/9406330.html    【前几行】

发表评论

0/200
233 点赞
0 评论
收藏