Working with std :: List in std :: Map for operator ==, operator! = Etc

I am trying to understand the implementation of Directed Graph (without the Boost library, which is mentioned a lot), just trying to learn something in C ++. Here's what I came up with while reading various pointers on various issues. I decided that I could use the map to search for specific places within the graph and a list to store links.

Thus, the key will say that the first one to be added to the list, and those it refers to, will be the value held as a list

This gives

key John
list of values
list taylor-> larry-> sarah

#include <iostream>
#include <map>
#include <list>

class Graph {
    public:
        typedef std::map <std::string, std::list<std::string> > MapType;
        MapType am; // adjacency map

        Graph() {
        }

        void addVertex(std::string s) {
            if(!am[s]){ // Trying to check if the key has been defined before
            std::list<std::string> l;
            am[s]=l;
            }       
        }

        void addEdge(std::string s1, std::string s2) {
            addVertex(s1);
            addVertex(s2);
            am[s1].push_back(s2);
        }

};


int main (int argc, char *argv[] ){
    Graph *people;
    people = new Graph();
    people->addVertex("John");
    people->addEdge("John","Taylor");

}

What is the correct way to check if a key on a map has been previously defined for a list as a value?
I know that for

std::map <std::string, int >

0, !am[s], , s.

, , , , , .

if(am[s].empty()){, , , .

+3
2

, , - find(). operator[], , , ( , ?)

find() , . , ( , - end(). - find().

std::multimap<std::string, std::string> std::map<std::string, std::list<std::string> >.

+3

std::map<k, v>::find(const k&) - , . , , , , std::map<k, v>::end(), .

0

Source: https://habr.com/ru/post/1775342/


All Articles