Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 984209e

Browse files
authored
Create 7.3.2删除指定字符.md
1 parent 6599da2 commit 984209e

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

‎7.3.2删除指定字符.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
- 问题:从可变的ASCII字符串中删除字符。函数中包含两个参数,str和remove。必须从str中删除remove中存在的字符。
2+
- 例子:str = "Battle", remove = "ae",输出:"Bttl"
3+
- 思想:
4+
- 此问题可以拆成两个问题:
5+
1. 判断是否应该删除。
6+
2. 解决如何删除。
7+
1. 判断是否应该删除:
8+
1. 将remove中的字符存储到map中。
9+
2. 遍历str,如果str中的字符在map中存在,就需要删除
10+
2. 如何删除:
11+
1. 使用一个指针,默认指向字符串第一个位置。
12+
2. 遍历字符串str,当每次遇到不需要删除的字符时,就将指针指向的字符重新写回到str中。
13+
14+
```cpp
15+
#include <string>
16+
#include <iostream>
17+
#include <map>
18+
using namespace std;
19+
20+
string Delete(string str, const string& target_str){
21+
map<char, int> memo;
22+
for(char c : target_str){
23+
memo[c] = 1;
24+
}
25+
int j = 0;
26+
for(int i = 0; i < str.size(); i++){
27+
if(memo[str[i]] != 1){
28+
str[j] = str[i];
29+
j++;
30+
}
31+
}
32+
return str.erase(j);
33+
}
34+
35+
int main() {
36+
string inputString = "angleofmusicguideandgardiengranttomeyourglory", targetString = "aeiou";
37+
38+
string result = Delete(inputString, targetString);
39+
40+
cout << "Resulting string after deletion: " << result << endl;
41+
42+
return 0;
43+
}
44+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /