自学c++之stl

stl六大组件,容器、算法、迭代器、仿函数、适配器、空间配置器

容器

各种数据结构,例如:vector、list、deque、set、map

vctor
#include 
#include 
#include
using namespace std;

void myprint(int val){
    cout<v;//相当于数组
    //插入数据
    v.push_back(10);
    v.push_back(20);
    //通过迭代器来访问数据
    //vector::iterator itBegin=v.begin();//起始迭代器,指向第一个
    //vector::iterator itEnd=v.end();//结束迭代器,最后一个元素的下一个位置

    /*第一种遍历方式
    while(itBegin!=itEnd){
        cout<<*itBegin<::iterator i=v.begin();i!=v.end();i++){
        cout<<*i<)
    for_each(v.begin(),v.end(),myprint);

}

补充:for_each是一个通用的算法,用于对容器中的所有元素执行给定的函数。for_each函数是定义在头文件中的,它是C++标准库的一部分。for_each主要用于遍历容器,如vector、list、set等,对容器中的每个元素执行特定操作。
for_each()事实上是個 function template,其源码如下:

for_each(InputIterator first, InputIterator last, Function fn);

  • first 输入迭代器,指向容器中第一个元素
  • last 输入迭代器,指向容器中最后一个元素的下一个位置
  • fn 一个可调用对象(函数指针、函数对象或Lambda表达式),它接受容器中元素的引用作为参数
#include 
#include 
#include
using namespace std;

void myprint(int val){
    cout<v;//相当于数组
    //插入数据
    v.push_back(10);
    v.push_back(20);
    //通过迭代器来访问数据
    //vector::iterator itBegin=v.begin();//起始迭代器,指向第一个
    //vector::iterator itEnd=v.end();//结束迭代器,最后一个元素的下一个位置

    /*第一种遍历方式
    while(itBegin!=itEnd){
        cout<<*itBegin<::iterator i=v.begin();i!=v.end();i++){
        cout<<*i<)
    for_each(v.begin(),v.end(),myprint);

}

算法

各种算法:sort、find、copy、for_each

迭代器

容器与算法之间的胶合剂

仿函数

行为类似函数,可作为算法的某种策略

vctor
#include 
#include 
#include
using namespace std;

void myprint(int val){
    cout<v;//相当于数组
    //插入数据
    v.push_back(10);
    v.push_back(20);
    //通过迭代器来访问数据
    //vector::iterator itBegin=v.begin();//起始迭代器,指向第一个
    //vector::iterator itEnd=v.end();//结束迭代器,最后一个元素的下一个位置

    /*第一种遍历方式
    while(itBegin!=itEnd){
        cout<<*itBegin<::iterator i=v.begin();i!=v.end();i++){
        cout<<*i<)
    for_each(v.begin(),v.end(),myprint);

}

补充:for_each是一个通用的算法,用于对容器中的所有元素执行给定的函数。for_each函数是定义在头文件中的,它是C++标准库的一部分。for_each主要用于遍历容器,如vector、list、set等,对容器中的每个元素执行特定操作。
for_each()事实上是個 function template,其源码如下:

for_each(InputIterator first, InputIterator last, Function fn);

  • first 输入迭代器,指向容器中第一个元素
  • last 输入迭代器,指向容器中最后一个元素的下一个位置
  • fn 一个可调用对象(函数指针、函数对象或Lambda表达式),它接受容器中元素的引用作为参数
#include 
#include 
#include
#include
using namespace std;

void myprint(int val){
    cout<name=m;
        this->age=a;
    }
    string name;
    int age;
};

//存放自定义数据类型
int main()
{
    vectorv;//相当于数组
    person p1("111",18);
    person p2("222",19);
    v.push_back(p1);
    v.push_back(p2);
    for(vector::iterator it=v.begin();it!=v.end();it++){
        cout<<(*it).name<<" "<<(*it).age<

适配器

一种用来修饰容器或者仿函数或者迭代器接口的东西

空间配置器

负责空间的配置与管理

你可能感兴趣的:(c++,开发语言)