怎么做属于自己的售卡网站,品牌设计和广告设计,网站搜索功能怎么做,wordpress分权限浏览器1#xff1a;C 语言中的字符串 vs C string 类
1.1 C 语言中的字符串
在 C 语言中#xff0c;字符串本质上是以 \0 结尾的字符数组。C 标准库为此提供了 str 系列函数#xff0c;如 strlen()、strcpy() 和 strcat() 等。虽然这些函数可以操作字符串#xff0c;但它们的操…1C 语言中的字符串 vs C string 类
1.1 C 语言中的字符串
在 C 语言中字符串本质上是以 \0 结尾的字符数组。C 标准库为此提供了 str 系列函数如 strlen()、strcpy() 和 strcat() 等。虽然这些函数可以操作字符串但它们的操作十分繁琐且容易出错尤其是在内存管理方面。
例如在 C 语言中进行字符串拼接的代码如下
#include stdio.h
#include string.hint main() {char str1[50] Hello;char str2[50] World;// 字符串连接strcat(str1, str2);printf(%s\n, str1); // 输出Hello Worldreturn 0;
}
问题C 语言中的字符串操作容易出现内存溢出因为需要手动管理字符数组的长度。
1.2 C string 类的优势
C 中的 string 类使得字符串操作更加安全和简便。它封装了复杂的内存管理并提供了类似数组的接口开发者不再需要手动管理字符串的长度和内存。例如使用 string 进行字符串拼接
#include iostream
#include string
using namespace std;int main() {string str1 Hello;string str2 World;// 使用 操作符进行拼接str1 str2;cout str1 endl; // 输出Hello Worldreturn 0;
}
2string 类的构造与基础操作
2.1 string 类的构造方法 string 类支持多种构造方式以下是常见的构造函数 函数名 功能描述 string() 默认构造一个空字符串 string(const char* s) 使用 C 字符串 s 构造 string 对象 string(size_t n, char c) 构造一个包含 n 个字符 c 的字符串 string(const string s) 使用已有的 string 对象进行拷贝构造 2.1.1 示例代码构造字符串
#include iostream
#include string
using namespace std;int main() {string s1; // 空字符串string s2(Hello C); // 通过C字符串初始化string s3(s2); // 拷贝构造string s4(5, A); // 5个A字符的字符串cout s1: s1 endl;cout s2: s2 endl;cout s3: s3 endl;cout s4: s4 endl;return 0;
}
输出示例
s1: s2: Hello C s3: Hello C s4: AAAAA
2.2 string 对象的常见操作 函数名 功能描述 size() 返回字符串的长度 length() 返回字符串的长度与 size() 等价 capacity() 返回当前分配的存储空间大小 empty() 判断字符串是否为空若为空返回 true clear() 清空字符串内容 reserve() 为字符串预留存储空间不改变有效字符的个数 resize() 改变字符串的长度若增大则用默认字符填充 2.2.1字符串容量操作
注意size()与length()方法底层实现原理完全相同引入size()的原因是为了与其他容器的接口保持一致一般情况下基本都是用size()。clear()只是将string中有效字符清空不改变底层空间大小。resize(size_t n) 与 resize(size_t n, char c)都是将字符串中有效字符个数改变到n个不 同的是当字符个数增多时resize(n)用\0来填充多出的元素空间resize(size_t n, char c)用字符c来填充多出的元素空间。注意resize在改变元素个数时如果是将元素个数增多可能会改变底层容量的大小如果是将元素个数减少底层空间总大小不变。reserve(size_t res_arg0)为string预留空间不改变有效元素个数当reserve的参数小于string的底层空间总大小时reserve不会改变容量大小。 size和capacity都不包括’\0’
#include iostream
#include string
using namespace std;int main() {string s Hello;cout Size: s.size() endl; // 返回字符串长度cout Capacity: s.capacity() endl; // 返回容量s.resize(10, X); // 将长度改为10多出部分用X填充cout Resized: s endl;s.clear(); // 清空字符串cout Is empty: s.empty() endl; // 检查是否为空return 0;
}
输出示例
Size: 5 Capacity: 15 Resized: HelloXXXXX Is empty: 1
2.3 字符串的遍历与访问
可以通过以下几种方式访问字符串中的字符 方法 功能描述operator[] 返回或设置指定位置的字符 at() 返回指定位置的字符并进行边界检查begin()/end() 返回字符串的首尾迭代器用于遍历字符串 rbegin()/rend() 返回反向迭代器支持从后向前遍历字符串 push_back() 在字符串末尾追加一个字符 append() 在字符串末尾追加另一个字符串或子字符串 注意
在string尾部追加字符时s.push_back(c) / s.append(1, c) / s c三种的实现方式差不多一般情况下string类的操作用的比较多操作不仅可以连接单个字符还可以连接字符串。 对string操作时如果能够大概预估到放多少字符可以先通过reserve把空间预留好。
2.3.1 遍历与访问字符
#include iostream
#include string
using namespace std;int main() {string s Hello C;// 使用下标访问cout First character: s[0] endl;// 使用迭代器遍历for (auto it s.begin(); it ! s.end(); it) {cout *it ;}cout endl;return 0;
} 输出示例 First character: H H e l l o C 3字符串的高级操作
3.1 字符串的查找操作
string 类提供了多种查找子字符串或字符的方法。常见的查找方法如下 函数名 功能说明find() 在字符串中查找子字符串或字符返回其首次出现的位置找不到则返回 string::npos rfind() 反向查找字符串返回最后一次出现子串或字符的位置find_first_of() 查找指定字符集中的任意一个字符返回第一次出现的索引 find_last_of() 查找指定字符集中的任意一个字符返回最后一次出现的索引 string::npos 是 std::string 类的一个常量静态成员变量它是用来表示查找操作失败时的返回值。 它通常等于无符号整数类型的最大值size_t(-1)具体值是实现定义的但它在所有实现中都用于表示“未找到”的状态。 #include iostream
#include string
using namespace std;int main() {cout Value of string::npos: string::npos endl;return 0;
} 输出结果 Value of string::npos: 18446744073709551615 // 这是 size_t 的最大值 (通常等于 -1) 3.1.1 查找子字符串
#include iostream
#include string
using namespace std;int main() {string s Hello World;// 查找 World 在字符串中的位置size_t pos s.find(World);if (pos ! string::npos) {cout World found at position: pos endl;} else {cout World not found endl;}// 反向查找 osize_t rpos s.rfind(o);cout o last found at position: rpos endl;return 0;
} 输出示例 World found at position: 6 o last found at position: 7 3.1.2 示例代码查找任意字符
#include iostream
#include string
using namespace std;int main() {string s Hello, World!;string charset aeiou; // 查找元音字母// 使用 find_first_of 查找字符集中任意字符第一次出现的位置size_t first_pos s.find_first_of(charset);if (first_pos ! string::npos) {cout First vowel in the string found at position: first_pos endl;} else {cout No vowel found in the string. endl;}// 使用 find_last_of 查找字符集中任意字符最后一次出现的位置size_t last_pos s.find_last_of(charset);if (last_pos ! string::npos) {cout Last vowel in the string found at position: last_pos endl;} else {cout No vowel found in the string. endl;}return 0;
} 输出示例 First vowel in the string found at position: 1 Last vowel in the string found at position: 8 3.3 字符串的比较操作
在 C 中string 类支持字符串的比较操作既可以使用运算符 、!、、 等也可以通过 compare() 方法进行更细粒度的比较。 方法 功能说明 operator 判断两个字符串是否相等 operator! 判断两个字符串是否不相等 operator 判断当前字符串是否小于另一个字符串 operator 判断当前字符串是否大于另一个字符串 compare() 进行详细的字符串比较返回 0 表示相等负值表示小于正值表示大于 3.2.1 字符串比较
#include iostream
#include string
using namespace std;int main() {string str1 Apple;string str2 Banana;if (str1 str2) {cout Strings are equal endl;} else {cout Strings are not equal endl;}// 使用 compare() 方法比较int result str1.compare(str2);if (result 0) {cout Strings are equal endl;} else if (result 0) {cout str1 is less than str2 endl;} else {cout str1 is greater than str2 endl;}return 0;
} 输出示例 Strings are not equal str1 is less than str2 3.3字符串的替换操作
在 C 中string 类允许我们通过 replace() 方法替换字符串中的部分内容。
函数名 功能说明 replace() 替换从指定位置开始的若干字符为新字符串
3.3.1 替换字符串中的部分内容
#include iostream
#include string
using namespace std;int main() {string str Hello World;// 替换 World 为 Cstr.replace(6, 5, C);cout str endl; // 输出Hello Creturn 0;
}
输出示例
Hello C 3.4 字符串的截取操作
string 类提供了 substr() 方法来提取字符串中的子字符串。该方法非常有用尤其是在处理文件路径或URL时。 函数名 功能说明 substr() 从指定位置开始截取若干字符并返回子字符串 3.4.1提取子字符串
#include iostream
#include string
using namespace std;int main() {string str Hello World;// 从位置 6 开始截取 5 个字符string sub str.substr(6, 5);cout Substring: sub endl;return 0;
} 输出示例 Substring: World 3.5字符串的插入与删除操作
在 C 中string 类支持通过 insert() 在字符串的指定位置插入子字符串或通过 erase() 从指定位置删除字符。
这两个方法因为时间复杂度挺高的所以还是避免常用 函数名 功能说明 insert() 在字符串的指定位置插入字符或子字符串 erase() 删除字符串中指定位置的若干字符 3.5.1 插入与删除操作
#include iostream
#include string
using namespace std;int main() {string str Hello World;// 在索引 5 处插入一个逗号str.insert(5, ,);cout After insert: str endl;// 删除索引 5 开始的 1 个字符str.erase(5, 1);cout After erase: str endl;return 0;
} 输出示例 After insert: Hello, World After erase: Hello World 3.6 字符串与数值的转换
C 提供了 to_string() 和 stoi() 等函数帮助我们在字符串和数值之间进行转换。这在处理用户输入、文件解析等场景中非常常用。
函数名 功能说明 to_string() 将数值转换为字符串 stoi() 将字符串转换为整数 stof() 将字符串转换为浮点数
3.6.1 数字与字符串的相互转换
#include iostream
#include string
using namespace std;int main() {int num 42;string str to_string(num); // 数字转字符串cout String: str endl;string strNum 123;int parsedNum stoi(strNum); // 字符串转整数cout Parsed Integer: parsedNum endl;return 0;
} 输出示例 String: 42 Parsed Integer: 123
文章转载自: http://www.morning.knzdt.cn.gov.cn.knzdt.cn http://www.morning.mlpmf.cn.gov.cn.mlpmf.cn http://www.morning.dxqfh.cn.gov.cn.dxqfh.cn http://www.morning.dplmq.cn.gov.cn.dplmq.cn http://www.morning.kqblk.cn.gov.cn.kqblk.cn http://www.morning.tjwlp.cn.gov.cn.tjwlp.cn http://www.morning.tkfnp.cn.gov.cn.tkfnp.cn http://www.morning.nnpwg.cn.gov.cn.nnpwg.cn http://www.morning.qwdlj.cn.gov.cn.qwdlj.cn http://www.morning.xoaz.cn.gov.cn.xoaz.cn http://www.morning.bxdlrcz.cn.gov.cn.bxdlrcz.cn http://www.morning.ygqjn.cn.gov.cn.ygqjn.cn http://www.morning.glpxx.cn.gov.cn.glpxx.cn http://www.morning.nyqm.cn.gov.cn.nyqm.cn http://www.morning.dhnqt.cn.gov.cn.dhnqt.cn http://www.morning.xcyhy.cn.gov.cn.xcyhy.cn http://www.morning.clbzy.cn.gov.cn.clbzy.cn http://www.morning.nxbkw.cn.gov.cn.nxbkw.cn http://www.morning.zqfz.cn.gov.cn.zqfz.cn http://www.morning.3jiax.cn.gov.cn.3jiax.cn http://www.morning.bwmm.cn.gov.cn.bwmm.cn http://www.morning.xgbq.cn.gov.cn.xgbq.cn http://www.morning.mzhhr.cn.gov.cn.mzhhr.cn http://www.morning.qzfjl.cn.gov.cn.qzfjl.cn http://www.morning.wpmlp.cn.gov.cn.wpmlp.cn http://www.morning.lzph.cn.gov.cn.lzph.cn http://www.morning.syssdz.cn.gov.cn.syssdz.cn http://www.morning.gxeqedd.cn.gov.cn.gxeqedd.cn http://www.morning.rdnpg.cn.gov.cn.rdnpg.cn http://www.morning.ddqdl.cn.gov.cn.ddqdl.cn http://www.morning.mrfjr.cn.gov.cn.mrfjr.cn http://www.morning.wjwfj.cn.gov.cn.wjwfj.cn http://www.morning.pwghp.cn.gov.cn.pwghp.cn http://www.morning.lmqfq.cn.gov.cn.lmqfq.cn http://www.morning.kfmlf.cn.gov.cn.kfmlf.cn http://www.morning.plflq.cn.gov.cn.plflq.cn http://www.morning.xfcjs.cn.gov.cn.xfcjs.cn http://www.morning.rdzlh.cn.gov.cn.rdzlh.cn http://www.morning.lnwdh.cn.gov.cn.lnwdh.cn http://www.morning.ygwbg.cn.gov.cn.ygwbg.cn http://www.morning.ykwqz.cn.gov.cn.ykwqz.cn http://www.morning.bpmfr.cn.gov.cn.bpmfr.cn http://www.morning.qmbtn.cn.gov.cn.qmbtn.cn http://www.morning.hnhkz.cn.gov.cn.hnhkz.cn http://www.morning.c-ae.cn.gov.cn.c-ae.cn http://www.morning.jzlkq.cn.gov.cn.jzlkq.cn http://www.morning.pnmnl.cn.gov.cn.pnmnl.cn http://www.morning.yzfrh.cn.gov.cn.yzfrh.cn http://www.morning.cgthq.cn.gov.cn.cgthq.cn http://www.morning.xyyplp.cn.gov.cn.xyyplp.cn http://www.morning.rbjf.cn.gov.cn.rbjf.cn http://www.morning.ssxlt.cn.gov.cn.ssxlt.cn http://www.morning.kflzy.cn.gov.cn.kflzy.cn http://www.morning.tsflw.cn.gov.cn.tsflw.cn http://www.morning.bfcxf.cn.gov.cn.bfcxf.cn http://www.morning.ljhnn.cn.gov.cn.ljhnn.cn http://www.morning.lhldx.cn.gov.cn.lhldx.cn http://www.morning.zfwjh.cn.gov.cn.zfwjh.cn http://www.morning.rahllp.com.gov.cn.rahllp.com http://www.morning.zhmgcreativeeducation.cn.gov.cn.zhmgcreativeeducation.cn http://www.morning.gjtdp.cn.gov.cn.gjtdp.cn http://www.morning.prxqd.cn.gov.cn.prxqd.cn http://www.morning.tnjkg.cn.gov.cn.tnjkg.cn http://www.morning.bnlkc.cn.gov.cn.bnlkc.cn http://www.morning.pnljy.cn.gov.cn.pnljy.cn http://www.morning.dsncg.cn.gov.cn.dsncg.cn http://www.morning.nkpls.cn.gov.cn.nkpls.cn http://www.morning.kjyqr.cn.gov.cn.kjyqr.cn http://www.morning.dyzbt.cn.gov.cn.dyzbt.cn http://www.morning.xsetx.com.gov.cn.xsetx.com http://www.morning.gcrlb.cn.gov.cn.gcrlb.cn http://www.morning.bfrff.cn.gov.cn.bfrff.cn http://www.morning.ltcnd.cn.gov.cn.ltcnd.cn http://www.morning.tzcr.cn.gov.cn.tzcr.cn http://www.morning.wdykx.cn.gov.cn.wdykx.cn http://www.morning.rwpjq.cn.gov.cn.rwpjq.cn http://www.morning.nclps.cn.gov.cn.nclps.cn http://www.morning.nkcfh.cn.gov.cn.nkcfh.cn http://www.morning.whothehellami.com.gov.cn.whothehellami.com http://www.morning.cpwmj.cn.gov.cn.cpwmj.cn