C++ string/stack/list/deque/multiset 小结
liuian 2025-07-07 20:08 33 浏览
1.string基础操作(包括初始化,遍历,替换,增删改查等基础操作)
2.stack基础操作(包括出栈,入栈,栈顶元素的获取和弹出)
3.list 基础操作(如何高效地进行插入删除操作)
4.deque基础操作(排序和查找)
5.multiset基础操作(解决了set不能同一个值出现的问题)
示例代码1 string基础操作
#include<iostream>
using namespace std;
void main11()
{
//初始化方式
string s1 = "aaaa";
string s2("aaaa");
string s3 = s2;
string s4 (10, 'a');
cout << "s1:" << s1 << endl;
cout << "s2:" << s2 << endl;
cout << "s3:" << s3 << endl;
cout << "s4:" << s4 << endl;
}
//string 的遍历
void main22()
{
//1 数组方式
string s1 = "asddfff";
for (int i = 0; i < s1.length(); i++)
{
cout << a[i] << "";
}
//2 迭代器
for (string::iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << "";
}
cout << endl;
for (int i = 0; i < s1.length(); i++)
{
cout << s1.at(i) << "";//抛出异常
}
cout << endl;
}
//字符指针和string 转换
void main33()
{
string s1 = "aaa";
printf("s1:%s\n", s1.c_str());
//s1的内容拷贝到buf中
char buf1[128];//分配内存
s1.copy(buf1, 3, 0);//只会拷贝三个字符,不会变成c风格的字符串
cout << "buf1:" << buf1 << endl;
}
//连接字符串
void main44()
{
string s1 = "aaa";
string s2 = "bbb";
s1 = s1 + s2;
cout << s1 << endl;
string s3 = "999";
string s4 = "4444";
s3.append(s4);
cout << "s3:" << s3 << endl;
}
//字符串的查找和替换
void main55()
{
string s1 = "sdf 555 sdgdf4564dfgtysrtw4tgrsghtyju7io869i7eu5ewtgrs";
int index = s1.find("555", 0);
cout << "index:" << index << endl;//位置下标,从0开始
int offindex = s1.find("555", 0);
while (offindex != string::npos)//出现的次数,数组下标
{
cout << "offindex" << offindex << endl;
offindex = offindex + 1;
s1.find("555", offindex);
}
//案例2 替换
string s3 = "aaa bbb ccc";
s3.replace(0, 3, "AAA");
cout << "s3" << s3 << endl;
offindex = s1.find("555", 0);
while (offindex != string::npos)//出现的次数,数组下标
{
cout << "offindex" << offindex << endl;
s1.replace(offindex, 3, "555");
offindex = offindex + 1;
s1.find("555", offindex);
}
cout << "s1替换后的结果:" << s1 << endl;
}
//区间删除和插入
void main66()
{
string s1 = "hello1 hello2 hello1";
string::iterator it = find(s1.begin(), s1.end(), '1');
if (it != s1.end())
{
s1.erase(it);
}
cout << "s1删除以后的结果" <<s1<< endl;
s1.erase(s1.begin(), s1.end());
cout << "s1全部删除:" << s1 << endl;
cout << "s1长度:" << s1.length() << endl;
}
void main77()
{
string s1 = "AAAbbb";
//函数的入口地址 函数对象 预定义的函数对象
transform(s1.begin(), s1.end(), s1.begin() ,tuopper);
cout << s1 << endl;
string s2 = "AAAbbb";
transform(s1.begin(), s1.end(), s2.begin(), tulower);
cout << "s2" << s2 << endl;
}
void main()
{
main11();
main22();
main33();
main44();
main55();
main66();
cout << "hello..." << endl;
system("pause");
return;
}
示例代码2 stack基础操作
#include<iostream>
using namespace std;
#include"stack"
//栈的模型
void main11()
{
stack<int> s;
//入栈
for (int i = 0; i < 10; i++)
{
//出栈
while (!s.empty())
{
int tmp = s.top();//获取栈顶元素
cout << tmp << "";
s.top();//弹出栈顶元素
}
}
class Teacher
{
public:
int age;
char name[32];
public:
void printT()
{
cout << "age:" << age << endl;
}
};
void main22()
{
Teacher t1, t2, t3;
t1.age = 31;
t2.age = 32;
t3.age = 33;
stack<Teacher> s;
s.push(t1);
s.push(t2);
s.push(t3);
while (!s.empty())
{
Teacher tmp = s.top();//获取栈顶元素
tmp.printT();
s.top();//弹出栈顶元素
}
void main33()
{
Teacher t1, t2, t3;
t1.age = 31;
t2.age = 32;
t3.age = 33;
stack<Teacher*> s;
s.push(&t1);
s.push(&t2);
s.push(&t3);
while (!s.empty())
{
Teacher *p = s.top();//获取栈顶元素
p->printT();
s.top();//弹出栈顶元素
}
void main()
{
main11();
main22();
main33();
cout << "hello..." << endl;
system("pause");
return;
}
示例代码 3 list 基础操作
/*
1 list是一个双向列表容器,可以高效地进行插入删除操作(左闭右开)
2 list不可以随机存取元素,所以不支持at.(pos)函数与[]操作符
#include<list>
*/
#include<iostream>
using namespace std;
#include"list"
void mian11()
{
list < int> l;
cout << "list的大小" << l.size() << endl;//测试
for (int i = 0; i < 10; i++)
{
l.push_back(i);//尾插法
}
cout << "" << l.size() << endl;
list<int>::iterator it = l.begin();//迭代器
while (it != l.end())
{
cout << *it << "";
it++;
}
it = l.begin();//list不能随机访问
it++;
it++;
it++;
list.insert(it, 100);//100插入在哪里?
for (list<int>::iterator it = l.begin(); it != l.end(); it++)
{
cout << *it << endl;
}
//结论:链表的结点index序号是从0号位置开始的.在3号位置插入元素,原来的三号位置,变成4号位置.
}
void main33()
{
list < int> l;
cout << "list的大小" << l.size() << endl;//测试
for (int i = 0; i < 10; i++)
{
l.push_back(i);//尾插法
}
cout << "" << l.size() << endl;
list<int>::iterator it1 = l.begin();
list<int>::iterator it2 = l.begin();
it2++;
it2++;
it2++;
l.erase(it1, it2);
for (list<int>::iterator it = l.begin(); it != l.end(); it++)
{
cout << *it << endl;
}
cout << endl;
//插入和删除
l.insert(l.begin(), 100);
l.insert(l.begin(), 100);
l.insert(l.begin(), 100);
l.erase(l.begin());
l.remove(100);
}
void main()
{
//main11();
///main33();
cout << "hello..." << endl;
system("pause");
return;
}
示例代码4 deque基础操作
#include<iostream>
using namespace std;
#include"deque"
#include"algorithm"
void printD(deque<int>&d)
{
for (deque<int>::iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << endl;
}
}
void main11()
{
deque<int> d1;
d1.push_back(1);
d1.push_back(3);
d1.push_back(5);
d1.push_front(-11);
d1.push_front(-33);
d1.push_front(-55);
cout << "头部元素" << d1.front << endl;
cout << "尾部元素" <<d1.back<< endl;
printD(d1);
d1.pop_front();
d1.pop_back();
printD(d1);
//查找-33数组下标的值
deque<int>::iterator it = find(d1.begin(), d1.end(), -33);
if (it != d1.end())
{
cout << "-33数组下标是" << distance(d1.begin(), it) << endl;
}
else
{
cout << "没有找到值为-33的元素" << endl;
}
}
void main()
{
main11();
cout << "hello..." << endl;
system("pause");
return;
}
示例代码5multiset基础操作
#include<iostream>
using namespace std;
#include<queue>
#include"list"
#include"set"
void main11()
{
multiset<int> setl;
int tmp = 0;
printf("请输入multiset集合的值:");
scanf("&d", &tmp);
while (tmp != 0)
{
setl.insert(tmp);
printf("请输入multiset集合的值:");
scanf("&d", &tmp);
}
for (multiset<int>::iterator it = setl.begin(); it != setl.end(); it++)
{
cout << *it << " ";
}
cout << endl;
while (!setl.empty())
{
multiset<int>::iterator it = setl.begin();
cout << *it << " ";
setl.erase(it);
}
}
void main()
{
main11();
cout << "hello..." << endl;
system("pause");
return;
}
予人玫瑰,手有余香.小羊伴你一路同行~~~
相关推荐
- Python中的列表详解及示例_python列表讲解
-
艾瑞巴蒂干货来了,数据列表,骚话没有直接来吧列表(List)是Python中最基本、最常用的数据结构之一,它是一个有序的可变集合,可以包含任意类型的元素。列表的基本特性有序集合:元素按插入顺序存储可变...
- PowerShell一次性替换多个文件的名称
-
告别繁琐的文件重命名,使用PowerShell语言批量修改文件夹中的文件名,让您轻松完成重命名任务在日常工作中,我们经常需要对大量文件进行重命名,以便更好地管理和组织。之前,我们曾介绍过使用Pytho...
- 小白必看!Python 六大数据类型增删改查秘籍,附超详细代码解析
-
在Python中,数据类型可分为可变类型(如列表、字典、集合)和不可变类型(如字符串、元组、数值)。下面针对不同数据类型详细讲解其增删改查操作,并给出代码示例、输出结果及分析总结。1.列表(Li...
- python数据容器之列表、元组、字符串
-
数据容器分为5类,分别是:列表(list)、元组(tuple)、字符串(str)、集合(set)、字典(dict)list#字面量[元素1,元素2,元素3,……]#定义变量变量名称=[元素1,元素...
- python列表(List)必会的13个核心技巧(附实用方法)
-
列表(List)是Python入门的关键步骤,因为它是编程中最常用的数据结构之一。以下是高效掌握列表的核心技巧和实用方法:一、理解列表的本质可变有序集合:可随时修改内容,保持元素顺序混合类型:一个列表...
- 如何利用python批量修改文件名_python如何对文件进行批量命名
-
很多语言都可以做到批量修改文件名,今天我就给大家接受一下Python的方法,首选上需求。图片中有10个txt文件,现在我需要在这些文件名的前面全部加一个“学生”,可以吗?见证奇迹的时刻到了。我是怎么做...
- Python中使用re模块实现正则表达式的替换字符串操作
-
#编程语言#我是"学海无涯自学不惜!",关注我,一同学习简单易懂的Python编程。0基础学python(83)Python中,导入re模块后还可以进行字符串的替换操作,就是sub()...
- python列表十大常见问题,你遇到第几个?
-
Python列表常见问题及解决方案1.修改列表时的常见陷阱问题:在遍历时修改列表#错误做法:在遍历时删除元素会导致意外结果numbers=[1,2,3,4,5,6]forn...
- python入门007:编辑列表_python列表怎么写入文件
-
一、列表的编辑操作列表创建后,随着程序的运行,可以通过对列表元素的增删改操作来编辑列表。1、修改列表元素的值修改列表元素的操作方法与访问列表元素的方法类似。例如,要修改列表元素的值,先指定列表及元素...
- Python教程:在python中修改元组详解
-
欢迎你来到站长在线的站长学堂学习Python知识,本文学习的是《在Python中修改元组详解》。本知识点主要内容有:在Python中直接使用赋值运算符“=”给元组重新赋值、在Python中使用加赋值运...
- Python列表(List)一文全掌握:核心知识点+20实战练习题
-
Python列表(List)知识点教程一、列表的定义与特性定义:列表是可变的有序集合,用方括号[]定义,元素用逗号分隔。list1=[1,"apple",3.14]lis...
- Python教程-列表复制_python对列表进行复制
-
作为软件开发者,我们总是努力编写干净、简洁、高效的代码。Python列表是一种多功能的数据结构,它允许你存储一个项目的集合。在Python中,列表是可变的,这意味着你可以在创建一个列表后改变它的...
- Python入门学习教程:第 6 章 列表
-
6.1什么是列表?在Python中,列表(List)是一种用于存储多个元素的有序集合,它是最常用的数据结构之一。列表中的元素可以是不同的数据类型,如整数、字符串、浮点数,甚至可以是另一个列表。列...
- Python列表、元组、字典和集合_python中的列表元组和字典
-
Python中的列表(List)、元组(Tuple)、字典(Dict)和集合(Set)是四种最常用的核心数据结构。掌握它们的基础操作只是第一步,真正发挥威力的是那些高级用法和技巧。首先我们先看一下这...
- 学习编程第167天 python编程 使用format方法灵活替换字符串
-
今天学习的是刘金玉老师零基础Python教程第51期,主要内容是python编程使用format方法灵活替换字符串。一、format方法(一)format方法是字符串自带的方法,使用的format方法...
- 一周热门
- 最近发表
- 标签列表
-
- python判断字典是否为空 (50)
- crontab每周一执行 (48)
- aes和des区别 (43)
- bash脚本和shell脚本的区别 (35)
- canvas库 (33)
- dataframe筛选满足条件的行 (35)
- gitlab日志 (33)
- lua xpcall (36)
- blob转json (33)
- python判断是否在列表中 (34)
- python html转pdf (36)
- 安装指定版本npm (37)
- idea搜索jar包内容 (33)
- css鼠标悬停出现隐藏的文字 (34)
- linux nacos启动命令 (33)
- gitlab 日志 (36)
- adb pull (37)
- python判断元素在不在列表里 (34)
- python 字典删除元素 (34)
- vscode切换git分支 (35)
- python bytes转16进制 (35)
- grep前后几行 (34)
- hashmap转list (35)
- c++ 字符串查找 (35)
- mysql刷新权限 (34)