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

C++ 容器

string

1. 构造

string s1();							// 1
string s2("hello");						// hello
string s3(3, 'k');						// kkk
string s4("hellohello", 2, 4);			// lloh

2. 赋值

string s1 = "hellohello";				// hellohello
string s2.assign(s1);					// hellohello
string s3.assign(s1, 2, 4);				// lloh
string s4.assign("hellohello", 2, 4);	// lloh

3. 长度

string s1 = "hellohello";
int len = s1.size();					// 10

4. 拼接

string s1("hello"), s2("world");		
string s3 = s1 + s2;					// helloworld
string s4 = s1.append(s2);				// helloworld (此时 s1 已经改变)
string s5 = s1.append("world", 2, 2);	// helloworldrl

5. 比较

string s1("hello"), s2("world");
int res1 = (s1 < s2);					// 1
int res2 = (s1 != s2);					// 0
int res3 = s1.compare(s2);				// -15 即 ('h' - 'w')
int res3 = s1.compare(3, 1, s2, 4, 2);	// 8 即 ('l' - 'd')

6. 子串

string s1 = "helloworld";
string s2 = s1.substr(2, 4);			// llow
string s3 = s1.substr(2);				// lloworld

7. 交换

string s1("hello"), s2("world");		// hello world
s1.swap(s2);							// world hello

8. 查找

string s1 = "helloworld";
int n;
if ((n = s1.find('w')) != string::npos) {cout << n << s1.substr(n) << endl;	// 5 world
}
if ((n = s1.find("wo")) != string::npos) {cout << n << s1.substr(n) << endl;	// 5 world
}
if ((n = s1.find_first_of("l")) != string::npos) {cout << n << s1.substr(n) << endl;	// 2 lloworld
}
if ((n = s1.find_last_of("l", 3)) != string::npos) {cout << n << s1.substr(n) << endl;	// 8 ld
}
if ((n = s1.find_first_not_of("hel")) != string::npos) {cout << n << s1.substr(n) << endl;	// 4 oworld
}

9. 替换

string s1("hello"), s2("world");		// hello world
s1.replace(1, 2, "123456", 2, 3);		// h345lo
s1.replace(1, 2, 3, 'k');				// hkkk5lo	
s1.replace(1, 2, s2, 3, 2);				// hldk5lo	

10. 删除

string s1("helloworld");				// hello world
s1.erase(1, 2);							// hloworld
s1.erase(4);							// hlow

11. 插入

string s1("hello"), s2("world");		// hello world
s1.insert(3, s2, 1, 3);					// helorllo
s1.insert(2, 3, 'k');					// hekkklorllo

12. 排序

static bool cmp(char c1, char c2) {return c1 > c2;}
string s1("helloworld");
sort(s1.begin(), s1.end());				// dehllloorw
sort(s1.begin(), s1.end(), cmp);		// wroolllhed
http://www.lryc.cn/news/140322.html

相关文章:

  • 【PHP】PHP文件操作详解
  • 硬核旗舰南卡OE CC开放式耳机发布,重新定义百元开放式耳机新标杆!
  • 785. 判断二分图
  • 限时 180 天,微软为 RHEL 9 和 Ubuntu 22.04 推出 SQL Server 2022 预览评估版
  • 一款ccm的功率因素校正控制器ncp1654
  • 4.若依框架上传文件
  • Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘ are required
  • idea的debug断点的使用
  • 【UE】蓝图通信——事件分发器
  • Python爬虫分布式架构问题汇总
  • AIGC人工智能涉及三十六职业,看看有没有你的职业(一)
  • 万界星空科技/免费MES系统/免费质量检测系统
  • 解决IndexError: index 0 is out of bounds for axis 1 with size 0
  • Java中hashTable的基本介绍,细节讨论,使用注意事项,常用方法和底层的扩容机制
  • redis -实战记录
  • Mysql知识梳理
  • 文生图模型之Stable Diffusion
  • Java List循环安全删除元素
  • 2023年03月 C/C++(三级)真题解析#中国电子学会#全国青少年软件编程等级考试
  • bert-base-chinese 判断上下句
  • vue3+vue-cli使用mockjs
  • Android 全局监听软键盘弹起隐藏 动态修改布局并适配无限循环的问题
  • 第 k 小整数
  • LeetCode 1448. 统计二叉树中好节点的数目:DFS
  • AR室内导航技术之技术说明与效果展示
  • 06-Numpy基础-线性代数
  • SpringBootWeb 登录认证
  • 【JVM 内存结构丨栈】
  • LeetCode 138.复制带随机指针的链表
  • 基于SSM的小说网站的设计与实现(论文+源码)_kaic