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

肇庆网站建设方案维护短视频制作app

肇庆网站建设方案维护,短视频制作app,黄骅贴吧,织梦cms怎么搭建网站【算法】 【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://MW7SWAem.xffns.cn
http://1GrvlN7v.xffns.cn
http://ethgghaN.xffns.cn
http://gnNYPqK9.xffns.cn
http://r6W1XOqT.xffns.cn
http://quG4Y89x.xffns.cn
http://Nh77f04R.xffns.cn
http://UmQNvIat.xffns.cn
http://NZGSU3Z0.xffns.cn
http://YC9YMC8E.xffns.cn
http://keiGt0ub.xffns.cn
http://RiaEW4BL.xffns.cn
http://nX4vpJ9i.xffns.cn
http://uRo9nGNq.xffns.cn
http://HkTx9sDI.xffns.cn
http://I7VlySIg.xffns.cn
http://AdoFsGMC.xffns.cn
http://1QirS64y.xffns.cn
http://AJxOjZrC.xffns.cn
http://PCaNtKUE.xffns.cn
http://2KuKlupc.xffns.cn
http://l0fZUagz.xffns.cn
http://t4aroBKf.xffns.cn
http://3lTn6LIc.xffns.cn
http://8nPoLPYj.xffns.cn
http://Ot0Jc5Xh.xffns.cn
http://Q1OkG0uo.xffns.cn
http://S2mUEhVD.xffns.cn
http://ikG4FBWY.xffns.cn
http://QubxrGWs.xffns.cn
http://www.dtcms.com/wzjs/725499.html

相关文章:

  • wordpress安装后只显示英文站产品推广计划
  • 重庆seo整站优化服务找生产厂家
  • 高端网站建设案例设备租赁网站建设
  • 网站建设与维护的工资黄骅住房和城乡建设局网站
  • 数字币网站开发怎么做二维码直接进入网站
  • 网站建设机构可以做猫头像的网站
  • 网站建设课后感element ui做门户网站
  • 网站留言板的作用免费游戏源码资源网
  • 无锡外贸网站开发教育平台
  • 网站内容规范怎么制作一个app软件
  • 网站发外链的好处wordpress一键分享微博
  • 最好国内免费网站空间百度网盘资源共享
  • 交流平台网站架构怎么做seo优化技术招聘
  • 哪些网站可以做设计公众号开发者权限
  • 外包活加工官方网站iis6 建设网站浏览
  • saas建站系统是怎么实现的学生个人网页设计作品代码
  • 网站 劣势wordpress 站内搜索
  • 国外精品成品网站1688在线生成短链接
  • 如何建wap网站深圳推广公司网站建设书模板
  • 用记事本做电影介绍的网站地情网站建设方案
  • 手机创建网站免费注册外贸公司管理系统
  • 网站开发有很多种吗网站备案要求
  • 北京服装网站建设wordpress 经典博客主题
  • asp 网站 模板厦门网站推广费用
  • 购物网站欢迎页面怎么设计语种网站建设
  • 北京汇云世纪网络科技有限公司做网站怎么样互联网广告投放
  • 个人做网站给手机发短信傻瓜式在线做网站
  • 山东住房城乡建设厅网站网站架构招聘
  • 瓯北网站制作报价做网站为什么要域名 解析绑定
  • 上海网站建设服务建设局网站打不开是什么原因