C++学习 --- STL初识

STL初识

1、STL的诞生

c++的三种特性:封装,继承,多态

封装:相似的东西封装成一个类,提高复用性

继承:子类继承父类中的属性,行为,提高代码复用性

多态:一个函数名称有多个接口,父类指针指向子类对象,调用同一个接口时,由于对象的不同会产生不同的形态,提高了复用性。

2、STL基本概念

3、STL六大组件

仿函数:小括号重载,类似于函数。

4、STL中容器、算法、迭代器

算法要通过迭代器才可以访问容器中的元素。

5、容器算法迭代器初识

5.1、vector存放内置数据类型

可参考 上一节通用数组类,MyArray案例来理解vector。

#include <iostream>
using namespace std;
#include <vector>
//标准算法的头文件
#include <algorithm>//vector 容器 存放内置数据类型
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.push_back(50);
​
    //第一种遍历方式
    cout << "第一种遍历方式:" << endl;
    //通过迭代器访问容器中的数据
    //起始迭代器 指向容器中第一个元素  
    vector<int>::iterator itBegin = v.begin();
    //结束迭代器 指向容器中最后一个元素的下一个位置
    vector<int>::iterator itEnd = v.end();
    while (itBegin != itEnd) {
        cout << *itBegin << endl;
        itBegin++;
    }
​
    //第二种遍历方式
    cout << "第二种遍历方式:" << endl;
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
        cout << *it << endl;
    }
​
    //第三种遍历方式 利用STL提供遍历算法
    //第三个参数 myPrint 回调函数,在for_each遍历期间调用。
    cout << "第三种遍历方式:" << endl;
    for_each(v.begin(), v.end(), myPrint);
}
    
int main() {
    test01();
​
    system("pause");
    return 0;
}

起始迭代器 指向容器中第一个元素,结束迭代器 指向容器中最后一个元素的下一个位置。

迭代器使用非常类似指针,需要解引用可访问到数据。

5.2、vector存放自定义类型数据
#include <iostream>
#include <string>
#include <vector>
using namespace std;
​
//vector 容器中存放自定义数据类型
class Person {
public:
    Person(string name, int age) {
        this->m_Name = name;
        this->m_Age = age;
    }
    string m_Name;
    int m_Age;
};
​
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);
​
    cout << "存放自定义数据类型:" << endl;
    //遍历容器中的数据
    for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {
        //cout << "姓名:" << (*it).m_Name << "\t年龄:" << (*it).m_Age << endl;
        cout << "姓名:" << it->m_Name << "\t年龄:" << it->m_Age << endl;
    }
}
​
//存放自定义数据类型的指针
void test02() {
    vector<Person *> v;
    Person p1("aaa", 10);
    Person p2("bbb", 10);
    Person p3("ccc", 10);
    Person p4("ddd", 10);
    Person p5("eee", 10);
​
    //像容器中添加数据
    v.push_back(&p1);
    v.push_back(&p2);
    v.push_back(&p3);
    v.push_back(&p4);
    v.push_back(&p5);
​
    //遍历容器中的数据
    cout << "存放自定义数据类型的指针:" << endl;
    for (vector<Person *>::iterator it = v.begin(); it != v.end(); it++) {
        cout << "姓名:" << (*it)->m_Name << "\t年龄:" << (*it)->m_Age << endl;
        //cout << "姓名:" << it->m_Name << "\t年龄:" << it->m_Age << endl;
    }
}
​
​
int main() {
    test01();
    test02();
​
    system("pause");
    return 0;
}

每个容器都有自己专属的迭代器 vector<Person>::iterator it; vector<Person *>::iterator itp;

解出的类型就是这个人容器中的类型 (*it) <==> Person; ( *itp) <==> Person *

5.3、vector容器嵌套容器
#include <iostream>
#include <vector>
using namespace std;
​
//vector容器嵌套容器
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++) {
        for (vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++) {
            cout << *vit << " ";
        }
        cout << endl;
    }
}
​
int main() {
    test01();
​
    system("pause");
    return 0;
}

 

 

 

posted @ 2021-09-07 12:32  yiwenzhang  阅读(52)  评论(0编辑  收藏  举报