2016-04-22 36 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