1. STL概念
2. STL三大组件
2.1 容器
2.2 算法
2.3 迭代器
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <stdexcept>
using namespace std;
//迭代器:遍历功能
//普通指针也算迭代器
void test() {
int array[5] = { 1,3,5,6,8 };
int* p = array;//指针指向数组首地址,&array[0]
for (int i = 0; i < 5; ++i) {
//cout << array[i] << endl;
cout << *(p++) << endl;
}
}
int main(){
test();
return 0;
}
void myPrint(int v) {
cout << v << endl;
}
void test() {
//声明容器
vector<int> v;//存放int型数据的容器
//插入数据
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
//使用迭代器遍历容器中的元素
vector<int>::iterator it = v.begin();//it指向容器v的起始位置
vector<int>::iterator end = v.end();//end指向容器v的最后位置的下一个位置
//第一种方式
while (it != end) {
cout << *it << endl;//对指针it解引用
it++;
}
//第二种方式
//for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
// cout << *it << endl;
//}
//第三种方式
//for_each(v.begin(), v.end(), myPrint);
}
//操作自定义数据类型
class Person {
public:
Person(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
void test() {
vector<Person> v;
Person p1("大头儿子", 10);
Person p2("小头爸爸", 20);
Person p3("隔壁王叔叔", 30);
Person p4("围裙妈妈", 28);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
for (vector<Person>::iterator it = v.begin(); it != v.end(); ++it) {
cout << "姓名:" << (*it).m_Name << " 年龄:" << it->m_Age << endl;
}
}
//存放自定义数据的指针
class Person {
public:
Person(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
void test() {
vector<Person *> v;
Person p1("大头儿子", 10);
Person p2("小头爸爸", 20);
Person p3("隔壁王叔叔", 30);
Person p4("围裙妈妈", 28);
v.push_back(&p1);
v.push_back(&p2);
v.push_back(&p3);
v.push_back(&p4);
for (vector<Person*>::iterator it = v.begin(); it != v.end(); ++it) {
cout << "姓名:" << (*it)->m_Name << " 年龄:" << (*it)->m_Age << endl;
}
}
void test() {
vector<vector<int>> v;
vector<int> v1;
vector<int> v2;
vector<int> v3;
for (int i = 0; i < 5; i++) {
v1.push_back(i);
v2.push_back(i + 10);
v3.push_back(i + 100);
}
//将小容器放入到大容器中
v.push_back(v1);
v.push_back(v2);
v.push_back(v3);
//遍历所有数据
for (vector<vector<int>>::iterator it = v.begin(); it != v.end(); ++it) {
for (vector<int>::iterator vIt = (*it).begin(); vIt != (*it).end(); ++vIt) {
cout << *vIt << " ";
}
cout << endl;
}
}
3. 常用容器
3.1 string容器
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stdexcept>
using namespace std;
void test() {
string s = "hello world";
for (int i = 0; i < s.size(); ++i) {
//cout << s[i] << endl;
cout << s.at(i) << endl;
}
//[]访问越界时程序会宕掉,at会抛出异常
try {
//cout << s[100] << endl;
cout << s.at(100) << endl;
}
catch (out_of_range & e) {
cout << e.what() << endl;
}
}
int main(){
test();
return 0;
}
void test() {
string s1 = "abcde";
string s2 = s1.substr(1, 3);
cout << "s2 = " << s2 << endl;
//查找一个邮件的用户名
string email = "charon@sina.com";
int pos = email.find("@");//6
string usrName = email.substr(0, pos);
cout << "用户名为:" << usrName << endl;
}
//将stirng字符串中的所有小写字母都变为大写字母
void test() {
string s = "abCdEfg";
for (int i = 0; i < s.size(); ++i) {
s[i] = toupper(s[i]);
//s[i] = tolower(s[i]);
}
cout << s << endl;
}
3.2 vector容器
void test() {
vector<int> v;
int arr[] = { 2,3,4,1,9 };
vector<int> v1(arr, arr + sizeof(arr) / sizeof(int));
vector<int>v2(v1.begin(), v1.end());
vector<int>v3(10, 100);//10个100
v3.swap(v2);//交换容器内的元素
}
//使用swap收缩空间
void test() {
vector<int> v;
for(int i=0;i<100000;++i){
v.push_back(i);
}
cout << "v的容量:" << v.capacity() << endl;
cout << "v的大小:" << v.size() << endl;
v.resize(3);
cout << "v的容量:" << v.capacity() << endl;
cout << "v的大小:" << v.size() << endl;
vector<int>(v).swap(v);
cout << "v的容量:" << v.capacity() << endl;
cout << "v的大小:" << v.size() << endl;
}
//reserve(int len) //容器预留len个元素长度,预留位置不初始化,元素不可访问
void test() {
vector<int> v;
int* p = NULL;
int num = 0;
v.reserve(100000);
for (int i = 0; i < 100000; ++i) {
v.push_back(i);
if (p != &v[0]) {
p = &v[0];
num++;
}
}
cout << num << endl;
//开辟100000数据用了30次内存空间转换
//如果先预留100000的空间,只需要1次转换
}
void test() {
//逆序遍历
vector<int> v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
//reverse_iterator 逆序迭代器
for (vector<int>::reverse_iterator it = v.rbegin(); it != v.rend(); ++it) {
cout << *it << " ";
}
}
//vector迭代器是随机访问的迭代器,支持跳跃式访问
vector<int>::iterator it = v.begin();
it = it + 3;
3.3deque容器
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stdexcept>
#include <deque>
#include <ctime>
using namespace std;
class Person {
public:
Person(string name,int score) {
this->name = name;
this->score = score;
}
string name;
int score;
};
void create(vector<Person>& v) {
string nameSeed = "ABCDE";
for (int i = 0; i < 5; ++i) {
string name = "选手";
name += nameSeed[i];
int score = 0;
Person p(name, score);//构造函数
v.push_back(p);
}
}
//排序规则
bool myCompare(int v1, int v2) {
return v1 > v2;
}
void setScore(vector<Person>& v) {
//对五个人进行打分
for (vector<Person>::iterator it = v.begin(); it != v.end(); ++it) {
deque<int>d;
for (int i = 0; i < 10; ++i) {
//10个评委
int score = rand() % 41 + 60;
d.push_back(score);
}
//排序
sort(d.begin(), d.end(), myCompare);
d.pop_back();
d.pop_front();
int sum = 0;
for (deque<int>::iterator it = d.begin(); it != d.end(); ++it) {
sum += *it;
}
//cout << "平均分为:" << sum / d.size() << endl;
it->score = sum / d.size();
}
}
void printScore(vector<Person>& d) {
for (vector<Person>::iterator it = d.begin(); it != d.end(); ++it) {
cout << "姓名:" << it->name << " 平局分为:" << it->score << endl;
}
}
void test() {
//设置随机种子
srand((unsigned int)time(NULL));
vector<Person> v;
create(v);
setScore(v);
printScore(v);
}
int main(){
test();
return 0;
}