c++ - How should I iterate this map according to a given string and then append its value? -


edited

why i'm asking...

yesterday, started project create morse code translator creates file or appends existing file, translated morse code given string or file text given.

mainly,i have no idea in hell how map work string in want return , feel if i've tried can google or read in documentation.

additionally...

i've left horrendous attempt @ iterating through data structures , time using vectors, having exhausted tries map methods. i'm sure i'm missing simple syntx map structure left last attempt because believe conveys intention quite due baroque nature.

so more specific, what's best way access map , return through function.

initial design

gettranslation()

/* @brief: program returns string translation  * of rvalue string takes argument  * @param text: string of letters, numbers , symbols translated  * @return translation: translated string appended map values  */ string gettranslation (const string&& text) noexcept(true) {    //return value  auto  translation = "";   map <string,string> morsekey; morsekey ["a"] = ".-"; morsekey ["b"] = "-..."; morsekey ["c"] = "-.-."; morsekey ["d"] = "-..."; //...  // going attempt // unpack vectors compare vectors of  // strings because of consistent issues // type safety errors // i've tried iterating on differently // last hope code here // on how accomplish in // better way still retain  // use of map because of ability  //to hold sorts of characters     //would appreciated  /*           vector <string> msymbol;     (auto itr : morsekey)     {            msymbols.push_back(itr.first);       }      vector <string> vtext;     (auto itr : text)     {     vtext.push_back(itr);      }       (int = 0; < text.length(); i++)     {      if (vtext[i] == msymbol[i])     {     translation += morsekey.at(i);     }       } */  translation = "*scaffolding* function not complete"; return translation;  } 

edit:

wow, iv'e received input , believe issues rooted in fact using auto caused translation const char* wouldn't allow me make map map std::map<char,string> morsekey. rvalue cast via move apparently unnecessarily (i had feeling). i'm going implement knowledge i've gained , post before mark answer.

edit 2

  1. i removed auto , 'translation' declared string

  2. gettranslation's signature takes const string&

  3. i initialize morsekey

static map <char,string> const morsekey = {{'a', ".-"},...

but compiler error of 'invalid conversion ‘const char*’ ‘const char&’ don't understand why is, or makes either pointer or ref in situation , therefore how fix it.

wow... practiced lot of concepts, , learning code!!!

i'm sure successful programmer (but should know needs lot more practicing!)

but "gettranslation" function, changed little to:

#include <algorithm> #include <cctype> /*...*/  string gettranslation (string text) noexcept(true) {     map <char,string> morsekey;     morsekey ['a'] = ".-";     morsekey ['b'] = "-...";     morsekey ['c'] = "-.-.";     /*...*/      string translation {""};     std::transform(text.begin(), text.end(), text.begin(), toupper);     (it: text){         translation += morsekey[it];     }     return translation; } 

as may know, map associative array; means don't need iterate on of element find interesting element. should associate key corresponding record. in case should associate char (not string) string; should define "morsekey" as:

map <char, string> morsekey; 

and when want associate character such 'a' ".-" should like:

morsekey ['a'] = ".-"; /*instead of morskey["a"] = ".-" */; 

also when used "auto" in defining "translation" variable, compiler consider "const char*". should explicitly define "translation" variable as:

string translation {""}; 

in addition because our "morsekey" map contains uppercase of alphabets, should convert alphabet characters of "text" variable uppercase. can done by:

std::transform(text.begin(), text.end(), text.begin(), toupper); 

but using command should include 2 libraries:

#include <algorithm> /*for "transform" */ #include <cctype> /*for "touppper" */ 

also shouldn't consider "text" variable rvalue more (because modify it) change function delcarion to:

string gettranslation (string text) noexcept(true)   

finally should iterate on "text" variable , find corresponding morse value of each character , append return value; can done by:

    (it: text){         translation += morsekey[it];     } 

have fun programming!

my reply second edit:

i think information not enough; perhaps it's better ask question new question, , provide more details, such in line got compile error or other useful details may think can helpful.


Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -