是否可以使用预处理器对数组进行排序?
我有很多很长的数组。不可能进行运行时排序。手动对它们进行排序也很耗时。此外,以后可以按任意顺序添加新元素,因此我想使用 C 预处理器按值对它们进行排序,或者可能有任何编译器标志(GCC)?
例如:
sometype S[] = {
{somevals, "BOB", someothervals},
{somevals, "ALICE", someothervals},
{somevals, "TIM", someothervals},
}
必须排序:
sometype S[] = {
{somevals, "ALICE", someothervals},
{somevals, "BOB", someothervals},
{somevals, "TIM", someothervals},
}
SOLVED
好的,这是我的解决方案:
- 手动将每个数组复制并粘贴到名为 tobesorted.c 的临时文件中,
- 按第二列排序:
sort -b -i --key=2 tobesorted.c
- 复制并粘贴输出回原始文件。
实际上,如果能够直接从预处理器调用“sort”就太好了(我希望至少 GCC 以某种方式支持此类功能,但似乎并不支持)。
I have a number of very long arrays. No run-time sort is possible. It is also time consuming to sort them manually. Moreover, new elements can be added in any order later, so I would like to sort them by value using C preprocessor or maybe there is any compilers flag (GCC)?
For example:
sometype S[] = {
{somevals, "BOB", someothervals},
{somevals, "ALICE", someothervals},
{somevals, "TIM", someothervals},
}
must be sorted so:
sometype S[] = {
{somevals, "ALICE", someothervals},
{somevals, "BOB", someothervals},
{somevals, "TIM", someothervals},
}
SOLVED
Ok, here is my solution:
- Manually copy&paste each array into a temporary file called tobesorted.c
- Sort it by 2nd column:
sort -b -i --key=2 tobesorted.c
- Copy&paste output back into original file.
Actually, it would be nice to have some possibility to call "sort" directly from the preprocessor (I had a hope that at least GCC somehow support such features, but it seems that it doesn't).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这样做。
将您的巨型数组放入文件中。
使用内置
sort
对文件进行排序使用内置
编写一个小程序来创建C代码从文件中。写C程序的AC程序就可以了。您可以使用 Python 及其一些很酷的模板包来简化这项工作。
编译由转换为 C 代码的排序文件和程序的其余部分组成的剩余程序。
编译由转换
Do this.
Put your giant array in a file.
Sort the file with the built-in
sort
Write a small program to create C code from the file. A C program that writes C programs is fine. You can use Python and some of it's cool template packages to make this job simpler.
Compile the remaining program consisting of the sorted file transformed into C code plus the rest of your program.
不,这是不可能的。您不能使用预处理器执行字符串操作(连接除外)。而且您也无法将字符串与模板元编程进行比较。
[编辑]您可以做的是将数据结构放入一个文件中,该文件将由外部构建脚本(例如unix“排序”实用程序)进行预处理,然后修改您的makefile/项目,以便在构建时,您生成一个带有(已排序的)初始化数组的 C 文件
No, it is not possible. You cannot do string operations (other than concatenation) with the preprocessor. And you can't compare strings with template metaprograming, either.
[edit] What you could do is put your datastructure in a file that is meant to be preprocessed by an external build script (e.g. the unix "sort" utility), and then modify your makefile/project so that at build time, you generate a C file with the (sorted) initialized arrays
我不认为你可以在 gcc 预处理器中做到这一点,从来没有见过可以做你正在寻找的事情的东西。
但是您可以用您最喜欢的脚本语言(python、perl、sed 等)编写自己的“预处理器”,它会在 gcc 启动之前对这些值进行排序。
I don't think you can do it in the gcc preprocessor, never seen something that could do what you are looking for.
But you could write your own "preprocessor" in your favourite scripting language (python, perl, sed etc...) that would sort those values before gcc kicks in.
我认为不可能使用预处理器,但您可以使用
sort
和#include
的组合来实现所需的效果:仅将值放入单独的文件中
values.h
排序键位于前面(您需要为此重新排列您的struct sometype
):在您的 Makefile 中,使用Unix 命令
sort
将该文件排序到values_sorted.h
中:在您的实际代码中,包含排序后的文件:
I can think of no possibility to use the preprocessor, but you can use a combination of
sort
and#include
to achieve the desired effect:Put just the values into a seperate file
values.h
with the sort key being in front (you will need to rearrange yourstruct sometype
for this):In your Makefile, use the Unix command
sort
to sort that file intovalues_sorted.h
:In your actual code, include the sorted file:
以下适用于两个和三个元素:
但我想说,很明显,这种方法不能推广到任意大小的数组。
我认为这是不可能的,但我仍然没有这个猜想的正式证明。
The following worked for two and three elements:
But I'd say that it is clear that this approach does not generalize to arbitrary sized arrays.
I suppose it is not possible, but I still don't have a formal proof for this conjecture.