比较两个字符串向量(字符)

时间:2016-04-22 15:52:09

标签: c++

我想比较两个矢量字符串

 vector <string> morse ={"A.-","B-...","C-.-.", "D-..", "E.", "F..-.", "G--.", "H....", "I.." ,"J.---", "K-.-", "L.-..", "M--" ,"N-." ,"O---" ,"P.--.", "Q--.-", "R.-.", "S...", "T-", "U..-", "V...-", "W.--" ,"X-..-" ,"Y-.--", "Z--.."};


vector<string> codeMorse (1);
codeMorse ={".---.--.-.-.-.---...-.---."};

     if (morse[i][j]==codeMorse[k]){    //my problem here =error


        }

有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

您的代码有两个问题:

  1. 您无法以这种方式制作二维矢量,也不会尝试将其制作为2D。
  2. 你写了morse[i][j],之前没有i和j的定义。
  3. 解决问题1&amp; 2:

    包括

    #include <vector>
    

    制作std :: pair(s)的向量:

    std::vector<std::pair<std::string, std::string>> morse;
    

    这允许你有一对字符串。 要添加新的莫尔斯电码,请使用:

    morse.push_back(std::pair<std::string, std::string>("LETTER HERE", "MORSE CODE HERE"));
    

    阅读&#39; em使用它:

    //read all via loop
        for (int i = 0; i <= morse.size(); i++) {
            std::cout << "Letter: " << morse[i].first << std::endl;         //.first access your first elemt of the pair
            std::cout << "Morse Code: " << morse[i].second << std::endl;    //.second to access the morse code
        }
    

    如果您已经知道它们,请使用迭代器:

    //read all via loop
        for (auto i = morse.begin(); i != morse.end(); i++) {
            std::cout << "Letter: " << i->first << std::endl;           //->first access your first elemt of the pair
            std::cout << "Morse Code: " << i->second << std::endl;      //->second to access the morse code
        }
    

    当然,您可以阅读具体的值:

    std::cout << morse[0].first << std::endl;  //[] same use as the array's brackets
    std::cout << morse[0].second << std::endl; //same here
    
相关问题