c++ - 你如何使用映射值?

标签 c++ oop pointers dictionary polymorphism

我有一个映射

map <ShapeType, vector <Shape *> > shapeMap;

然后我将随机形状插入到 map 中。 我想对映射值使用方法(绘制)。当我遍历 map 时,你会怎么做?

void RandomAttributes(Shape *shape);

// declare our objects, and pointers for downcasting
MyRect rObj, *rPtr;
MyTriangle tObj, *tPtr;
MyCircle cObj, *cPtr;



void main()
{
    int shapes; // loop index

    // seed the random number generator
    srand((unsigned int)time(0));

    // allow the user time to move the console window away
    // from the FilledShapes window are
    cout << "Move this window to the lower right of the screen, and press ENTER to continue:\n";
    cin.get();



    // define our array size
    const int baseSize = 3;

    // create an vector of base class pointers
    vector <Shape *> baseShape(baseSize);

    // initialize our vector of base class pointers
    //initialize vector of shapes
    baseShape[0] = &rObj; // a MyRect IS A Shape
    baseShape[1] = &tObj;   // a MyTriangle IS A Shape
    baseShape[2] = &cObj; // a MyCircle IS A Shape

    enum ShapeType {
        MyRectangle = 0,
        MyTriangle = 1,
        MyCircle = 2
    };

    //map
    map <ShapeType, vector <Shape *> > shapeMap;


    for (int i = 0; i<PROGRAM_RUN; i++)
    {
        // clear the window
        // note that I can use ANY instance of a MyRect
        // object to clear the window
        baseShape[0]->ClearScreen();

        int rNum = rand() % 3;

        //CREATING RANDOM SHAPES
        // choose random parameters for each rectangle
        RandomAttributes(baseShape[rNum]);

        //insert shape
        shapeMap.insert(pair<ShapeType, vector <Shape *>>(ShapeType(rNum), baseShape)) ;

    }

    for (map <ShapeType, vector <Shape *> >::iterator pos = shapeMap.begin(); pos != shapeMap.end(); ++pos)
    {
       //DOES NOT WORK
        pos->second->Draw();
    }

}

我当前的实现:

    for (map <ShapeType, vector <Shape *> >::iterator pos = shapeMap.begin(); pos != shapeMap.end(); ++pos)
    {
       //DOES NOT WORK
        pos->second->Draw();
    }

}

如何遍历 map 并使用draw方法如下: 我要实现的映射值如下:

//baseShape[0]->Draw();
//baseShape[1]->Draw();
//baseShape[2]->Draw();

如果我的 map 数据类型是 baseShape vector 指针。

最佳答案

您的建议无效,因为 pos->second不是 Shape* , 这是一个 vector<Shape* >。你也必须迭代第二个:

for (map <ShapeType, vector <Shape *> >::iterator pos = shapeMap.begin(); 
     pos != shapeMap.end(); ++pos)
{
    vector<Shape*>& shapes = pos->second;

    for (size_t i = 0; i < shapes.size(); ++i) {
        shapes[i]->Draw();
    }
}

或者如果您可以使用 C++11:

for (auto& pr : shapeMap) {
    for (auto shape : pr.second) {
        shape->Draw();
    }
}

关于c++ - 你如何使用映射值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27495019/

相关文章:

c++ - 创建 std::function 返回具有函数成员值的变量。分段故障

javascript - 如何在不创建实例的情况下获取类扩展的第一个类

C++ 指针和对象实例化

c++ - 为递归函数传递什么来转换二维数组 "90 degrees"

c++ - 通过引用将派生类指针传递给期望基指针的函数

c++ - Qt/C++ CLI 文本到 QLabel

c++ - 如果第一位小数大于或等于5,如何+1到数字?

.net - 学习 .NET/OOP 最佳实践的最佳方式?

PHP:是否可以像这样访问静态方法:Object::ChildObject::method()?

c++ - Sqlite 不再打开 Google Drive 数据库