questions of structured data

I can understand the first row and last two rows.
However, does anyone know what the remaining part means ????(line3-line16)
especially for line3-line5


1 struct Bracket
2 {
3 Bracket(char type, int position):
4 type(type),
5 position(position)
6 {}

7 bool Matchc(char c)
8 {
9 if (type == '[' && c == ']')
10 return true;
11 if (type == '{' && c == '}')
12 return true;
13 if (type == '(' && c == ')')
14 return true;
15 return false;
16 }

17 char type;
18 int position;
19 };
Last edited on
There is a second syntax for writing what you have a bit differently:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct Bracket
{
 Bracket(char type, int position);
 bool Matchc(char c);
 char type;
 int position;
};


Bracket::Bracket(char type, int position)
 : type(type), position(position)
{}


bool Bracket::Matchc(char c)
{
 if (type == '[' && c == ']')
  return true;
 if (type == '{' && c == '}')
  return true;
 if (type == '(' && c == ')')
  return true;
 return false;
}

Have you seen terms member function and constructor anywhere? They are usually mentioned with classes, but in C++ class and struct are very similar. With constructor, one can learn about initializer list.
Last edited on
Lines 3-6 are a constructor. However, lines 4-5 are problematic. The argument names conflict with the member variables at lines 17-18. Lines 4-5 initialize the arguments to themselves.

Lines 7-16 are a simple member function that checks if type and c are matching types of brackets, braces or parens. If so, true is returned. If not, false is returned.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Do not number the lines yourself.
If you put the code in the proper format and remove the line numbers it becomes more obvious.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
struct Bracket
{
  Bracket(char type, int position) :
    type(type),
    position(position)
  {}

  bool Matchc(char c)
  {
    if (type == '[' && c == ']')
      return true;
    if (type == '{' && c == '}')
      return true;
    if (type == '(' && c == ')')
      return true;
    return false;
  }

  char type;
  int position;
};

Line 3-6 define a constructor.
Line 8 to 17 define a member function.
Line 19-20 define 2 member variables, though position is not used.
Using std::regex, with escape characters and the regex separator for the Matchc function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# include <iostream>
# include <regex>
# include <string>
# include <iomanip>

bool Matchc (char type, char c)
{
    return std::regex_match(std::string{type, c},
            std::regex("(\\\(\\))|(\\\{\\})|(\\\[\\])"));
}

int main()
{
   std::cout << std::boolalpha << Matchc('(', ')') << " " << Matchc('{', '}')
                << " " << Matchc('[', ']') << " " << Matchc('[', ')');
    //prints true - true - true - false
}


see here for introduction to c++ regex: http://www.cplusplus.com/reference/regex/ECMAScript/
pay particular attention to escape characters and separators
Thanks,guys.

I need to read constructor parts of c++. I never learn that part before.
Topic archived. No new replies allowed.