0

Let`s say I have a class named Graph. I want to shorten the parameter list by using shorter type names. Is it possible to do that?

void Graph::APUtil(int u, vector <bool> visited, vector <bool> disc,  
                   vector <bool> low,  vector <bool> parent, vector <bool> ap)

I tried using auto but it does not work:

void Graph::APUtil(int u, auto visited, auto disc ...

I think since this is a class method lambda also does not work.

Any suggestion? I am asking this to save time in coding challenges.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
CS_EE
  • 399
  • 2
  • 10
  • have you considered using `typedef`? and or `#define`? – Minato Oct 31 '19 at 09:04
  • 1
    When you say "save time" do you mean your *own* time writing the code, or saving time for the compiler or execution? Because something like this will increase the compile-time as the compiler need to do more work. And it won't have any effect on run-time. – Some programmer dude Oct 31 '19 at 09:10
  • Also note that using type-aliases (introduced with either `typedef` or `using`) generally makes the code harder to read and understand, unless you create descriptive names with some semantic meaning which means you probably have just as much to write as before if not more. Using single-letter symbols or names is a bad habit that should never be used in "real life" (and unfortunately bad habits do tend to stick, so even initially limited use will spread). – Some programmer dude Oct 31 '19 at 09:12
  • @CS_EE as side note, are you sure of passing everything by copy? – Moia Oct 31 '19 at 09:26
  • @Moia this was just an example – CS_EE Oct 31 '19 at 10:18
  • @Someprogrammerdude I mean my own time. Since time is limited I aim to limit time spent on typing. – CS_EE Oct 31 '19 at 10:21
  • @CS_EE then use alias – Moia Oct 31 '19 at 12:54

2 Answers2

5

There are 3 ways to do this.

Macro [#define] should be avoided. typedef and using are equivalent, so you may use any of them.

Let's consider syntax of using type alias:

using identifier attr(optional) = type-id;

So, for your code you should write

using v = std::vector<bool>;
// . . .
void Graph::APUtil(int u, v visited, v disc,  
               v low, v parent, v ap);
xorover
  • 94
  • 9
  • 1
    `using` is superior to typedef and should be preferred. The explanation is in the same thread you linke. – Moia Oct 31 '19 at 09:24
3

You can use typedef to define shorten the typenames like.

typedef vector<bool> bvec;

or if you're using c++11 standard, the preferred way is using as suggested by @Yehor like

using bvec = vector<bool>;

in the end which ever option you use you can modify your method signature to

void Graph::APUtil(int u, bvec visited, bvec disc,  
           bvec low, bvec parent, bvec ap);
Minato
  • 4,383
  • 1
  • 22
  • 28
  • 1
    `using` is preferred over `typedef` – Moia Oct 31 '19 at 09:22
  • @Moia Thanks for the suggestion, `using` is one of the newer constructs, supported by c++11 and up, I prefer `typedef` since op didn't mention which standard he's using for his project. – Minato Oct 31 '19 at 09:32
  • keep in mind that "newer" means 9 years ago. At least C++11 should be considered as standard, not C++98. @Minato – Moia Oct 31 '19 at 09:37
  • @Moia "newer" is a relativistic term, and Again OP didn't specify the standard, in which case I prefer to use the older constructs. – Minato Oct 31 '19 at 09:39