【C++STL基础入门】深入浅出string类的比较(compare)、复制(copy) |
您所在的位置:网站首页 › 文字对比用什么函数 › 【C++STL基础入门】深入浅出string类的比较(compare)、复制(copy) |
文章目录
前言一、比较1.比较运算符2.compare函数
二、复制1.copy函数
总结
前言
本系列STL使用VS2022+C++20版本 在C++标准库中,string类是一个功能强大的字符串处理类,提供了丰富的操作函数。本文将详细介绍string类的比较、复制、查找字串、返回字串、交换等常用操作。通过深入浅出的解析,让读者对这些操作有更清晰的理解和掌握。 一、比较 1.比较运算符1、重载比较运算符, 结果是真和假。 示例代码: #include #include int main() { std::string str1 = "Hello"; std::string str2 = "World"; if (str1 == str2) { std::cout std::string str1 = "apple"; std::string str2 = "banana"; int result = str1.compare(str2); if (result std::cout std::string str = "hello"; int result = str.compare("world"); if (result std::cout std::string str = "helloworld"; int result = str.compare(0, 5, "hello"); if (result std::cout std::string str1 = "helloworld"; std::string str2 = "ello"; int result = str1.compare(1, 5, str2, 1, 3); if (result std::cout std::string source = "hello world"; char destination[10]; size_t copiedChars = source.copy(destination, 5, 3); destination[copiedChars] = '\0'; // 添加字符串结束符 std::cout |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |