网站首页 > 资源文章 正文
stl基本知识
#include<iostream>
using namespace std;
#include"vector"
#include"algorithm"
class Teacher
{
public:
int age;
char name[64];
public:
void printT()
{
cout << "age:" << age << endl;
}
};
void main11()
{
Teacher t1, t2, t3;
t1.age = 31;
t2.age = 32;
t3.age = 33;
Teacher *p1, *p2, *p3;
p1 = &t1;
p2 = &t2;
p3 = &t3;
vector<Teacher*> v1;//把你的元素拷贝到容器中
v1.push_back(p1);
v1.push_back(p2);//容器实现了 数据类型和算法的有效分离
v1.push_back(p3);//添加的时候已经复制到容器里面了
//迭代器相当于一个指针
for (vector<Teacher*>::iterator it = v1.begin(); it != v1.end(); it++)//迭代器
{
cout << (*it)->age <<endl;
}
int num1= count(v1.begin(), v1.end(), 3);
cout << "num1:" << num1 << endl;
}
void main()
{
main11();
cout << "hello" << endl;
system("pause");
return;
}
string 和char*比较
1)string是一个类,char*是一个指向字符的指针
string封装了char*,管理这个字符串,是一个char*型的容器
2)string不用考虑内存释放和越界
string管理char*所分配的内存,每一次string的复制,取值都由string 类复制维护,所以不用担心复制越界和取值越界
3)string提供了一系列的字符串操作函数
void main11()
{
string s1 = "aaaa";
string s2("bbbb");
string s3 = s2;//通过拷贝构造函数 来初始化对象
string s4(10, 'a');
cout << "s1:" << s1 << endl;
cout << "s2:" << s2 << endl;
cout << "s3:" << s3 << endl;
cout << "s4:" << s4 << endl;
}
//字符串的遍历
void main22()
{
string s1 = "asdgfgh";
//重载了数组操作符 可以用数组方式遍历
for (int i = 0; i < s1.length(); i++)
{
cout << s1[i] << " ";
}
//迭代器
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) << " ";//抛出异常
}
}
//字符指针和string 的转换
void main23()
{
string s1 = "aaabbbbbb";
printf("s1:%s\n", s1.c_str());//s1=>char *
char buf1[128];
s1.copy(buf1, 3, 0);//s1=>buf
cout << "buf1:" << buf1 << endl;
}
//字符串的连接
void main24()
{
string s1 = "aaaa";
string s2 = "bbbb";
s1 = s1 + s2;
cout << s1 << endl;
string s3 = "333";
string s4 = "444";
s3.append(s4);
cout << "s3:" << s3 << endl;
}
void main25()//查找和替换
{
string s1 = "scd hello scd 222 111 scd";
// 第一次出现scd的下标
int index = s1.find("scd", 0);
cout << "index:" << index << endl;
//求scd出现的次数 第一次出现的数组下标
int offindex=s1.find("scd", 0);
while (offindex != string::npos)
{
cout << "offindex:" << offindex << endl;
offindex = offindex + 1;
offindex= s1.find("scd", offindex);
}
string s3 = "aaa bbb ccc";
s3.replace(0, 3, "AAA");
cout << "s3" << s3 << endl;
offindex = s1.find("scd", 0);
while (offindex != string::npos)
{
cout << "offindex:" << offindex << endl;
offindex = offindex + 1;
offindex = s1.find("scd", offindex);
}
cout << "s3" << s3 << endl;
}
void main26()//区间删除和插入
{
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;
string s2 = "BBB";
s2.insert(0, "AAA");//头插
s2.insert(s2.length(),"CCC");//尾插
cout << s2 << endl;
}
void main27()//算法基本用法
{
string s1 = "AAAbbb";
//函数入口地址 函数对象 预定义的函数对象
transform(s1.begin(), s1.end(), s1.begin(),toupper);
cout << s1 << endl;
string s2 = "AAAbbb";
transform(s1.begin(), s1.end(), s1.begin(), tolower);
cout << "s2:" << s2 << endl;
}
- 上一篇: 掼蛋技巧实战:保留变形木板!学会这个技巧打掼蛋所向披靡
- 下一篇: C++ STL之(容器算法迭代器)
猜你喜欢
- 2024-11-18 第03章-linux常用命令
- 2024-11-18 Python的文件处理
- 2024-11-18 Redash中文版配置MySQL数据源
- 2024-11-18 域名批量查询功能常用查询方法教程
- 2024-11-18 js中的正则表达式入门
- 2024-11-18 C++ string/stack/list/deque/multiset 小结
- 2024-11-18 C++ STL之(容器算法迭代器)
- 2024-11-18 微信小程序开发-自定义评论列表组件
- 2024-11-18 掼蛋技巧实战:保留变形木板!学会这个技巧打掼蛋所向披靡
- 2024-11-18 沈阳精品无抵消靓号 大厅过户
你 发表评论:
欢迎- 05-23如何制作含字母的流水条码
- 05-23NiceLabel又高出新境界,一分钟学会设计第一个标签
- 05-23商业设计中字体设计的创意方法
- 05-23条码软件如何调整条码数据的样式
- 05-23可变数据软件中条形码的制作步骤
- 05-23常用条形码类型组成及使用说明2
- 05-23条形码下面数据字号不一样大的UPC-A码怎么制作
- 05-23强烈推荐!这四款条形码生成工具超好用!
- 最近发表
- 标签列表
-
- 电脑显示器花屏 (79)
- 403 forbidden (65)
- linux怎么查看系统版本 (54)
- 补码运算 (63)
- 缓存服务器 (61)
- 定时重启 (59)
- plsql developer (73)
- 对话框打开时命令无法执行 (61)
- excel数据透视表 (72)
- oracle认证 (56)
- 网页不能复制 (84)
- photoshop外挂滤镜 (58)
- 网页无法复制粘贴 (55)
- vmware workstation 7 1 3 (78)
- jdk 64位下载 (65)
- phpstudy 2013 (66)
- 卡通形象生成 (55)
- psd模板免费下载 (67)
- shift (58)
- localhost打不开 (58)
- 检测代理服务器设置 (55)
- frequency (66)
- indesign教程 (55)
- 运行命令大全 (61)
- ping exe (64)
本文暂时没有评论,来添加一个吧(●'◡'●)