title: How do you determine what kind of node you are dealing with in yaml-cpp?
body: <p>I'm reading the tutorial code here: <a href="https://code.google.com/p/yaml-cpp/wiki/Tutorial" rel="nofollow">https://code.google.com/p/yaml-cpp/wiki/Tutorial</a></p>
  
  <p>One example goes like this:</p>
  
  <pre><code>YAML::Node primes = YAML::Load("[2, 3, 5, 7, 11]");
  
  for (YAML::const_iterator it=primes.begin();it!=primes.end();++it) {
    std::cout &lt;&lt; it-&gt;as&lt;int&gt;() &lt;&lt; "\n";
  }
  </code></pre>
  
  <p>And the next like this:</p>
  
  <pre><code>YAML::Node lineup = YAML::Load("{1B: Prince Fielder, 2B: Rickie Weeks, LF: Ryan Braun}");
  
  for(YAML::const_iterator it=lineup.begin();it!=lineup.end();++it) {
    std::cout &lt;&lt; "Playing at " &lt;&lt; it-&gt;first.as&lt;std::string&gt;() &lt;&lt; " is " &lt;&lt; it-&gt;second.as&lt;std::string&gt;() &lt;&lt; "\n";
  }
  </code></pre>
  
  <p>However, if you swap the YAML files between these two cases, you will get an error, as you are accessing a map iterator for a sequence or vice versa:</p>
  
  <pre><code>terminate called after throwing an instance of 'YAML::InvalidNode'
  what():  yaml-cpp: error at line 0, column 0: invalid node; this may result from using a map iterator as a sequence iterator, or vice-versa
  </code></pre>
  
  <p>For an arbitrary YAML input, how can I determine whether I am dealing with a sequence or a map in the loop (ie whether I should be using ->first or not) without using a try/catch block? </p>
  
  <p>I tried looking for the documentation, but I could not find it.</p>
  
  <p><strong>UPDATE</strong>:</p>
  
  <p>This is what I am trying to do:</p>
  
  <pre><code>YAML::Node config = YAML::LoadFile(filename);
  
  for (YAML::const_iterator it=config.begin();it!=config.end();++it) {
      if (it-&gt;Type() == YAML::NodeType::Map) { // exception
          std::cout &lt;&lt; it-&gt;first.as&lt;std::string&gt;();
      } else if (it-&gt;Type() == YAML::NodeType::Sequence) {
          std::cout &lt;&lt; it-&gt;as&lt;std::string&gt;();
      }
  }
  </code></pre>
  
  <p>But when I run the code I get the exception as above. It compiles fine.</p>
  
  <p>I am using the yaml-cpp which comes with ubuntu 14.04 (0.5.1).</p>