zoukankan      html  css  js  c++  java
  • 数据结构:set

    set即集合:可以存储任何类型数据,并且是唯一的。

    应用场景1:创建set

    var set = new Set();
    set.add(100);
    set.add("asdf");
    set.add({ name: "herry" });
    set.add("fn");
    set.add(100);
    console.log(set);
    
    //结果:{100, "asdf", {…}, "fn"}
    

    应用场景2:set长度计算

    console.log(set.size);
    //结果: 4
    

    应用场景3:判断set中是否包含某项

    console.log(set.has(100));
    console.log(set.has({ name: "herry" }));
    //结果:true   
            false(因为对象在内存中存储的是地址,而不是值,所以是false)
    

    应用场景4:删除set中某元素

    set.delete("fn");
    console.log(set);
    //结果:{100, "asdf", {…}}
    

    应用场景5:遍历set

    for (let item of set) {
      console.log(item);
    }
    //结果:100              "asdf"             {…}   
    

    应用场景6:set转为数组

    const arr = Array.from(set);
    console.log(arr);
    //结果:[100, "asdf", {…}]
  • 相关阅读:
    Codeforces 451A Game With Sticks
    POJ 3624 Charm Bracelet
    POJ 2127 Greatest Common Increasing Subsequence
    POJ 1458 Common Subsequence
    HDU 1087 Super Jumping! Jumping! Jumping!
    HDU 1698
    HDU 1754
    POJ 1724
    POJ 1201
    CSUOJ 1256
  • 原文地址:https://www.cnblogs.com/yxkNotes/p/11550386.html
Copyright © 2011-2022 走看看