矢量vector

vector存储类对象:

#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
 
class point{
public:
    point(int _x =0, int _y =0 ):x(_x), y(_y){};
    int GetX(void) const { return x;}
    int GetY(void) const { return y;}
private:
    int x,y;
};

int _tmain(int argc, _TCHAR* argv[])
{
    vector<point> vec;
    vec.push_back(point(1,2));
    vec.push_back(point(2,2));
 
    for (auto itr = vec.cbegin(); itr != vec.cend(); ++itr)
    {
        cout<<"point("<<itr->GetX()<<","<<itr->GetY()<<")"<<endl;
    }

    getchar();
    return 0;
}

输出结果:


result1.PNG

存储类指针:

#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
 
class Point{
public:
    Point(int x =0, int y =0 ):x(x), y(y){};
    int GetX(void) const { return x;}
    int GetY(void) const { return y;}
private:
    int x,y;
};

int _tmain(int argc, _TCHAR* argv[])
{
    //存储类指针
    vector<Point*> vecptr;
    for (int i = 0; i < 5; i++)
    {
        Point pt;
        Point* ptr=&pt; //此种方法为pt分配的地址都是一样的,不可行
        vecptr.push_back(ptr);
    }
    for (int i = 0; i < vecptr.size(); i++)
    {
        cout<<vecptr[i]<<endl;  //地址都相同
    }
    cout<<endl;
    cout<<endl;

    vecptr.clear(); //清空矢量容器
    for (int i = 0; i < 5; i++)
    {
        Point* ptr=new Point(); //此法可行,分配成功返回地址
        vecptr.push_back(ptr);
    }
    for (int i = 0; i < vecptr.size(); i++)
    {
        cout<<vecptr[i]<<endl;  //地址都不同,可行
    }
    cout<<endl;

    //存储类对象
    vector<Point> vec;
    for (int i = 0; i < 10; i++)
    {
        Point pt;
        vec.push_back(pt);
    }
    for (int i = 0; i < vec.size(); i++)
    {
        cout<<&vec[i]<<endl;
    }
    getchar();
    return 0;
}

输出结果:


result2.PNG

指向基类的vector用法:

#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
 
class Polygon 
{
protected:
    int width, height;
public:
    void set_values (int a, int b)
    {
        width=a; 
        height=b; 
    }
    virtual int area() const = 0;
};
 
class Rectangle: public Polygon 
{
public:
    virtual int area() const 
    { 
        return width*height;
    }
};
 
class Triangle: public Polygon 
{
public:
    virtual int area() const 
    { 
        return width*height/2; 
    }
};
int _tmain(int argc, _TCHAR* argv[])
{
    Rectangle rect;
    Triangle trgl;
    vector<Polygon*> vec;   //指向基类的矢量容器

    Polygon * ppoly1 = &rect;
    ppoly1->set_values (4,5);
    vec.push_back(ppoly1);

    Polygon * ppoly2 = &trgl;
    ppoly2->set_values (4,5);
    vec.push_back(ppoly2);
    
    for (auto itr = vec.cbegin(); itr != vec.cend(); ++itr) //不知道用什么类型时可以选择用auto
    {
        cout << (*itr)->area() << '\n';
    }
    getchar();
    return 0;
}

输出结果:
20
10

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 33,683评论 18 399
  • 1.C和C++的区别?C++的特性?面向对象编程的好处? 答:c++在c的基础上增添类,C是一个结构化语言,它的重...
    杰伦哎呦哎呦阅读 13,299评论 0 45
  • 三十岁的女人像玫瑰,迷人; 三十岁的女人像苹果,好看又好吃; 三十岁的女人像一杯茶,清香,怡人; 三十岁的女人像一...
    半象花阅读 4,330评论 8 3
  • 让你放开你的手机一小时,你最想干什么?
    亨利何阅读 4,340评论 2 1
  • 红薯餐从17年1月开始,前4次185天,本次44天,偿试180天。5年的高血压药停了13个月!虎杖粉加萝卜麻药方第...
    南得糊涂呀阅读 1,094评论 0 0

友情链接更多精彩内容