vector - 将元素添加到嵌套向量

标签 vector clojure io

我不知道如何完成这件事,也许有人可以帮助我?我想做这样的事情:

vector<int> L[MAX_V];
for(int i=1;i<N;++i){
    scanf("%d %d",&u,&v);
    --u; --v;
    L[u].push_back(v);
    L[v].push_back(u);
}

,但是用 Clojure 语言。到目前为止,我已经有了类似的东西,但它不起作用:

(defn LoadTree []
  (def n (read-string (read-line)))
  (def tree (atom (into [] (repeat n []))))
  (loop [x n]
    (when (> x 1)
      (let [input (read-string (str "[" (read-line) "]"))]
        (swap! (get @tree (dec (get input 0))) conj (dec (get input 1))) <-Error
        (swap! (get @tree (dec (get input 1))) conj (dec (get input 0))) <-Error
      (recur (dec x))
      )
    )
  )

最佳答案

我会写这样的东西:

(defn foo [num-reads size]
  (loop [i num-reads, r (vec (repeat size []))]
    (if (zero? i) 
      r
      (let [[u v] (repeatedly read)]
        (recur (dec i), (-> r (update u conj v) (update v conj u)))))))

关于vector - 将元素添加到嵌套向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46839824/

相关文章:

xml - 使用clj-xpath在带有任意标签的clojure中解析xml

c# - 如何在 C# 中强制解锁文件?

java - PrintWriter 在下一行打印

c++ - 为什么不能将 vector<bool> 中元素的地址推回 vector<bool*>?

c++ - 考虑到干净代码的性能,什么更好

c++ - 保存数据指针

c++ - 在 vector 中找到最长的 'consecutive numbers' 条纹的最快方法是什么?

clojure - 内部惰性序列究竟是如何工作的

concurrency - Clojure:用于 IO 的 future、agent 或 core.async

java - GoF IO 装饰器模式的用例和示例