vector的元素不仅仅可以使int,double,string,还可以是结构体

vector的元素不仅仅可以使int,double,string,还可以是结构体,第1张

vector的元素不仅仅可以使int,double,string,还可以是结构

link

#include
#include
#include
#include
using namespace std;

typedef struct rect
{
    int id;
    int length;
    int width;

    //对于向量元素是结构体的,可在结构体内部定义比较函数,下面按照id,length,width升序排序。
    bool operator< (const rect &a)  const
    {
        if(id!=a.id)
            return id vec;
    Rect rect;
    rect.id = 1;
    rect.length = 2;
    rect.width = 3;
    vec.push_back(rect);
    vector::iterator it = vec.begin();
    cout << (*it).id << ' ' << (*it).length << ' ' << (*it).width << endl;

    return 0;
}

1 2 3

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5698494.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存