简体   繁体   English

STL映射,使用类的迭代器和指针

[英]STL map, using iterators and pointers of classes

I am having trouble accessing data inside a map using an iterator. 我在使用迭代器访问地图内的数据时遇到麻烦。 I want to return all of the values inserted into the map by using an iterator. 我想通过使用迭代器返回插入到映射中的所有值。 However, when I use the iterator, it doesn't acknowledge any of the members in the instance of the class it has been past into. 但是,当我使用迭代器时,它不会确认该类实例中的任何成员。

int main()
{
    ifstream inputFile;
    int numberOfVertices;
    char filename[30];
    string tmp;

    //the class the holds the graph
    map<string, MapVertex*> mapGraph;

    //input the filename
    cout << "Input the filename of the graph: ";
    cin >> filename;
    inputFile.open(filename);

    if (inputFile.good())
    {
        inputFile >> numberOfVertices;
        inputFile.ignore();

        for (int count = 0; count < numberOfVertices; count++)
        {
            getline(inputFile, tmp);
            cout << "COUNT: " << count << "  VALUE: " << tmp << endl;

            MapVertex tmpVert;
            tmpVert.setText(tmp);
            mapGraph[tmp]=&tmpVert;
        }

        string a;
        string connectTo[2];

        while (!inputFile.eof())
        {

            //connectTo[0] and connectTo[1] are two strings that are behaving as keys

            MapVertex* pointTo;
            pointTo = mapGraph[connectTo[0]];
            pointTo->addNeighbor(mapGraph[connectTo[1]]);
            //map.find(connectTo[0]).addNeighbor(map.find(connectTo[1]));
            //cout << connectTo[0] << "," << connectTo[1] << endl;
        }

        map<string,MapVertex*>::iterator it;
        for (it=mapGraph.begin(); it!=mapGraph.end(); it++)
        {
            cout << it->getText() << endl;

        }
    }

    return 0;
}

Compiler output: 编译器输出:

\lab12\main.cpp||In function `int main()':|
\lab12\main.cpp|69|error: 'struct std::pair<const std::string, MapVertex*>'
                           has no member named 'getText'|
||=== Build finished: 1 errors, 0 warnings ===|

There is an access member in my MapVertex class called getText() which returns the data that is inside it. 我的MapVertex类中有一个名为getText()的访问成员,该访问成员返回其中的数据。

To fix the compiler error, you need to do it->second->getText() as the *iterator is a pair<string, MapVertex*> . 要修复编译器错误,您需要执行- it->second->getText()因为*iteratorpair<string, MapVertex*> But there are other problems in your code. 但是您的代码中还有其他问题。 While inserting into the map, you are inserting the address of a local variable into it. 在插入地图时,您正在向其中插入局部变量的地址。 This address will be invalid by the time you try to iterate the map using for loop. 在您尝试使用for循环迭代地图时,此地址将无效。 I would suggest you to declare the map as std::map<string, MyVertex> so that when you insert into the map a copy of the MyVertex is inserted into map. 我建议您将地图声明为std::map<string, MyVertex>以便在将地图插入MyVertex的副本时将其插入地图。

tmpVert is the problem. tmpVert是问题所在。 Look, you create it on the stack. 看,您在堆栈上创建了它。 It is destroyed at the end of each for loop. 在每个for循环的结尾将其销毁。

It Is Destroyed. 它被破坏了。

So, your mapGraph is holding pointers to objects that don't exist. 因此,您的mapGraph持有指向不存在的对象的指针。

'struct std::pair' has no member named 'getText'

means that what the iterator returns is std::pair, not your object directly; 意味着迭代器返回的是std :: pair,而不是您的对象; the first element of the pair is the key, the second the value, so you need to get the value, and then call the method: it->second->method() . 该对中的第一个元素是键,第二个是值,因此您需要获取值,然后调用方法: it->second->method()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM