求职面试问题——数据结构
假设我有一组文件,并且每个文件中都有 include# 到同一组中的其他文件。假设我有加载某个文件中所有包含内容的函数。我需要编写加载某个给定源文件中所有包含内容的函数所以我将避免无限循环和循环调用包含。
为了解决这个问题,我必须使用这个函数,我不能使用 pragma 一次或类似的东西 我认为可以通过递归来解决,但我不确定如何解决,
谢谢
Suppose I have set of files and in every files there are include# to other files in the same set.Suppose I have function that load all the includes in certain file.I need to write function that load all the includes in some given source file so I will avoid infinite loop and circular call for the include.
To solve the question I have to use this function i can't use pragma once or something similar
I think it could be solved by recursion though I am not sure how
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
定义一个预处理器变量,通常为“CLASSNAME_H”,
检查该变量是否已定义,如果没有,则定义它并执行代码。例如:
这就是你所说的,对吗?
Define a preprocessor variable, typically 'CLASSNAME_H'
Check to see if the variable is defined and, if not, then define it and execute the code. For example:
This is what you're talking about, right?
来自 Wiki:
From Wiki:
我认为您想查看拓扑排序。
I think that you want to look at Topological sorting.
从您的评论来看,您似乎无法对包含防护或
#pragma Once
使用正常检查。另一种方法是保存字符串中包含的每个文件的绝对(或相对)路径,如果再次遇到包含路径,则不包含该文件。From your comments it seems you can't use the normal check for include guards or
#pragma once
s. Another way is to save the absolute (or maybe relative) path for every file you include in a string and if you encounter on of the include pathes again, don't include the file.