将函数名称和属性存储在哈希表中
Store function name with attributes in hash table
假设我有以下程序:
__attribute__((annotate("ex"))) void ex() {};
void val() {};
__attribute__((annotate("foo"))) void foo() {
ex();
cout<<"You have called a function with an attribute"<<endl;
val();
cerr<<"Error!You have called a function without an attribute"<<endl;
}
int main()
{
foo();
return 0;
}
如果你观察,main() 调用 foo() 和 foo() 依次调用 ex() 和 val()。当调用 ex() 时没有问题,因为它有一个与之关联的属性,但是当它调用 val() 时,它应该显示错误消息,说明此函数没有任何属性。
因此,为此,我需要跟踪哈希表中具有属性和没有属性的函数,并在调用函数时对其进行检查。
而且,我不知道该怎么做?我希望现在清楚了。
看起来你需要使用结构或类。
我将给你一个参数的看法,你可以扩展到一个函数。
函数参数具有名称和类型:
struct Function_Parameter
{
std::string name;
std::string type;
};
函数还具有名称和返回类型:
struct Function_Token
{
std::string name;
std::string return_type;
};
一个函数可以有零个或多个参数。 因此通常意味着某种容器。 因此,请使用std::vector
:
struct Function_Token
{
std::string name;
std::string return_type;
std::vector<Function_Parameter> parameters;
};
许多编译器可能会将名称转换为数值(使用某种哈希函数或表)。 这通常会加快编译速度,因为字符串需要很长时间才能比较。
相关文章:
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 用C++将哈希表写入文件并从文件中恢复
- C++中的并发哈希表
- 在具有开放寻址的哈希表中插入节点 [优化逻辑]
- 与C++哈希表的基础知识混淆
- 调整大小和复制哈希表数组中的元素
- 带链接的基本哈希表
- C++哈希表中,两个相同的实现,但一个给出错误
- 如果索引不是整数,我们如何在 C++ 中插入哈希表
- 查找项目在哈希表中的位置
- 为什么C++ STL 哈希表 (unordered_map) 不接受向量作为键
- C++哈希表 - 如何解决自定义数据类型作为键的unordered_map冲突?
- 使用哈希表设置实现
- 创建用于存储某些指针值的哈希表
- 将函数名称和属性存储在哈希表中
- 如何存储需要随机选择和删除的数据,矢量,哈希表或其他
- 如何提高具有100万个元素和997个存储桶的哈希表的性能
- 在不使用预定义的C++函数的情况下,按字母顺序打印存储在哈希表中每个索引处的名称
- 哈希函数和哈希表中的存储