Enum

I'm trying to learn how to use enum. Can I in someway use enum with functions? Like the three below, all three is different ways of sorting a list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//----------------------------------------------------------------------------
//Sort persons by name
//----------------------------------------------------------------------------
void sortbyName(vector<Person>& persons)
{
    sort(persons.begin(), persons.end(), [](const Person& lhs, const Person& rhs)
         {
             if(lhs.lastname != rhs.lastname)
             {
                 return strcmp(lhs.lastname.c_str(), rhs.lastname.c_str()) < 0;
             }
             else
             {
                 return strcmp(lhs.firstname.c_str(), rhs.firstname.c_str()) < 0;
             }
             return strcmp(lhs.lastname.c_str(), rhs.lastname.c_str()) < 0;
         });
    
    cout << "List is now sorted by name(last name)";
}

//----------------------------------------------------------------------------
//Sort persons by signature
//----------------------------------------------------------------------------

void sortbySign(vector<Person>& persons)
{
    sort( persons.begin( ), persons.end( ), [ ]( const Person& lhs, const Person& rhs )
         {
             if(lhs.sign != rhs.sign)
             {
                 return lhs.sign < rhs.sign;
             }
             else
             {
                 return lhs.lastname < rhs.lastname;
             }
             
         });
    
    
    cout << "List is now sorted by signature";
}

//----------------------------------------------------------------------------
//Sort persons by length
//----------------------------------------------------------------------------

void sortbyLength(vector<Person>& persons)
{
    
    sort( persons.begin( ), persons.end( ), [ ]( const Person& lhs, const Person& rhs )
         {
             if(lhs.length != rhs.length)
             {
                 return lhs.length > rhs.length;
             }
             else
             {
                 return lhs.lastname < rhs.lastname;
             }
             
         });
    
    cout << "List is now sorted by length";
}
You mean you want to use an enum to decide what sort function to call?

You could do something like this ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
enum class PersonSortOrder
{
	name,
	sign,
	length	
};

void sort(vector<Person>& persons, PersonSortOrder sortOrder)
{
	switch (sortOrder)
	{
	case PersonSortOrder::name:
		sortbyName(persons);
		break;
	case PersonSortOrder::sign:
		sortbySign(persons);
		break;
	case PersonSortOrder::length:
		sortbyLength(persons);
		break;
	}
}

... and when you call the function you just pass in whatever enum value you want.

 
sort(persons, PersonSortOrder::name);


You can of course create variables, assign values and pass them to functions just like you do with other types.

1
2
PersonSortOrder sortOrder = PersonSortOrder::length;
sort(persons, sortOrder);



You shouldn't have to use c_str() and strcmp() when comparing two std::string objects.

This
 
strcmp(lhs.lastname.c_str(), rhs.lastname.c_str()) < 0
could be written as
 
lhs.lastname.compare(rhs.lastname) < 0
or simply
 
lhs.lastname < rhs.lastname
Last edited on
Yes!
Thank you @Peter87
Topic archived. No new replies allowed.