IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 53_C++_Vector容器的各类功能+STL中Vector容器功能【难点:for_each中的参数信息以及sort排序函数中的参数】 -> 正文阅读

[C++知识库]53_C++_Vector容器的各类功能+STL中Vector容器功能【难点:for_each中的参数信息以及sort排序函数中的参数】

难点:

对int类型的容器遍历

1for_each(vi.begin(), vi.end(), [=](const int &midint){cout << midint << endl;});

2for_each(v1.begin(), v1.end(), [=](const int a){cout << a << endl;});

对_stu_类对象类型的容器遍历

for_each(s1.begin(), s1.end(), [=](const stu &node){cout << node.num << node.name << endl;});

sort排序函数中的参数

class MYSORT
{
public:
    bool operator()(int a,int b)
    {
        return a<b;
    }
};

bool myccc(int a,int b)
{
    return a<b;
}

//回调函数、或者调用类重载‘()’
//sort(v1.begin(), v1.end(), MYSORT());
sort(v1.begin(), v1.end(), myccc);

main.cpp

#include <iostream>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;


class stu
{
public:
    int num;
    string name;

public:
    stu()
    {
        this->num = 0;
        this->name = new char[1024]();
    }

    stu(int num , string name):name(name)
    {
        this->num = num;

    }
};

//自己定义实现的函数/*......................*/
void print(stu node)
{
    cout << node.num <<" " << node.name << endl;

}

void play(int i)
{
    cout << i << endl;
}

class myplay1
{
  public:
    //对()重载
    bool operator()(int a, int b)
    {
        return a > b;
    }
};

class MYSORT
{
public:
    bool operator()(int a,int b)
    {
        return a<b;
    }
};

bool myccc(int a,int b)
{
    return a<b;
}


bool mysort(stu a, stu b)
{
    return a.num > b.num;
}


class mysortclass
{
public:
    bool operator()(stu a, stu b)
    {
        return a.num > b.num;
    }
};
//自己定义实现的函数和类/*......................*/

