2016-04-22 57 views
-2

我想比较两个向量串比较字符串(字符)的两个向量

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 


     } 

任何人可以帮助我吗?

+0

恕我直言,我不会存储与莫尔斯电码的字符,而是使用'std :: pair'并将其分开。 – NathanOliver

回答

0

你的代码有2个问题:

  1. 你不能让2维向量这样也不你甚至试图使它2D。
  2. 你写了morse[i][j]没有先前定义的i和j。

要解决问题1 & 2:

包括

#include <vector> 

使性病的矢量::对(S):

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

这可以让你有一个一对弦。 要添加新的莫尔斯电码,使用此:

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

要读“时间使用:

//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