English 中文(简体)
建立静态无序的静态——由静态无序的钥匙产生——地图
原标题:Creating a static unordered_set from keys of a static unordered_map

I m 为汇编者撰写前端,目前正在实施校正仪扫描功能。 我将使用<代码>Punctuator等舱,代表我方的输入源文档中的教员,此外,它也是一些静态扫描方法的所在地。 这里是辅导员。

#ifndef PUNCTUATOR_H_
#define PUNCTUATOR_H_

#include <string>
#include <unordered_map>
#include <unordered_set>
#include <string>

#include "reserved_component.h"

enum class PunctuatorEnum {
    OPEN_BRACE,
    CLOSE_BRACE,
    TERMINATOR,
    EQUAL,
    PLUS,
    MINUS,
    MULTIPLY,
    DIVIDE,
};

class Punctuator : public ReservedComponent {
public:
    Punctuator(std::string lexeme);
    Punctuator(Punctuator&& punctuator);

    static bool IsPunctuator(std::string buffer);
    static PunctuatorEnum ForLexeme(std::string buffer);

private:
    PunctuatorEnum punctuator_enum;
    static std::unordered_map<std::string, PunctuatorEnum> dictionary;
};

#endif // PUNCTUATOR_H_

由于你能够使用静态<代码>unordered_map,储存可能的旁听器的“字典”,然后使用该地图检查某一方的护卫是否代表旁人。 我将地图放在旁边,c 在这里:

#include "punctuator.h"

std::unordered_map<std::string, PunctuatorEnum> Punctuator::dictionary = {
    { "{", PunctuatorEnum::OPEN_BRACE },
    { "}", PunctuatorEnum::CLOSE_BRACE },
    { ".", PunctuatorEnum::TERMINATOR },
    { "=", PunctuatorEnum::EQUAL },
    { "+", PunctuatorEnum::PLUS },
    { "-", PunctuatorEnum::MINUS },
    { "*", PunctuatorEnum::MULTIPLY },
    { "/", PunctuatorEnum::DIVIDE }
};

Punctuator::Punctuator(std::string lexeme) : ReservedComponent(lexeme) {
    this->punctuator_enum = dictionary.at(lexeme);
}

Punctuator::Punctuator(Punctuator&& punctuator) : ReservedComponent(std::move(punctuator)) {
    this->punctuator_enum = std::move(punctuator.punctuator_enum);
}

bool Punctuator::IsPunctuator(std::string buffer) {
    return dictionary.contains(buffer);
}

PunctuatorEnum Punctuator::ForLexeme(std::string lexeme) {
    return dictionary.at(lexeme);
}

I understand this is not the cleanest approach as this forces me to maintain the PunctuatorEnum and the unordered map, but it s what I ve chosen so far. I d also like to implement a method StartsPunctuator(char c) to check whether a given char starts a punctuator.

My question is as follows: Is it possible to declare a static unordered_set variable in my Punctuator class, say punctuator_first_char, and have it initialized to store only the first chars of every entry of the key-set of the static member variable dictionary? This way I would be able to simply call the contains method on punctuator_first_char in my StartsPunctuator(char ch) method without having to iterate through dictionary.

最佳回答

能否在我的<代码>Punctuator类别上宣布static unordered_setps, 说punctuator_first_char/code>, 是否初步将其储存在固定成员变量的关键条目-dictionary 上的每一条目中的第一个焦炭?

是:

#include <unordered_set> 

class Punctuator : public ReservedComponent {
   //...
private:
    PunctuatorEnum punctuator_enum;
    static std::unordered_map<std::string, PunctuatorEnum> dictionary;
    static std::unordered_set<char> punctuator_first_char;
};

初始:

#include <algorithm> // std::transform
#include <iterator>  // std::inserter

std::unordered_map<std::string, PunctuatorEnum> Punctuator::dictionary = { ... };

std::unordered_set<char> Punctuator::punctuator_first_char = [] {
    std::unordered_set<char> rv;
    std::transform(dictionary.begin(), dictionary.end(),
                   std::inserter(rv, rv.end()),
                   [](auto&& str_punctenum) { return str_punctenum.first[0]; });
    return rv;
}();
问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?