这篇文章将讨论如何在 C++ 中从字符串中删除空格。
默认情况下,以下字符被视为空白字符:
- 空间
' '
- 换行
'\n'
- 回车
'\r'
- 水平制表符
'\t'
- 换页
'\f'
- 垂直制表符
'\v'
1.使用 std::remove_if
功能
标准解决方案是使用 std::remove_if
删除空白字符的算法 std::string
使用 擦除删除成语 技术。由于 std::remove_if
算法实际上并不从字符串中删除字符,而是移动所有 非空白 字符到前面并返回一个指向结束位置的迭代器。然后我们可以通过调用来删除空白字符 std::erase
.
std::remove_if
需要一个谓词来确定要从字符串中删除哪些字符。有很多方法可以指定删除空格字符的谓词:
⮚ ::isspace
我们可以用 isspace()
中定义的 cctype
标头,它检查按当前安装的 C 语言环境分类的空白字符。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> #include <string> #include <cctype> #include <algorithm> int main() { std::string s = "Hello \n\nWorld"; s.erase(std::remove_if(s.begin(), s.end(), ::isspace), s.end()); std::cout << s; return 0; } |
输出:
HelloWorld
⮚ std::isspace
我们可以使用 C 语言环境分类的空白字符,而不是依赖于 std::isspace
在标题中定义 locale
,它使用指定语言环境的 ctype facet 对空白字符进行分类。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <string> #include <algorithm> #include <locale> #include <functional> int main() { std::string s = "Hello \n\nWorld"; s.erase(std::remove_if(s.begin(), s.end(), std::bind(std::isspace<char>, std::placeholders::_1, std::locale::classic() )), s.end()); std::cout << s; return 0; } |
使用 C++11,我们可以使用 lambdas 而不是 std::bind
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <string> #include <algorithm> int main() { std::string s = "Hello \n\nWorld"; s.erase(std::remove_if(s.begin(), s.end(), [](char &c) { return std::isspace<char>(c, std::locale::classic()); }), s.end()); std::cout << s; return 0; } |
⮚ 自定义谓词
而不是使用 ::isspace
或者 std::isspace
,我们甚至可以编写一个自定义谓词,如果字符被归类为空白字符,则返回 true,否则返回 false。在 C++ 中有很多方法可以实现这一点:
⮚1.一元函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <string> #include <algorithm> bool isSpace(unsigned char c) { return (c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\v' || c == '\f'); } int main() { std::string s = "Hello \n\nWorld"; s.erase(std::remove_if(s.begin(), s.end(), isSpace), s.end()); std::cout << s; return 0; } |
⮚2.实现类的对象 ()
操作员
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <string> #include <algorithm> struct isSpace { bool operator()(unsigned c) { return (c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\v' || c == '\f'); } }; int main() { std::string s = "Hello \n\nWorld"; s.erase(std::remove_if(s.begin(), s.end(), isSpace()), s.end()); std::cout << s; return 0; } |
⮚3.拉姆达
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <string> #include <algorithm> int main() { std::string s = "Hello \n\nWorld"; s.erase(std::remove_if(s.begin(), s.end(), [](char c) { return (c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\v' || c == '\f'); }), s.end()); std::cout << s; return 0; } |
2.使用 std::regex_replace
功能
使用 C++11,我们还可以使用 std::regex_replace 为了这。如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <string> #include <regex> int main() { std::string s = "Hello \n\nWorld"; std::regex r("\\s+"); s = std::regex_replace(s, r, ""); std::cout << s; return 0; } |
3. 使用 Boost 库
最后,我们可以一起去 boost::algorithm::erase_all
,这会删除所有出现的 空间 来自输入的字符串。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> #include <string> #include <boost/algorithm/string/erase.hpp> int main() { std::string s = "Hello World"; boost::algorithm::erase_all(s, " "); std::cout << s; return 0; } |
这就是从 C++ 中的字符串中删除空格的全部内容。