gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
QStringList去除空字符
- User
QStringList去除空字符
- Assistant
有多种方法可以从QStringList中删除空字符串。
方法1:使用QStringList中的filter函数
QStringList list = {"", "hello", "", "world", ""}; list = list.filter([](const QString& str) { return !str.isEmpty(); }); // list现在为{"hello", "world"}这个方法使用了C++11的lambda表达式,将一个函数作为参数传递给filter,该函数的返回值表示字符串是否应该保留。
方法2:使用STL的remove和erase函数
QStringList list = {"", "hello", "", "world", ""}; list.erase(std::remove(list.begin(), list.end(), ""), list.end()); // list现在为{"hello", "world"}这个方法使用STL的remove函数将所有空字符串移到容器的末尾,然后使用erase函数从末尾删除它们。