【问题标题】:How do you use the mapped value?你如何使用映射的值?
【发布时间】:2014-12-15 23:26:02
【问题描述】:

我有一个映射

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

然后我将随机形状插入到地图中。 我想将方法​​(绘制)用于映射值。当我遍历地图时,你如何做到这一点?

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();
    }

}

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

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

如果我的地图数据类型是 baseShape 向量指针。

【问题讨论】:

    标签: c++ oop pointers dictionary polymorphism


    【解决方案1】:

    您的建议不起作用,因为pos-&gt;second 不是Shape*,而是vector&lt;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();
        }
    }
    

    【讨论】:

    • 这是将项目插入地图的正确实现吗? shapeMap.insert(pair>(ShapeType(rNum), baseShape)) ;更具体地说 (ShapeType(rNum)。因为当我打印出外循环的计数器时,我应该得到 20 (PROGRAM_RUN = 20) 时只得到 3。
    • @user2577345 在语法上它是正确的,但是您的地图中只有 3 个不同的键(MyRectangleMyTriangleMyCircle)。 map 每个键只能有一个值...因此您只有 3 个项目。如果您需要更多键,或者如果您可以为每个键设置多个值,请使用 multi_map
    • 我使用了多图并按照您所说的插入了所有内容。但是,当我输出映射值时,我会在同一区域中获得相同的 3 个形状,一次迭代一个。我没有正确插入物品吗?这里: shapeMap.insert(pair>(ShapeType(rNum), baseShape)) ;我可以有 20 个 MyCircle ShapeType,但其中只有 1 个重复输出。
    • 你每次都插入相同的向量。如果你想要不同的形状,你必须插入不同的值。
    【解决方案2】:

    取消引用pos 迭代器的结果是pair&lt;ShapeType, vector&lt;Shape*&gt;&gt;&amp;,因此它的second 成员是vector&lt;Shape*&gt; - 换句话说,您需要遍历映射中每个值的所有元素,例如,

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

    【讨论】:

      猜你喜欢
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 2015-05-24
      • 2011-01-12
      • 2019-11-12
      • 1970-01-01
      • 1970-01-01
      • 2021-11-15
      相关资源
      最近更新 更多