foreach forall 차이

자료실 2015. 8. 24. 16:27

class PersonClass: NameListClass {

  property uint Age;


  PersonClass():

    NameListClass()

  {

  }


  PersonClass(NameListClass parent = null, const String& name = "", uint age = 0):

    NameListClass(parent, name),

    Age = age

  {

  }

}


class MainClass {

  void Main() {

    SystemClass::ClearConsole(SystemClass::SHOWCONSOLE);

    PersonClass family();

    PersonClass bob(family, "Bob", 20);

    new PersonClass(family, "Jane", 12);

    new PersonClass(family, "Nick", 82);

    new PersonClass(bob, "Clyde", 45);

    ForeachDump(family);

    ForallDump(family);

    NextDump(family);

    if (NodeClass nick = family.Find("Nick"))

      family.Remove(nick);

    Console.WriteLine("removed nick");

    ForallDump(family);

    family.Insert(new PersonClass(null, "Harold"), NodeClass::INSERTAFTER, bob);

    Console.WriteLine("inserted harold between bob and jane");

    ForallDump(family);

    Console.WriteLine("toplevel children: {0}", family.Count());

    Console.WriteLine("all children: {0}", family.CountAll(0, NodeClass::COUNTNODES | NodeClass::COUNTFOLDERS | NodeClass::COUNTRECURSIVE));

    PersonClass otherFamily();

    otherFamily.Insert(bob);

    Console.WriteLine("after inserting bob into otherfamily");

    ForallDump(family);

    Console.WriteLine("otherfamily: ");

    ForallDump(otherFamily);

    if (NodeClass clyde = FindByProperty(otherFamily, "Age", 45))

      Console.WriteLine("found: {0}", clyde.Name());

  }


  void ForeachDump(PersonClass persons) {

    Console.WriteLine("=== foreach dump ===");

    foreach (PersonClass person in persons)

      DumpPerson(person);

  }


  void ForallDump(PersonClass persons) {

    Console.WriteLine("=== forall dump ===");

    forall (PersonClass person in persons)

      DumpPerson(person);

  }


  void NextDump(PersonClass persons) {

    Console.WriteLine("=== next dump ===");

    for (PersonClass current = persons.FirstChild(); current != null; current = current.Next())

      DumpPerson(current);

  }


  static NodeClass FindByProperty(NodeClass root, const String& propertyName, const String& value) {

    forall (NodeClass node in root) {

      if (PropertyClass prop = node.DynamicType().FindProperty(propertyName)) {

        variant v;

        v.GetProperty(node, prop);

        String vStr = v;

        if (vStr == value)

          return node;

      }

    }

    return null;

  }


  static void DumpPerson(PersonClass person) {

    Console.WriteLine("{0} is {1}", person.Name(), person.Age);

  }

}










----------------------------------결과-------------------------------------------
=== foreach dump ===
Bob is 20
Jane is 12
Nick is 82
=== forall dump ===
Bob is 20
Clyde is 45
Jane is 12
Nick is 82
=== next dump ===
Bob is 20
Jane is 12
Nick is 82
removed nick
=== forall dump ===
Bob is 20
Clyde is 45
Jane is 12
inserted harold between bob and jane
=== forall dump ===
Bob is 20
Clyde is 45
Harold is 0
Jane is 12
toplevel children: 3
all children: 4
after inserting bob into otherfamily
=== forall dump ===
Harold is 0
Jane is 12
otherfamily: 
=== forall dump ===
Bob is 20
Clyde is 45
found: Clyde


'자료실' 카테고리의 다른 글

d3dx9_39.dll  (0) 2015.09.28
컴퓨터 시세  (0) 2015.09.08
국민은행 화상키보드 없애기 / 사용 안하기  (0) 2015.08.13
http://studyforus.tistory.com 유용한 자료  (0) 2015.08.02
분석서버 JEB 사용법  (0) 2015.07.24
Posted by wakira
,