繁体   English   中英

我应该如何根据给定的字符串迭代此映射,然后附加其值?

[英]How should I iterate this map according to a given string and then append its value?

已编辑

我为什么要问...

昨天,我启动了一个项目,以创建一个莫尔斯电码翻译器,该翻译器创建一个文件或将其附加到现有文件中,然后从给定的字符串或给定的文件文本中翻译莫尔斯电码。

主要是,我在地狱中不知道如何使该地图与要返回的字符串一起使用,我感觉好像我已经尝试了所有可以谷歌阅读或阅读文档的内容。

另外...

我已经进行了遍历数据结构的可怕尝试,这一次是使用向量,而尝试了map方法。 我确定我在地图结构上缺少简单的语法,但是我放弃了最后的尝试,因为我相信由于其巴洛克式的性质,它可以很清楚地传达我的意图。

因此,更具体地说,访问此映射并通过此函数返回它的最佳方法是什么。

初步设计

getTranslation()

/* @brief: Program returns string which is a translation
 * of the Rvalue string which it takes as a argument
 * @param text: string of letters, numbers and some symbols to be translated
 * @return translation: translated string appended with 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"] = "-...";
//...

// I was going to attempt to
// unpack to vectors then compare as vectors of 
// strings because of consistent issues with
// type safety errors
// i've tried iterating over it differently
// but this is last hope code here
// any help on how to accomplish this in
// a better way but still retain the 
// use of a map because of its ability 
//to hold all sorts of characters
    //would be greatly appreciated

/*      
    vector <string> mSymbol;
    for (auto itr : morseKey)
    {   
        mSymbols.push_back(itr.first);


    }

    vector <string> vText;
    for (auto itr : text)
    {
    vText.push_back(itr);

    }


    for (int i = 0; i < text.length(); i++)
    {

    if (vText[i] == mSymbol[i])
    {
    translation += morseKey.at(i);
    }


    }
*/

translation = "*SCAFFOLDING* FUNCTION NOT COMPLETE";
return translation;

}

编辑:

哇,我收到了一些非常好的建议,我认为我的问题根源在于使用auto导致translation成为const char*的事实,这不允许我将地图设为地图std::map<char,string> morseKey 另外,我通过移动投射的右值显然不必要(我有一种感觉)。 因此,在标记答案之前,我将运用从中获得的知识并将其发布。

编辑2

  1. 我删除了auto ,现在将“ translation”声明为string

  2. getTranslation的签名采用const string&

  3. 我将morseKey初始化为

static map <char,string> const morseKey = {{'A', ".-"},...

但是收到'invalid conversion from 'const char*' to 'const char&'的编译器错误,我不明白为什么会这样,或者在这种情况下是什么使指针或ref产生,以及如何解决它。

该功能可以通过以下方式定义。 (注意:我会将地图作为某个命名空间中的全局变量,并使用std :: pair(s)数组对其进行初始化)。

std::string getTranslation( const std::string&& text) noexcept(true)
{
   std::string translation;

   std::map <std::string, std::string> morseKey;
   morseKey ["A"] = ".-";
   //...

   for ( char c : text )
   {
      c = std::toupper( c );

      auto it = morseKey.find( c );

      if ( it != morseKey.end() ) translation.push_back( it->second );
   }  

   return translation;
}

哇...您练习了很多概念,而您正在学习编码!!!

我相信您将成为一名成功的程序员(但您应该知道它需要更多的练习!)

但是关于您的“ getTranslation”功能,我做了一点修改:

#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);
    for (it: text){
        translation += morseKey[it];
    }
    return translation;
}

如您所知,map是一个关联数组。 这意味着您无需遍历其所有元素即可找到您感兴趣的元素。 您应该将一个密钥与其对应的记录相关联。 在您的情况下,应将一个字符(不是字符串)与一个字符串关联; 因此,您应将“ morseKey”定义为:

map <char, string> morseKey;

当您要将“ A”等字符与“ .-”相关联时,您应该执行以下操作:

morseKey ['A'] = ".-"; /*instead of morsKey["A"] = ".-" */;

同样,当您在定义“翻译”变量时使用“自动”时,编译器会将其视为“ const char *”。 因此,您应该将“ translation”变量明确定义为:

string translation {""};

另外,由于我们的“ morseKey”映射仅包含大写字母,因此我们应将“ text”变量的字母字符转换为大写。 可以很容易地通过以下方法完成此操作:

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

但是要使用此命令,您应该包括两个库:

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

另外,您不应该再将“文本”变量视为右值 (因为我们对其进行了修改),因此我将函数delcarion更改为:

string getTranslation (string text) noexcept(true)  

最后,您应该只遍历“文本”变量,找到每个字符的相应摩尔斯值,并将其附加到返回值上; 这也可以通过以下操作很容易地完成:

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

玩得开心!

我对您的第二次修改的答复:

我认为您的信息还不够; 也许最好将您的问题作为一个新问题提出,并为其提供更多详细信息,例如在哪一行出现此编译错误,或者您认为有帮助的其他有用信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM