std::string::erase in C++ 之 erase()方法截取string的前几位、后几位

Syntax 1: Erases all characters in a string 

string& string ::erase ()
复制代码
// CPP code to illustrate
// erase() function
 
#include <iostream>
#include <string>
using namespace std;
 
// Function to demo erase()
void eraseDemo(string str)
{
    // Deletes all characters
    str.erase();
 
    cout << "After erase() : ";
    cout << str;
}
 
// Driver code
int main()
{
    string str("Hello World!");
 
    cout << "Before erase() : ";
    cout << str << endl;
    eraseDemo(str);
 
    return 0;
}
cpp
复制代码

output:

Before erase() : Hello World!
After erase() : 

 

Syntax 2: Erases all characters after position ‘pos’ 

string& string ::erase (size_type pos)
- Throw out_of_range if idx > size().
复制代码
// CPP code to illustrate working of
// erase(idx)

#include <iostream>
#include <string>
using namespace std;

// Function to demo erase
void eraseDemo(string str)
{
    // Deletes all characters except first one
    str.erase(1);

    cout << "After erase(idx) : ";
    cout << str;
}

// Driver code
int main()
{
    string str("Hello World!");

    cout << "Before erase(idx) : ";
    cout << str << endl;
    eraseDemo(str);

    return 0;
}
cpp
复制代码

output:

Before erase(idx) : Hello World!
After erase(idx) : H

Syntax 3: Erases at most, len characters of *this, starting at index idx. 

string& string ::erase (size_type idx, size_type len )
- If len is missing, all remaining characters are removed.
- Throw out_of_range if idx > size().
- 
复制代码
// CPP code to illustrate
// erase(size_type idx, size_type len )
#include <iostream>
#include <string>
using namespace std;

// Function to demo erase
void eraseDemo(string str)
{
    // Deletes 4 characters from index number 1
    str.erase(1, 4);

    cout << "After erase : ";
    cout << str;
}

// Driver code
int main()
{
    string str("Hello World!");

    cout << "Before erase : ";
    cout << str << endl;
    eraseDemo(str);

    return 0;
}
View Code
复制代码

output:

Before erase : Hello World!
After erase : H World!

Syntax 4: Erase the single character at iterator position pos. 

string& string ::erase (iterator pos)
- Return the first character after the last character removed
- If no such character is remaining then, returns 
  string::end() i.e. position after the last character.
复制代码
// CPP code to illustrate
// erase(iterator pos)

#include <iostream>
#include <string>
using namespace std;

// Function to demo erase
void eraseDemo(string str)
{
    // Deletes character at position 4
    str.erase(str.begin() + 4);

    cout << "After erase : ";
    cout << str;
}

// Driver code
int main()
{
    string str("Hello World!");

    cout << "Before erase : ";
    cout << str << endl;
    eraseDemo(str);

    return 0;
}
View Code
复制代码

output:

Before erase : Hello World!
After erase : Hell World!

Syntax 5: Erase characters from iterator pos. to another iterator pos.

string& string ::erase (iterator beg, iterator end )
- Erases all characters of the range [ beg, end)
- Returns end i.e. the first character after the
  last character removed.
- If no such character is remaining then, returns 
  string::end() i.e. position after the last character
复制代码
// CPP code to illustrate
// erase(iterator pos, iterator end)

#include <iostream>
#include <string>
using namespace std;

// Function to demo erase
void eraseDemo(string str)
{
    // Deletes all characters between 0th index and
    // str.end() - 6
    str.erase(str.begin() + 0, str.end() - 6);

    cout << "After erase : ";
    cout << str;
}

// Driver code
int main()
{
    string str("Hello World!");

    cout << "Before erase : ";
    cout << str << endl;
    eraseDemo(str);

    return 0;
}
View Code
复制代码

output:

Before erase : Hello World!
After erase : World!

 


 

from:https://www.geeksforgeeks.org/stdstringerase-in-cpp/

 

posted @   PiaYie  阅读(670)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示