是否可以使用预处理器对数组进行排序? [英] Is it possible to sort arrays using preprocessor?

查看:108
本文介绍了是否可以使用预处理器对数组进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多非常长的数组。没有运行时排序是可能的。手动分类也很费时。此外,新元素可以随后以任何顺序添加,因此我希望使用 C 预处理器按价值对它们进行排序,或者可能有任何编译器标志(GCC)?



例如:

  sometype S [] = {
{somevals,BOB,otherothervals},
{somevals,ALICE,otherothervals},
{somevals,TIM,otherothervals},
}

必须按以下顺序排列:

  sometype S [] = {
{somevals,ALICE,otherothervals},
{somevals,BOB,otherothervals},
{somevals,TIM ,
}



已解决



好的,这里是我的解决方案:


  1. 手动复制并粘贴每个数组到一个名为tobesorted.c的临时文件中。
  2. 按第二列对其进行排序: sort -b -i --key = 2 tobesorted.c


  3. $ b
  4. 其实,它会很好有一些可能性直接从预处理器调用sort(我希望至少GCC能以某种方式支持这些特性,但它本身ems that it does not)。

    解决方案

    不,这是不可能的。您不能对预处理器执行字符串操作(除了串接)。你无法将字符串与模板元编程相比较。



    你可以做的是将你的数据结构放在一个文件中是由外部构建脚本(例如unixsort工具)预处理的,然后修改你的makefile / project,以便在构建时生成一个带有(排序)初始化数组的C文件。 b $ b

    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:

    1. Manually copy&paste each array into a temporary file called tobesorted.c
    2. Sort it by 2nd column: sort -b -i --key=2 tobesorted.c
    3. 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).

    解决方案

    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

    这篇关于是否可以使用预处理器对数组进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