C++ STL
1.Vector 中符合数据类型,指针的存储,出现次数的统计
#include<iostream>
#include<algorithm>
#include<vector>
const int SIZE=40;
using namespace std;
typedef struct Teacher
{
int age;
int name[SIZE];
}Teacher;
void vector_int()
{
vector<int> vec;
vec.push_back(-1);
vec.push_back(1);
vec.push_back(3);
vec.push_back(5);
for(vector<int>::iterator it=vec.begin();it!=vec.end();it++)
{
cout<<*it<<" ";
}
cout<<endl;
int sum=count(vec.begin(),vec.end(),3);
cout<<"count(3)="<<sum<<endl;
}
//复合数据类型 存储
void vector_teacher()
{
Teacher t1,t2,t3;
t1.age=t2.age=t3.age=10;
vector<Teacher> vec;
vec.push_back(t1);
vec.push_back(t2);
vec.push_back(t3);
for(vector<Teacher>::iterator it=vec.begin();it!=vec.end();it++)
{
cout<<"age ,"<<it->age<<" ,name"<<it->name<<endl;
}
cout<<endl;
}
//Vector里面储存指针,取出的时候首先要解引用
void vector_teacher_pro()
{
Teacher t1,t2,t3;
t1.age=t2.age=t3.age=10;
vector<Teacher*> vec;
vec.push_back(&t1);
vec.push_back(&t2);
vec.push_back(&t3);
for(vector<Teacher*>::iterator it=vec.begin();it!=vec.end();it++)
{
cout<<"age ,"<<(*it)->age<<" ,name"<<(*it)->name<<endl;
}
cout<<endl;
}
int main()
{
vector_int();
vector_teacher();
vector_teacher_pro();
return 0;
}
2.String的常用操作
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
//string的初始化
void string_init()
{
string s1 = "aaaa";
string s2("bbbb");
string s3 = s2; //通过拷贝构造函数 来初始化对象s3
string s4(10, 'a');
cout << "s1:" << s1 << endl;
cout << "s2:" << s2 << endl;
cout << "s3:" << s3 << endl;
cout << "s4:" << s4 << endl;
}
//string的 遍历
void string_show()
{
string s1 = "abcdefg";
//1 数组方式
for (int i=0; i<s1.length(); i++)
{
cout << s1[i] << " ";
}
cout << endl;
//2 迭代器
for (string::iterator it = s1.begin(); it != s1.end(); it++ )
{
cout << *it << " ";
}
cout << endl;
//用at[i]会抛出异常
try
{
for (int i=0; i<s1.length() + 3; i++)
{
cout << s1.at(i) << " "; //抛出异常
}
}
catch ( ... )
{
cout << "发生异常\n" ;
}
cout << "at之后" << endl;
/*
try
{
for (int i=0; i<s1.length() + 3; i++)
{
cout << s1[i] << " "; //出现错误 不向外面抛出异常 引起程序的中断
}
}
catch ( ... )
{
cout << "发生异常\n" ;
}
*/
}
//字符指针和string的转换
void string_char()
{
string s1 = "aaabbbb";
//1 s1===>char *
//printf("s1:%s \n", s1.c_str());
//2 char *====>sting
//3 s1的内容 copy buf中
//char buf1[128] = {0};
//s1.copy(buf1, 3, 0); //注意 只给你copy3个字符 不会变成C风格的字符串
//cout << "buf1:" << buf1 << endl;
}
//字符串的 连接
void string_append()
{
string s1 = "aaa";
string s2 = "bbb";
s1 = s1 + s2;
cout << "s1:" << s1 << endl;
string s3 = "333";
string s4 = "444";
s3.append(s4);
cout << "s3:" << s3 << endl;
}
//字符串的查找和替换
void string_find_replace()
{
string s1 = "wbm hello wbm 111 wbm 222 wbm 333 ";
// ▲
//第一次 出现wbm index
int index = s1.find("wbm", 0); //位置下标 从0开始
cout << "index: " << index << endl;
//案例1 求wbm出现的次数 每一次出现的数组下标
int offindex = s1.find("wbm", 0);
while (offindex != string::npos)
{
cout << "offindex:" << offindex << endl;
offindex = offindex + 1;
offindex = s1.find("wbm", offindex); //wang bao ming
}
//案例2 把小写wbm===>WBM
string s3 = "aaa bbb ccc";
s3.replace(0, 3, "AAA");
cout << "s3" << s3 << endl;
offindex = s1.find("wbm", 0);
while (offindex != string::npos)
{
cout << "offindex:" << offindex << endl;
s1.replace(offindex,3, "WBM");
offindex = offindex + 1;
offindex = s1.find("wbm", offindex); //
}
cout << "s1替换后的结果: " << s1 << endl;
}
//截断(区间删除)和插入
void string_erase_insert()
{
string s1 = "hello1 hello2 hello1";
string::iterator it = find(s1.begin(), s1.end(), 'l');
if (it != s1.end() )
{
s1.erase(it);
}
cout << "s1删除l以后的结果:" << 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 string_transform()
{
string s1 = "AAAbbb";
//1函数的入口地址 2函数对象 3预定义的函数对象
transform(s1.begin(), s1.end(),s1.begin(), ::toupper);
cout << "s1" << s1 << endl;
string s2 = "AAAbbb";
transform(s2.begin(), s2.end(), s2.begin(), ::tolower);
cout << "s2:" << s2 << endl;
}
int main()
{
string_init();
string_show();
string_char();
string_append();
string_find_replace();
string_erase_insert();
string_transform();
return 0;
}