1 /*** 2 * string 基础api复习 3 * 8 AUG 2018 4 */ 5 6 #include <iostream> 7 #include <string> 8 using namespace std; 9 10 void print(string str, int index) 11 { 12 std::cout << "str" << index << " = " << str << std::endl; 13 } 14 15 int main() 16 { 17 // string对象初始化 18 string str0("i am a boy~"); 19 string str1 = "hello world!"; 20 string str2(str1); 21 string str3(5, ‘c‘); 22 string str4(str1, 2, 3); // 从【2】索引起始,长度为3的字符串 23 24 print(str0, 0); 25 print(str1, 1); 26 print(str2, 2); 27 print(str3, 3); 28 print(str4, 4); 29 30 // string相关简单函数 31 bool power; 32 int str_len; 33 char te_ch; 34 string te_str; 35 power = str0.empty(); // 判断字符串是否为空 36 std::cout << "power = " << power << std::endl; 37 str_len = str1.size(); // 字符串长度 38 std::cout << "str_len = " << str_len << std::endl; 39 te_ch = str2[4]; // 返回字符串的第n个字符,下标从0开始 40 std::cout << "str2[4] = " << te_ch << std::endl; 41 te_ch = str2.at(4); // 返回字符串的第n个字符,下标从0开始,同上 42 std::cout << "str2.at(4) = " << te_ch << std::endl; 43 te_str = str1 + str0; // 连接两个字符串变成一个新的字符串 44 std::cout << "str1 + str0 = " << te_str << std::endl; 45 te_str = str3; // 替换字符串 46 std::cout << "te_str = " << te_str << std::endl; 47 te_str.append(str0); // 追加字符串 48 std::cout << "te_str = " << te_str << std::endl; 49 te_str.assign(str0); // 为字符串对象赋值 50 std::cout << "te_str = " << te_str << std::endl; 51 52 // str.begin()&&str.end()均为迭代器 53 string::iterator it = str0.begin(); // 字符串迭代器,类似指针 54 std::cout << "it = " << (*it) << std::endl; 55 56 // string::c_str() 57 char* char_str = new char[strlen(str0.c_str()) + 1]; 58 strcpy(char_str, str0.c_str()); // 当需要将字符串转成const char*时,用c_str(意味着转换成C语音的方式) 59 std::cout << "char_str = " << char_str << std::endl; 60 delete []char_str; 61 char_str = NULL; 62 63 // string::erase(),注意,erase后迭代器自动指向后一个位置 64 str0.erase(5, 2); // 从index值为5.删除长度为4个字符 65 std::cout << "str0.erase(5, 2) = " << str0 << std::endl; 66 str0.erase(str0.begin() + 2); // 删除index值为2的字符 67 std::cout << "str0.erase(str0.begin() + 2) = " << str0 << std::endl; 68 str0.erase(str0.begin() + 2, str0.end() - 1); // 从index值为2,删除到index末尾减一 69 std::cout << "str0.erase(str0.begin() + 2) = " << str0 << std::endl; 70 71 // string::find() 72 std::size_t found = str1.find(str4, 0); // 从str1中找str4第一次出现的地方 73 if (found != string::npos) 74 std::cout << "first found is " << found << std::endl; 75 found = str1.find(‘o‘); // 从str1中找o第一次出现的地方 76 if (found != string::npos) 77 std::cout << "first found is " << found << std::endl; 78 found = str1.find("world"); // 从str1中找world第一次出现的地方 79 if (found != string::npos) 80 std::cout << "first found is " << found << std::endl; 81 82 // string::insert() 83 str0.insert(1, " love"); // 在str0的index值为1后插入 love 84 print(str0, 0); 85 str0.insert(6, " you", 0, 3); // 在str0的index值为6后插入 you中从index值为0到长度为3的字符串 86 print(str0, 0); 87 str0.insert(0, str1); // 在str0的index值为0后插入str1 88 print(str0, 0); 89 90 // string::length()与string::size()用法相同,值也一样 91 std::cout << "str0.length() = " << str0.length() << " str0.size() = " << str0.size() << std::endl; 92 93 // string::maxsize()返回能够存储字符串的最大长度 94 std::cout << "str0.max_size() = " << str0.max_size() << std::endl; 95 96 // string::rbegin()和string::rend()逆向迭代器 97 for (string::reverse_iterator it = str0.rbegin(); it != str0.rend(); it++) 98 { 99 std::cout << (*it); 100 } 101 std::cout << std::endl; 102 print(str0, 0); 103 104 // 这个是algorithm里面的,混入是为了对比效果 105 reverse(str0.begin(), str0.end()); 106 print(str0, 0); 107 108 // string::replace()用法类似string::insert() 109 str0.replace(5, 3, "ni hao", 2, 4); 110 print(str0, 0); 111 112 // string::substr() 113 te_str = str0.substr(5, 3); 114 std::cout << "te_str = " << te_str << std::endl; 115 116 // string::swap() 117 te_str.swap(str0); 118 print(str0, 0); 119 std::cout << "te_str = " << te_str << std::endl; 120 121 while (true) 122 { 123 getchar(); 124 } 125 return 0; 126 }
原文地址:https://www.cnblogs.com/tangcumalaxia/p/9441420.html
时间: 2024-10-05 05:06:15