int main()
{
    vector<int> v1;

    cout << v1.size() << endl;
    cout << v1.capacity() << endl;
    /*.............................................................................*/

//    for(int i = 0; i < 5; i++)
//    {
//        v1.push_back(i);
//        cout<<"元素个数 :" << v1.size() << " ;" << "容器容量大小 :"<< v1.capacity() << endl;

//    }
//    cout<<"元素总个数 :" << v1.size() << endl;
//    cout << "容器容量总大小 :"<< v1.capacity() << endl;
/*.............................................................................*/


    v1.push_back(5);
    v1.push_back(9);
    v1.push_back(4);
    v1.push_back(3);

    cout << v1.front() << endl;
    cout << v1.back() << endl;
    putchar('\n');

    cout<<"......................................"<<endl;
    cout <<"对数字从小到大排序 :" << endl;
    cout <<"原数字 :" << endl;
    for_each(v1.begin(), v1.end(), play);
    putchar('\n');

    cout <<"排序后(方法2) :" << endl;
    //sort(v1.begin(), v1.end(), MYSORT());
    sort(v1.begin(), v1.end(), myccc);
    for_each(v1.begin(), v1.end(), [=](const int a){cout << a << endl;});
    cout<<"......................................"<<endl;
    putchar('\n');

    cout << *(v1.begin()) << endl;
    cout << *(v1.end() - 1) << endl;
    putchar('\n');

    /*.............................................................................*/


    vector<int>::iterator pstrat =  v1.begin();
    vector<int>::const_iterator pend = v1.end();
    cout<<"元素个数 :" << v1.size() << " ;" << "容器容量大小 :"<< v1.capacity() << endl;


    for( ; pstrat != pend ;)
    {
        cout << *pstrat << endl;
        pstrat++;
    }
    putchar('\n');

    /*.............................................................................*/

    cout <<"验证-删除一个顶端元素 :"<<endl;
    v1.pop_back();
    cout<<"元素个数 :" << v1.size() << " ;" << "容器容量大小 :"<< v1.capacity() << endl;
    pstrat =  v1.begin();
    pend = v1.end();
    for( ; pstrat != pend ;)
    {
        cout << *pstrat << endl;
        pstrat++;
    }
    putchar('\n');

    /*.............................................................................*/


    cout <<"验证-在2号位置插入 1 个新元素 :"<<endl;
    v1.insert(v1.begin() + 2, 1, 666);
    cout<<"元素个数 :" << v1.size() << " ;" << "容器容量大小 :"<< v1.capacity() << endl;
    pstrat =  v1.begin();
    pend = v1.end();
    for( ; pstrat != pend ;)
    {
        cout << *pstrat << endl;
        pstrat++;
    }
    putchar('\n');

    /*.............................................................................*/

    cout <<"验证-rbegin-rend-反向输出容器内容 :"<<endl;
    vector<int>::reverse_iterator ptstart1 = v1.rbegin();
    vector<int>::reverse_iterator pend1 = v1.rend();
    cout << *ptstart1 << endl;
    cout << *(++ptstart1) << endl;
    cout << *(pend1) << endl;
    cout << *(--pend1) << endl;//!!!
    putchar('\n');

/*.............................................................................*/
/*.............................................................................*/

    cout <<"二、类容器 :"<<endl;

    vector<stu> s1;
   //stu student[5] = {stu(12, "a")};
    s1.push_back(stu(43, "上三"));
    s1.push_back(stu(24, "里斯"));
    s1.push_back(stu(15, "王武"));
    s1.push_back(stu(36, "霸邪"));
    cout<<"元素个数 :" << s1.size() << " ;" << "容器容量大小 :"<< s1.capacity() << endl;

    vector<stu>::iterator pstart2 = s1.begin();
    vector<stu>::iterator pend2 = s1.end();



    cout << "resize->容器容量大小 :"<< s1.capacity() << endl;

    for(; pstart2 != pend2; )
    {
        cout << pstart2->num <<" " << pstart2->name << endl;
        pstart2++;
    }
    putchar('\n');

    /*.............................................................................*/

    cout<<"......................................"<<endl;
    cout << "重新分配大小(创建了的空间,旧有空间被释放:):" <<endl;
    s1.resize(8);
    pstart2 = s1.begin();
    pend2 = s1.end();

    //(错误)
    // cout<<(*(pstart2-1)).num<<" "<<(*(pstart2-1)).name<<endl;
    //cout<<(*(pend2)).num<<" "<<(*(pend2)).name<<endl;

    cout << "for循环-遍历vector容器内容:" <<endl;
    for(; pstart2 != pend2; )
    {
        cout << pstart2->num <<" " << pstart2->name << endl;
        pstart2++;
    }
    cout<<"元素个数 :" << s1.size() << " ;" << "容器容量大小 :"<< s1.capacity() << endl;
    cout<<"......................................"<<endl;
    putchar('\n');

    /*.............................................................................*/

    cout << "for_each遍历vector容器内容(回调函数方式输出):" <<endl;
    for_each(s1.begin(), s1.end(), print);//回调函数调用--输出
    putchar('\n');

    /*.............................................................................*/

    cout << "for_each遍历vector容器内容([=]直接输出):" <<endl;
    for_each(s1.begin(), s1.end(), [=](const stu &node){cout << node.num << node.name << endl;});
    putchar('\n');

    /*.............................................................................*/

    cout <<"学号有序排序 :" << endl;
    //sort(s1.begin(), s1.end(), mysort);
    sort(s1.begin(), s1.end(), mysortclass());

    for_each(s1.begin(), s1.end(), [=](const stu &node){cout << node.num << node.name << endl;});
    putchar('\n');


    cout <<"插入一个新的学号,学号有序排序 :" << endl;
    s1.insert(s1.begin()+1, 1, stu(99, "共舞"));
    sort(s1.begin(), s1.end(), mysortclass());
    for_each(s1.begin(), s1.end(), [=](const stu &node){cout << node.num << node.name << endl;});
    putchar('\n');

/*.............................................................................*/
/*.............................................................................*/

    cout<<"API 接口的vector的5种方式:" << endl;

    //方式1
    vector<int>  vstu;
    vstu.push_back(2);
    vstu.push_back(4);
    vstu.push_back(22);
    vstu.push_back(5);
    vector<int > vi(vstu.begin(), vstu.begin() +2);

    //方式2
    //int a[4] = {1,2,3,4};
    //vector<int > vi(a, a+4);

    //方式3
    //vector<int> vi(3, 777);

    for_each(vi.begin(), vi.end(), [=](const int &midint){cout << midint << endl;});

    /*.............................................................................*/

    //方式4
//    vector<stu>  vstu;
//    vstu.push_back(stu(33,"张三"));
//    vstu.push_back(stu(24,"李四"));
//    vstu.push_back(stu(45,"王五"));
//    vstu.push_back(stu(26,"赵六"));
//    vector<stu> vi;
//    vi = vstu;

    //方式5
    //vector<stu> vi(3, stu(22, "啊撒"));

    //方式6
    //vector<stu> vi(vstu.begin(), vstu.begin() +2);

    //for_each(vi.begin(), vi.end(), [=](const stu &p){cout << p.num << p.name << endl;});




    return 0;
}

图示:

在这里插入图片描述在这里插入图片描述

在这里插入图片描述

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-08-19 11:52:27  更:2021-08-19 11:54:19 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 15:26:16-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码