成都设计公司视频制作亚马逊关键词优化软件
news/
2025/10/6 23:04:41/
文章来源:
成都设计公司视频制作,亚马逊关键词优化软件,常州做的网站的公司网站,资讯网站怎么做【C】使用 list 与 string 实现基础字符串操作 文章目录 一、字符串的基础操作1.1 - startsWith1.2 - endsWith1.3 - trim1.4 - indexOf1.5 - replaceAll 二、list 基础操作2.1 - 遍历2.1.1 - 使用迭代器访问2.1.2 - 使用基于范围的 for 循环遍历2.1.3 - 使用标准算法库遍历 2.… 【C】使用 list 与 string 实现基础字符串操作 文章目录 一、字符串的基础操作1.1 - startsWith1.2 - endsWith1.3 - trim1.4 - indexOf1.5 - replaceAll 二、list 基础操作2.1 - 遍历2.1.1 - 使用迭代器访问2.1.2 - 使用基于范围的 for 循环遍历2.1.3 - 使用标准算法库遍历 2.2 - 访问元素2.3 - 删除元素 三、list\string\3.1 - 移除所有空字符串元素3.2 - 遍历字符串并应用 trim3.3 - 移除连续的空白行 一、字符串的基础操作
1.1 - startsWith
bool startsWith(const std::string fullString, const std::string starting)
{if (fullString.length() starting.length()) {return (0 fullString.compare(0, starting.length(), starting));} else {return false;}
}1.2 - endsWith
bool endsWith(const std::string fullString, const std::string ending) {if (fullString.length() ending.length()){return (0 fullString.compare(fullString.length() - ending.length(), ending.length(), ending));}else{return false;}
}1.3 - trim
用于移除字符串前后两端的空白符
// Function to trim whitespace from the beginning and end of a string
std::string trim(const std::string str) {size_t first str.find_first_not_of( \t\n\r\f\v);// No non-whitespace charactersif (first std::string::npos){ // 如果从头开始非空白符找不到说明所有的字符都是空白符因此全部去掉return ; }size_t last str.find_last_not_of( \t\n\r\f\v);// 即便 last 为 string::npos substr 也会做处理。return str.substr(first, (last - first 1));
}或者
#include algorithm
#include cctype// 去除字符串左侧空白
static inline void ltrim(std::string s) {s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {return !std::isspace(ch);}));
}// 去除字符串右侧空白
static inline void rtrim(std::string s) {s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {return !std::isspace(ch);}).base(), s.end());
}// 去除字符串两侧空白
static inline void trim(std::string s) {ltrim(s);rtrim(s);
}1.4 - indexOf
用于获取第一个子串的位置索引如果找不到则返回 -1。
// Function to find the index of the first occurrence of a substring
int indexOf(const std::string str, const std::string substr)
{size_t pos str.find(substr);return (pos ! std::string::npos) ? static_castint(pos) : -1;
}1.5 - replaceAll
// 替换字符串中所有匹配的子字符串
void replaceAll(std::string source, const std::string from, const std::string to)
{// 如果字符串为空则返回。if (from.empty()) { return; }size_t startPos 0;while ((startPos source.find(from, startPos)) ! std::string::npos) {source.replace(startPos, from.length(), to);startPos to.length(); // 在替换后移动过去新增的部分}
}二、list 基础操作
2.1 - 遍历
2.1.1 - 使用迭代器访问
#include iostream
#include list
std::listint myList {1, 2, 3, 4, 5};// 使用迭代器遍历 std::list
for (auto it myList.begin(); it ! myList.end(); it)
{std::cout *it ;
}
std::cout std::endl;2.1.2 - 使用基于范围的 for 循环遍历
#include iostream
#include list
// 使用范围基 for 循环遍历 std::list
for (int elem : myList)
{std::cout elem ;
}
std::cout std::endl;2.1.3 - 使用标准算法库遍历
#include iostream
#include list
#include algorithm // for std::for_each
std::listint myList {1, 2, 3, 4, 5};// 使用 std::for_each 遍历 std::list
std::for_each(myList.begin(), myList.end(), [](int elem) {std::cout elem ;
});
std::cout std::endl;2.2 - 访问元素
访问第 N 个元素
#include iostream
#include liststd::listint myList {10, 20, 30, 40, 50};
int N 3; // 以 0 为起始索引访问第 4 个元素
auto it myList.begin();
std::advance(it, N); // 使用 std::advance 前进到第 N 个元素if (it ! myList.end()) {std::cout The element at index N is *it std::endl;
} else {std::cout Index out of range. std::endl;
}2.3 - 删除元素
删除前 N 个元素
std::listint myList {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};int N 3; // 指定要删除的元素数量
if (N myList.size()) {// 获取开始到第 N 个元素的迭代器auto it myList.begin();std::advance(it, N); // 移动迭代器到第 N 个位置// 从开始到第 N 个元素进行删除myList.erase(myList.begin(), it);
}// 打印剩余的元素
for (int elem : myList) {std::cout elem ;
}
std::cout std::endl;三、liststring
3.1 - 移除所有空字符串元素
#include iostream
#include list
#include string// 创建并初始化一个 std::liststd::string
std::liststd::string strings {Hello, , World, , C17, };
// 输出原始列表
std::cout Original list: std::endl;
for (const auto str : strings)
{std::cout str std::endl;
}
// 移除所有空字符串
strings.remove_if([](const std::string s) { return s.empty(); });
// 输出修改后的列表
std::cout \nList after removing empty strings: std::endl;
for (const auto str : strings) {std::cout str std::endl;
}3.2 - 遍历字符串并应用 trim
std::liststd::string myStrings { hello , world! , example };// 遍历列表并应用 trim 函数
for (std::string str : myStrings) {trim(str);
}
// 打印修剪后的字符串列表
for (const auto str : myStrings) {std::cout str std::endl;
}3.3 - 移除连续的空白行
将多个连续的空白行替换为一个空白行
#include iostream
#include list
#include string
#include iteratorvoid compressEmptyLines(std::liststd::string lines) {bool lastWasEmpty false;for (auto it lines.begin(); it ! lines.end(); ) {// 检查当前行是否为空白或只包含空格bool isEmpty it-find_first_not_of( \t\n\v\f\r) std::string::npos;if (isEmpty) {if (lastWasEmpty) {// 如果当前行是空的并且上一行也是空的删除当前行it lines.erase(it);} else {// 如果当前行是空的但上一行不是保留这行并标记lastWasEmpty true;it;}} else {// 如果当前行不是空的继续前进lastWasEmpty false;it;}}
}int main() {std::liststd::string lines {Hello, , ,World,,,!, ,End};compressEmptyLines(lines);// 输出处理后的列表for (const auto line : lines) {std::cout line std::endl;}return 0;
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/929794.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!