当前位置: 首页 > news >正文

新乡移动网站建设北京兼职做网站推广

新乡移动网站建设,北京兼职做网站推广,图片制作在线,建设网站的费用入什么科目【算法】 【c】字符串s1 中删除所有 s2 中出现的字符 eg: s1:“helloworld” s2:“wd” 删除后:s1:“helloorl” 1 双循环匹配并删除–>时间复杂度O(n^2) string 里面的删除函数–>erase std::string::erase 是 C 标准库中用于删除字符串中字符…

【算法】 【c++】字符串s1 中删除所有 s2 中出现的字符

eg:
s1:“helloworld”
s2:“wd”
删除后:s1:“helloorl”

1 双循环匹配并删除–>时间复杂度O(n^2)

string 里面的删除函数–>erase

std::string::erase` 是 C++ 标准库中用于删除字符串中字符或子串的方法。


1. erase(pos, len)
  • 作用:从字符串的 索引 pos 位置开始,删除 len 个字符。
  • 参数
    • pos:起始索引(从 0 开始)。
    • len:要删除的字符数量(默认值 std::string::npos,即删除到末尾)。
  • 返回值:修改后的 string 自身(左移剩余字符)。

示例

#include <iostream>
#include <string>using namespace std;int main() {string s = "abcdef";s.erase(2, 2);  // 从索引 2 ('c') 开始删除 2 个字符,变成 "abef"cout << s << endl;  // 输出: "abef"return 0;
}

2. erase(iterator pos)
  • 作用:删除 pos 迭代器指向的字符。
  • 参数
    • pos:指向要删除字符的 迭代器
  • 返回值:指向删除字符后 下一个字符的迭代器

示例

#include <iostream>
#include <string>using namespace std;int main() {string s = "abcdef";auto it = s.begin() + 2;  // 指向 'c's.erase(it);  // 删除 'c',变成 "abdef"cout << s << endl;  // 输出: "abdef"return 0;
}

3. erase(iterator first, iterator last)
  • 作用:删除 [first, last) 范围内的字符(包括 first,不包括 last)。
  • 参数
    • first:起始迭代器。
    • last:结束迭代器(不删除)。
  • 返回值:指向删除区域后第一个剩余字符的迭代器。

示例

#include <iostream>
#include <string>using namespace std;int main() {string s = "abcdef";s.erase(s.begin() + 1, s.begin() + 4);  // 删除索引 1~3 ('b' 到 'd'),变成 "aef"cout << s << endl;  // 输出: "aef"return 0;
}

4. erase-remove惯用法(高效删除多个字符)

在 C++ 里,erase 不能直接删除多个 指定字符,但可以配合 std::remove_if 实现 高效批量删除

示例

#include <iostream>
#include <string>
#include <algorithm>using namespace std;int main() {string s = "abcdebc";// 删除所有 'b's.erase(remove(s.begin(), s.end(), 'b'), s.end());cout << s << endl;  // 输出: "acdec"return 0;
}

原理

  1. remove 把所有 非 ‘b’ 的字符 重新排列到前面,返回新末尾的迭代器。
  2. erase 删除新末尾之后的所有字符,完成去重。

总结

用法作用适用场景
erase(pos, len)pos 开始删除 len 个字符删除固定范围的子串
erase(iterator pos)删除 pos 迭代器指向的字符通过迭代器删除单个字符
erase(iterator first, iterator last)删除 [first, last) 范围字符通过迭代器删除一段子串
erase(remove_if(...))批量删除多个指定字符需要高效删除多个字符

#include<iostream>
using namespace std;
#include<string>
#include<vector>
void dele(string& s1, string& s2)
{int n2 = s2.size();if (n2 < 1)return;for (int i = 0;i < s1.size();i++){for (int j = 0;j < n2;j++){if (s1[i] == s2[j]){s1.erase(i,1);i--;}}}}
int main()
{string s1, s2;cin >> s1 >> s2;dele(s1, s2);cout << s1 << endl;
}

2unordered_set存储s2

unordered_set使用

是 C++ STL 提供的 无序集合,底层使用 哈希表 实现,支持 O(1) 平均时间复杂度 进行 插入、删除、查找

1. 基本用法

(1) 头文件

#include <unordered_set>

(2) 声明

unordered_set<int> mySet;  // 存储 int 类型
unordered_set<string> mySet2;  // 存储 string 类型

(3) 插入

mySet.insert(10);
mySet.insert(20);

(4) 遍历

for (int num : mySet) {  cout << num << " ";  
}

⚠️ 注意: unordered_set 无序存储,遍历顺序 不确定


2. 主要操作

(1) 插入元素

unordered_set<int> mySet;
mySet.insert(1);
mySet.insert(2);

(2) 查找元素

if (mySet.find(2) != mySet.end()) {  cout << "2 存在" << endl;  
} else {  cout << "2 不存在" << endl;  
}

find(x) != end() 表示 找到 x,否则 未找到

(3) 删除元素

mySet.erase(2);  // 删除值为 2 的元素

(4) 统计个数

cout << mySet.size() << endl;

(5) 清空集合

mySet.clear();

3. 高级用法

(1) 使用 count(x) 判断是否存在

if (mySet.count(2)) {cout << "2 存在" << endl;
} else {cout << "2 不存在" << endl;
}

count(x) 返回 0(不存在)或 1(存在)。

(2) 迭代器遍历

for (auto it = mySet.begin(); it != mySet.end(); ++it) {cout << *it << " ";
}

4. 时间复杂度

操作平均时间复杂度
insert(x)O(1)
find(x)O(1)
erase(x)O(1)
遍历O(n)

5. 适用场景
快速去重unordered_set 可以存储唯一值,O(1) 查找是否存在
快速查找:比 vectorset 更快的 O(1) 查找
集合操作:常用于 字符删除、重复元素去除 等。


  • 使用 unordered_set 来加速字符匹配,使得删除操作的时间复杂度从优化到 O(n)。
  • set的查询速度为O(1)
  • 使用writeIndex保存s1要写入的位置
  • 在存储s2的set里面查询,是否存在对应字符,不存在的写入s1前面
  • 删掉后续字符
#include <iostream>
#include <string>
#include <unordered_set>using namespace std;void dele(string& s1, const string& s2) {unordered_set<char> removeSet(s2.begin(), s2.end()); // Step 1: 用哈希表存储 s2 中所有字符int writeIndex = 0; // Step 2: 记录写入位置for (int i = 0; i < s1.size(); i++) { // Step 3: 遍历 s1if (!removeSet.count(s1[i])) { // Step 4: 如果 s1[i] 不在 s2 中s1[writeIndex++] = s1[i]; // Step 5: 把 s1[i] 写入当前位置}}s1.erase(writeIndex); // Step 6: 删除剩余部分
}int main() {string s1, s2;cin >> s1 >> s2;dele(s1, s2);  // 调用删除函数cout << s1 << endl;  // 输出修改后的 s1return 0;
}
http://www.dtcms.com/a/568036.html

相关文章:

  • 网站程可以自己做吗c语言 网站开发
  • 太仓建设银行网站微信小程序加盟
  • 站酷海报设计图片深圳医疗网站建设
  • 网站建设需要多少技术各省住房和城乡建设厅网站
  • jquery 网站模板如何制作网页图
  • 建论坛网站seo搜索引擎优化课后答案
  • 网站建设行业知乎免费课程网站有哪些
  • 360怎么变成建设银行首选网站网站建设的空间是什么
  • 网站一次性链接怎么做怎么做高端品牌网站设计
  • 网站后台管理规定美食网站开发与设计文献综述
  • 陕西网站建设哪家专业广西住房和建设厅官网
  • 投资做网站网络服务器的配置与应用心得
  • 重庆有哪些科技骗子公司wordpress4.7优化
  • 如何高效建设品牌网站如何下载wordpress
  • 卡片形式的网站wordpress 批量标签
  • wordpress纯代码下载seo搜索引擎优化薪资
  • 销售网站开发步骤网站以什么名字备案
  • 大宗贸易采购平台福州网站怎么做seo
  • 淘宝的网站怎么做内容营销策略
  • 成都网站定制wordpress ip_hash失效
  • 门户网站还能建设么cida室内设计师资格证
  • 企业网站建设具体步骤网站建设中 怎么办
  • 承接网站建设服务一个公司可以做几个网站吗
  • windows 网站模板热点新闻素材
  • 积分兑换商城网站建设随州企业网络推广怎么做
  • 建设营销网站多少钱wordpress启用主题404
  • 玩具 东莞网站建设 技术支持建设现金分期网站
  • 网站推广打包爱网站关键词挖掘工具
  • 做网站费是多少政务网站设计鉴赏
  • 单位网站备案交友免费网站建设