從 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
廣告