Home c++ MAP does not preserve the association for an existing key

MAP does not preserve the association for an existing key




Good afternoon. I solve the task of : Two words are called synonyms each other if they are similar. Implement the following operations on the synonyms dictionary:
Add Word1 Word2 – Add to dictionary a pair of synonyms (Word1, Word2).
Count Word – find out the number of synonyms word Word.
Check Word1 Word2 – Check whether words Word1 and Word2 are synonymous. Word1 and Word2 are considered synonyms if there was at least one request to add Word1 Word2 or Add Word2 among ADD queries.
For simplification, we assume that synonyms do not have transitivity, that is, if A is synonym B, and b is synonym C, then it does not follow from this that A is synonym C.
Format input
First, the number of requests q is entered, then Q rows with query descriptions. It is guaranteed that in each Request Check and Add Word1 and Word2 words are different. All words consist only of Latin letters, numbers and shutting symbols.
Output format
For each query in the appropriate line, output the answer to it:
In response to the Count Word request, output the only integer – the number of synonyms of the word Word.
In response to Check Word1 Word2 request, output the YES string if Word1 and Word2 are synonyms, and otherwise. “

wrote code using a MAP container.

# include & lt; iostream & gt;
#Include & lt; Map & GT;
#Include & lt; vector & gt;
#Include & lt; String & GT;
Using Namespace STD;
Void PRINT_MAP (Const Map & Lt; String, String & GT; & amp; MP) {
  For (AUTO I: MP) {
    COUT & LT; & LT; I.First & LT; & lt; ":" & lt; & lt; i.second & lt; & lt; endl;
Void Add (MAP & LT; String, String & GT; & Amp; MP, String Word1, String Word2) {
  MP [WORD1] = WORD2;
INT COUNT (Const Map & Lt; String, String & GT; & amp; MP, String Word) {
  INT COUNT_ = 0;
  For (AUTO I: MP) {
    if (i.first == Word || i.second == Word)
      Count _ ++;
BOOL CHECK (MAP & LT; STRING, STRING & GT; & AMP; MP, String Word1, String Word2) {
  if (MP [WORD1] == WORD2 || MP [WORD2] == WORD1) {
  int q;
  String Name_Command, Syn1, Syn2;
  Map & LT; String, String & GT; MP;
  CIN & GT; & GT; Q;
  For (int i = 0; i & lt; q; i ++) {
    CIN & GT; & GT; name_command;
    if (Name_Command == "add") {
      CIN & GT; & GT; SYN1 & GT; & GT; syn2;
      Add (MP, SYN1, SYN2);
    if (Name_Command == "Count") {
      CIN & GT; & GT; syn1;
      COUT & LT; & LT; COUNT (MP, SYN1) & LT; & lt; Endl;
    if (name_command == "Check") {
      CIN & GT; & GT; SYN1 & GT; & GT; syn2;
      If (Check (MP, SYN1, SYN2)) {
        COUT & LT; & LT; "Yes" & lt; & lt; endl;
      ELSE {
        COUT & LT; & lt; "no" & lt; & lt; endl;
  Print_map (MP);

Error is that 6 Test View:

Add C++ Rust
Add C++ java
Check RUST C++

gives “no” and it is incorrect. I do not understand why the MAP container does not create the second “Association” between C++ and Java, and rubs the first, changing Rust to Java. I would be grateful for your help in correcting the bug.

Answer 1, Authority 100%

Because Map is essentially a set in which there can be only one identical element (since the comparison goes on the key – one key).

You need either Multimap when there can be many elements with one key, or what seems to me,

map & lt; string, vector & lt; string & gt; & gt;

So you can also store a set of different words in one element.

This is on a specific issue. But generally speaking, you need another structure – Rather, the graph, because if a – synonym B, then b – synonym A. and the search can go to any side.

So I would use one of the views for graphs. Synonymity is quite a small phenomenon, so the graph will be rare, and therefore I would use something like adjacency lists.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions