【问题标题】:Is there a data structure similar to std::set in TCL?TCL中是否有类似std::set的数据结构?
【发布时间】:2012-01-07 17:29:07
【问题描述】:

TCL 有一个名为dict 的数据结构,它维护着一组键值对。

是否有另一种数据结构维护一组键(没有值)?

如果不是,那么也许有人已经在 dict 上写了一个带有空值的简单适配器?

【问题讨论】:

    标签: list data-structures dictionary set tcl


    【解决方案1】:

    你可以使用 tcllib 包::struct::set

    http://tcllib.sourceforge.net/doc/struct_set.html

    【讨论】:

      【解决方案2】:

      只使用一个列表。

      set example [list "key1" "key2" "key3"]
      if {[lsearch -exact $example "key3"] != -1} {
          puts "found your key!"
      } else {
          puts "your key is not in the list"
      }
      

      也许您应该问一个更具体的问题以获得更准确的答案。 dict 的替代方案是 array,它不保留键的顺序。

      【讨论】:

      • 您可以使用if {"key3" in $example} { ... 代替lsearch(8.5 中引入的运算符)。
      • 老实说,我认为这个问题非常具体地说明了它在寻找什么。列表、映射和集合都是具有不同属性的不同数据结构。
      • 这不是一个好的答案,因为搜索不是 O(1)。
      【解决方案3】:

      另一种方法是将所有内容累积成$bucket

      然后做:

      set uniqueItems [lsort -unique $bucket]
      

      【讨论】:

        猜你喜欢
        • 2011-05-19
        • 2010-09-05
        • 1970-01-01
        • 1970-01-01
        • 2015-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多