A good way to keep the number of elements between braces?

Given type input

a { b c d { e f } g }

I want to parse it into one token at a time (letter or parenthesis). When I press the first closing bracket }, I need to know how many elements have been since the last opening bracket (e and f = 2). And then when I hit one after him, I need 4 (b, c, d, g).

It’s easy to grab 1 on 1 markers, but ... I don’t know how to count them. I was thinking about Stack<int>, but I can’t change the top element to enlarge it?

+3
source share
1 answer

Instead of trying to change the top element, why not save it only in a variable int.

  • , " " 0.
  • , " "
  • , , , , , "count until"

EDIT: , , , pop-increment-push. :

  • : 0
  • Letter: pop-increment-push
  • : , , ,

, , , , .

+8

Source: https://habr.com/ru/post/1771043/


All Articles