- STL基本概念
STL称为标准模板库,广义上讲分为容器、算法、迭代器。容器和算法是通过迭代器进行无缝连接,STL几乎所以代码都采用了模板类或者模板函数。
1.1STL六大组件
容器、算法、迭代器件、仿函数、适配器、空间适配器
- vector容器
#include <iostream>
#include <vector>
#include <algorithm>//算法头文件
using namespace std;
//回调函数
void myPrint(int val)
{
cout << val << endl;
}
void test01()
{
//创建vector容器对象,并且通过模板参数指定容器中存放的数据类型
vector<int> v;
//向容器中放数据
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
//每个容器都有自己的迭代器,迭代器是用来遍历容器中的元素
//v.begin():返回迭代器,这个迭代器指向容器中第一个数据
//v.end():返回迭代器,这个迭代器指向容器中最后一个数据的下一个位置
//vector<int>::iterator :拿到vector<int>这种容器迭代器的类型
//通过迭代器访问容器中的数据
vector<int>::iterator pBegin = v.begin();//创建起始迭代器,指向容器中第一个元素
vector<int>::iterator pEnd = v.end();
//第一种遍历方式:
while (pBegin != pEnd)
{
cout << *pBegin << endl;
pBegin++;
}
//第二种遍历方式:
for(vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << endl;
}
//第三种遍历方式:使用STL提供的标准算法
for_each(v.begin(),v.end(),myPrint);
}
int main()
{
test01();
return 0;
}
2.1 vector存放自定义数据类型
示例:
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
class Person
{
public:
Person(string name, int age)
{
this->m_age = age;
this->m_name = name;
}
int m_age;
string m_name;
};
void test01()
{
vector<Person> v;
Person p1("aaa",10);
Person p2("bbb",20);
Person p3("ccc",30);
Person p4("ddd",40);
Person p5("eee",50);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
//迭代器it可以看作是Person类型的指针(即*it为<>里的类型)
for(vector<Person>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "姓名:" << (*it).m_name << "年龄:" << (*it).m_age << endl;
cout << "姓名:" << it->m_name << "年龄:" << it->m_age << endl;
}
}
void test02()
{
vector<Person *> v;
Person p1("aaa",10);
Person p2("bbb",20);
Person p3("ccc",30);
Person p4("ddd",40);
Person p5("eee",50);
v.push_back(&p1);
v.push_back(&p2);
v.push_back(&p3);
v.push_back(&p4);
v.push_back(&p5);
//迭代器it可以看作是Person *类型的指针
for(vector<Person *>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "姓名:" << (*it)->m_name << "年龄:" << (*it)->m_age << endl;
}
}
int main(void)
{
test01();
test02();
return 0;
}
- vector容器嵌套
示例:
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
void test01()
{
//创建一个大容器
vector<vector<int>> v;
//创建小容器
vector<int> v1;
vector<int> v2;
vector<int> v3;
vector<int> v4;
//向小容器中添加数据
for(int i=0; i<4; i++)
{
v1.push_back(i+1);
v2.push_back(i+2);
v3.push_back(i+3);
v4.push_back(i+4);
}
//将小容器插入到大容器中
v.push_back(v1);
v.push_back(v2);
v.push_back(v3);
v.push_back(v4);
//通过大容器,把所有数据遍历一遍
for(vector<vector<int>>::iterator it = v.begin(); it != v.end(); it++)
{
//(*it)代表vector<int>,也就是小容器
for(vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++)
{
cout << *vit << "";
}
cout << endl;
}
}
int main(void)
{
test01();
return 0;
}
- string基本概念
string是C++风格的字符串,而string本质上是一个类
string和char * 的区别是string是一个类,类内部封装了char *,管理这个字符串,是一个char *型的容器,而char * 就算一个字符指针。
特点:string类内部封装了很多成员方法。
例如:查找find,拷贝copy,删除delete,替换replace,插入insert
3.1 string构造函数
string();
string(const char * s);
string(const string &str);
string(int n,char c); //使用n个字符c初始化
string的赋值方式:示例
#include <iostream>
#include <string>
using namespace std;
void test01()
{
string str1 = "hello C++";
string str2 = str1;
string str3;//不能直接=
str3 = 'c';
string str4;//不能直接.assign
str4.assign("hello C++");
string str5;
str5.assign("hello C++", 5);//显示前5个字符
string str6;
str6.assign(str5);
string str7;
str7.assign(10,'w');//10个w组成的字符串
cout << "str1 = " << str1 << endl;
cout << "str2 = " << str2 << endl;
cout << "str3 = " << str3 << endl;
cout << "str4 = " << str4 << endl;
cout << "str5 = " << str5 << endl;
cout << "str6 = " << str6 << endl;
cout << "str7 = " << str7 << endl;
}
int main(void)
{
test01();
return 0;
}
string字符串拼接:示例
#include <iostream>
#include <string>
using namespace std;
void test01()
{
string str1 = "我";
str1 += "爱玩游戏";
str1 += ":";
string str2 = "LOL DNF";
str1 += str2;
cout << str1 << endl;
string str3 = "I";
str3.append("love");
str3.append("game abcd",4);//拼接前四个字符
str3.append(str2);//拼接字符串str2
str3.append(str2,4,3);//从第三个字符开始拼接后面3个字符
cout << str3 << endl;
}
int main(void)
{
test01();
return 0;
}
string查找和替换
#include <iostream>
#include <string>
using namespace std;
//查找函数
void test01()
{
string str1 = "adahjqwhj";
int pos = str1.find("hj");//返回所在下标(0开始数)
if(pos != -1)
{
cout << "查找成功:pose = " << pos << endl;
}
else
{
cout << "查找失败" << endl;
}
pos = str1.rfind("hj");
cout << "查找成功:pose = " << pos << endl;//重后向前查看返回i所在下标
}
//替换函数
void test02()
{
string str1 = "zhang";
str1.replace(1, 3, "11111");//重1号位置起3个字符替换为111
cout << "str1 = " << str1 << endl;//结果z11111g
}
int main(int argc, char const *argv[])
{
test01();
test02();
return 0;
}
string的比较:主要用于比较两个字符串是否相等
#include <iostream>
#include <string>
using namespace std;
void test01()
{
string str1 = "hello";
string str2 = "hezo";
if(str1.compare(str2) == 0)
{
cout << "str1等于str2" << endl;
}
else if(str1.compare(str2) ==1)
{
cout << "str1大于str2" << endl;
}
else
{
cout << "str1小于str2" << endl;
}
}
int main(void)
{
test01();//str1小于str2
return 0;
}
string字符存取
#include <iostream>
#include <string>
using namespace std;
void test01()
{
string str1 = "hello";
//通过[]访问单个字符
for(int i = 0; i < str1.size(); i++)
{
cout << str1[i] << " ";
}
cout << endl;
//通过at方式访问单个字符
for(int i = 0; i < str1.size(); i++)
{
cout << str1.at(i) << " " ;
}
cout << endl;
//修改单个字符
str1[1] = 'z';
str1.at(2) = 'z';
cout << str1 << endl;
}
int main(void)
{
test01();
return 0;
}
string字符插入和删除
#include <iostream>
#include <string>
using namespace std;
void test01()
{
string str1 = "hello";
str1.insert(1,"zzh");//重第一个位置插入字符串“zzh”
cout << "str1 = " << str1 << endl;
str1.erase(1,3);//重第一个位置起删3个字符
cout << "str1 = " << str1 << endl;
}
int main(void)
{
test01();
return 0;
}
string子串
#include <iostream>
#include <string>
using namespace std;
void test01()
{
string str1 = "hello";
string subStr = str1.substr(1, 3);//重第一个位置截取三个字符
cout << "subStr = " << subStr << endl;//ell
}
void test02()
{
//截取邮箱人名
string email = "zhangsan@sina.com";
// int pos = email.find("@");
// string name = email.substr(0,pos);
string name = email.substr(0,email.find('@'));
cout << name << endl;
}
int main(void)
{
test01();
test02();
return 0;
}