Home > Blockchain >  what does '#' mean in cpp code? here's a code for reference . I stuck at " char
what does '#' mean in cpp code? here's a code for reference . I stuck at " char

Time:12-15

bool isValid(string s) {
    stack<char> st;



    for(int i=0;i<s.length();i  ) {
        char top = st.empty() ? '#' : st.top();
    

The code is the solution for 'Valid Parenthesis' on LeetCode.

CodePudding user response:

When you wrote:

char top = st.empty() ? '#' : st.top();

The above checks if st is empty. If st is empty then top is initialized with the character literal '#'. Otherwise, top is initialized with the character returned from st.top().

Also, '#' is a character literal.

CodePudding user response:

'#' is a literal of type char. (Note it's an int type in C.)

It's used in this code as a placeholder for the top of an empty stack. Whether or not that's a good idea (is there any issue arising from a possible ambiguity with a stack explicitly containing '#'?) is another issue.

  •  Tags:  
  • c
  • Related