从 C++ 中的 std::string 中删除空格
在本程序中,我们将了解如何在 C++ 中从 std::string 中删除空格。为了实现此目的,我们将使用 remove() 函数。此 remove() 函数获取迭代器的开头和结尾,然后获取第三个参数,该参数将从该迭代器对象中被删除。
Input: A string "This is C++ Programming Language" Output: "ThisisC++ProgrammingLanguage"
算法
Step 1: Get the string Step 2: Remove spaces from the given string using remove() function. Step 3: Return string.
示例代码
#include<iostream>
#include<algorithm>
using namespace std;
main() {
string my_str = "This is C++ Programming Language";
cout << "String with Spaces :" << my_str << endl;
remove(my_str.begin(), my_str.end(), ' ');
cout << "String without Spaces :" << my_str;
}输出
String with Spaces :This is C++ Programming Language String without Spaces :ThisisC++ProgrammingLanguage
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP