【C++】使用 list 与 string 实现基础字符串操作
文章目录
一、字符串的基础操作
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(string">" \t\n\r\f\v");// No non-whitespace charactersif (first == std::string::npos){ // 如果从头开始非空白符找不到,说明所有的字符都是空白符,因此全部去掉return string">""; }size_t last = str.find_last_not_of(string">" \t\n\r\f\v");// 即便 last 为 string::npos substr 也会做处理。return str.substr(first, (last - first + 1));
}
或者
#include string"><algorithm>
#include string"><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_cast<int>(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__105">二、list 基础操作
2.1 - 遍历
2.1.1 - 使用迭代器访问
#include string"><iostream>
#include string"><list>
std::list<int> myList = {1, 2, 3, 4, 5};// 使用迭代器遍历 std::list
for (auto it = myList.begin(); it != myList.end(); ++it)
{std::cout << *it << string">" ";
}
std::cout << std::endl;
2.1.2 - 使用基于范围的 for 循环遍历
#include string"><iostream>
#include string"><list>
// 使用范围基 for 循环遍历 std::list
for (int elem : myList)
{std::cout << elem << string">" ";
}
std::cout << std::endl;
2.1.3 - 使用标准算法库遍历
#include string"><iostream>
#include string"><list>
#include string"><algorithm> // for std::for_each
std::list<int> myList = {1, 2, 3, 4, 5};// 使用 std::for_each 遍历 std::list
std::for_each(myList.begin(), myList.end(), [](int elem) {std::cout << elem << string">" ";
});
std::cout << std::endl;
2.2 - 访问元素
访问第 N 个元素
#include string"><iostream>
#include string"><list>std::list<int> 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 << string">"The element at index " << N << string">" is " << *it << std::endl;
} else {std::cout << string">"Index out of range." << std::endl;
}
2.3 - 删除元素
删除前 N 个元素
std::list<int> 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 << string">" ";
}
std::cout << std::endl;
liststring_185">三、list<string>
3.1 - 移除所有空字符串元素
#include string"><iostream>
#include string"><list>
#include string"><string>// 创建并初始化一个 std::list<std::string>
std::list<std::string> strings = {string">"Hello", string">"", string">"World", string">"", string">"C++17", string">""};
// 输出原始列表
std::cout << string">"Original list:" << std::endl;
for (const auto& str : strings)
{std::cout << string">"'" << str << string">"'" << std::endl;
}
// 移除所有空字符串
strings.remove_if([](const std::string& s) { return s.empty(); });
// 输出修改后的列表
std::cout << string">"\nList after removing empty strings:" << std::endl;
for (const auto& str : strings) {std::cout << string">"'" << str << string">"'" << std::endl;
}
3.2 - 遍历字符串并应用 trim
std::list<std::string> myStrings = {string">" hello ", string">" world! ", string">" example "};// 遍历列表并应用 trim 函数
for (std::string& str : myStrings) {trim(str);
}
// 打印修剪后的字符串列表
for (const auto& str : myStrings) {std::cout << string">'"' << str << string">'"' << std::endl;
}
3.3 - 移除连续的空白行
将多个连续的空白行替换为一个空白行
#include string"><iostream>
#include string"><list>
#include string"><string>
#include string"><iterator>void compressEmptyLines(std::list<std::string>& lines) {bool lastWasEmpty = false;for (auto it = lines.begin(); it != lines.end(); ) {// 检查当前行是否为空白(或只包含空格)bool isEmpty = it->find_first_not_of(string">" \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::list<std::string> lines = {string">"Hello",string">" ",string">" ",string">"World",string">"",string">"",string">"!",string">" ",string">"End"};compressEmptyLines(lines);// 输出处理后的列表for (const auto& line : lines) {std::cout << string">'"' << line << string">'"' << std::endl;}return 0;
}