C++ unordered_map 没有从命令行参数中找到值

标签 c++ command-line-arguments unordered-map

我正在编写涉及元素周期表的 C++ 程序。我有一个 Table 类,其中包含一个指向所有元素对象的指针的 vector 和一个将元素名称和符号转换为原子数的 unordered_map。它重载 [] 运算符,以便 Table[const char*] 返回指向具有指定名称或符号的元素的指针。如果我将名称或符号硬编码到像 Table["H"] 这样的调用中,这就很好用了,但是如果我像 ./program H 这样调用程序并且尝试 Table argv[1] 即使 H 在表中并且 Table["H"] 工作正常,也会出现 out_of_range 错误。为什么当符号是命令行参数时它不按符号查找元素,但当符号是硬编码时它会查找元素?

这是我的代码,只有一个元素以使其尽可能简单:

#include <stdio.h>
#include <vector>
#include <unordered_map>
#include <cstring>
#include <stdlib.h>

using namespace std;

class Element{
public:
    const char* name;//name
    const char* sym;//symbol
    const int z;//atomic number
    const double m;//atomic mass
    const double p;//density
    const double mP;//melting point
    const double bP;//boiling point
    const double c;//specific heat
    const double eneg;//electonegativity
    const int gp;//group
    const int pd;//period
    Element(const char* n,const char* s,const int an,const double am,
            const double d,const double mp,const double b,
            const double sh,const double e,const int g,const int pd):
        name(n),sym(s),z(an),m(am),p(d),mP(mp),bP(b),c(sh),eneg(e),gp(g),pd(pd)
        {}
    Element(const Element& e):
        name(e.name),sym(e.sym),z(e.z),m(e.m),p(e.p),mP(e.mP),bP(e.bP),c(e.c),
        eneg(e.eneg),gp(e.gp),pd(e.pd)
        {}
    const Element& operator=(const Element& e){
        return e;
    }
};

Element ah("Surprise","Ah",0,0.0625,0.000001,4999 ,9998,999999,0,0,0);
Element* undiscovered=&ah;

class Table{
public:
    vector<const Element*> E_n;//vector<Element> E_n;
    unordered_map<const char*,int>E_ns;
    const Element* operator[](int i){
        return E_n[i];
    }
    const Element* operator[](const char* n){
        try{
            return E_n[E_ns.at(n)];
        }catch(out_of_range e){
            return undiscovered;
        }
    }
    int size(){
        return E_n.size();
    }
    void add(const Element& e){
        while(E_n.size()<=e.z)E_n.push_back(undiscovered);
        E_n[e.z]=&e;
        E_ns[e.name]=e.z;
        E_ns[e.sym]=e.z;
    }
    void print(){
        puts("\n");
        for(auto e:E_n){
            printf("%3d %13s(%3s). m:%7.3f, p:%10f, mp:%7.2f, bp:%7.2f\n",e->z,e->name,e->sym,e->m,e->p,e->mP,e->bP);
        }
    }
};

Table Elements;

int main(int argc,char **argv){
    int i;
    Elements.add(Element("Hydrogen","H",1,1.008,0.00009,14.1,20.28,14.304,2.20,1,1));
    i=Elements[argv[1]]->z;
    if(!i)i=atoi(argv[1]);
    if(!i){
        printf("There is no element \"%s\".\n",argv[1]);
        return 0;
    }
    printf("%f\n",Elements[i]->m);
    return 0;

}

最佳答案

两个 const char * 不相等,因为一个指向常量而另一个不指向常量。由于您使用指针对 map 进行了索引,因此指针必须相等才能使 find 成功。取而代之的是,使用 std::string 对映射进行索引,这样相等的字符串将定位该元素。

关于C++ unordered_map 没有从命令行参数中找到值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20166584/

相关文章:

c++ - 从 Visual C++ 中的 unordered_map 继承时,模板参数的 sizeof() 不正确

c++ - 如何忽略无序映射/映射中的单个字符串值?

c++ - 检查嵌套类模板的概念

c++ - 桌面 DirectX 表面 (WDM)

video - 此 ffmpeg 命令中的倒数第二个参数有什么作用?

algorithm - 从/向文件读取/写入 std::unordered_map 的更快方法

c++ - 将机器的IP地址发送到服务器

c++ - 何时使用 std::make_shared_for_overwrite?

python - argparse:多个可选位置参数之间的依赖关系

powershell - 在 Powershell 中处理字符串参数的 bool 值