(How) can I count the items in an enum?
There's not really a good way to do this, usually you see an extra item in the enum, i.e.
enum foobar {foo, bar, baz, quz, FOOBAR_NR_ITEMS};
So then you can do:
int fuz[FOOBAR_NR_ITEMS];
Still not very nice though.
But of course you do realize that just the number of items in an enum is not safe, given e.g.
enum foobar {foo, bar = 5, baz, quz = 20};
the number of items would be 4, but the integer values of the enum values would be way out of the array index range. Using enum values for array indexing is not safe, you should consider other options.
edit: as requested, made the special entry stick out more.
Total number of items defined in an enum
You can use the static method Enum.GetNames
which returns an array representing the names of all the items in the enum. The length property of this array equals the number of items defined in the enum
var myEnumMemberCount = Enum.GetNames(typeof(MyEnum)).Length;
Number of elements in an enum
I don't believe there is. But what would you do with such a number if they are not sequential, and you don't already have a list of them somewhere? And if they are sequential but start at a different number, you could always do:
enum blah {
FIRST = 128,
SECOND,
THIRD,
END
};
const int blah_count = END - FIRST;
Is it possible to determine the number of elements of a c++ enum class?
Not directly, but you could use the following trick:
enum class Example { A, B, C, D, E, Count };
Then the cardinality is available as static_cast<int>(Example::Count)
.
Of course, this only works nicely if you let values of the enum be automatically assigned, starting from 0. If that's not the case, you can manually assign the correct cardinality to Count, which is really no different from having to maintain a separate constant anyway:
enum class Example { A = 1, B = 2, C = 4, D = 8, E = 16, Count = 5 };
The one disadvantage is that the compiler will allow you to use Example::Count
as an argument for an enum value -- so be careful if you use this! (I personally find this not to be a problem in practice, though.)
How to get number of possible items of an Enum?
Yes you can use the Enum.values()
method to get an array of Enum values then use the length
property.
public class Main {
enum WORKDAYS { Monday, Tuesday, Wednesday, Thursday, Friday; }
public static void main(String[] args) {
System.out.println(WORKDAYS.values().length);
// prints 5
}
}
http://ideone.com/zMB6pG
Determine the number of enum elements TypeScript
Typescript does not provide a standard method to get the number of enum elements. But given the the implementation of enum reverse mapping, the following works:
Object.keys(ExampleEnum).length / 2;
Note, for string enums you do not divide by two as no reverse mapping properties are generated:
Object.keys(StringEnum).length;
Enums with mixed strings and numeric values can be calculated by:
Object.keys(ExampleEnum).filter(isNaN).length;
How do I get the count of a Swift enum?
As of Swift 4.2 (Xcode 10) you can declare
conformance to the CaseIterable
protocol, this works for all
enumerations without associated values:
enum Stuff: CaseIterable {
case first
case second
case third
case forth
}
The number of cases is now simply obtained with
print(Stuff.allCases.count) // 4
For more information, see
- SE-0194 Derived Collection of Enum Cases
Find total number of items defined in an enum C#
Because your namespace is Enum as well. Its confusing the compiler. Try this:
namespace Enum
{
class Program
{
static void Main(string[] args)
{
Test test = new Test();
Console.WriteLine(test.FruitCount);
}
}
public class Test
{
enum Fruits { Apple, Orange, Peach }
public int FruitCount
{
get
{
return System.Enum.GetNames(typeof(Fruits)).Length;
}
}
}
}
I basically fully-qualified Enum with System.Enum.GetNames
elegant way of counting items in a list by enum value in python
Check out Counter. But beware using it requires hashable dict keys so you can't just use the class object.
from collections import Counter
c= Counter(map(lambda x:x.t,objList)) # it should be x.t.value if t is an intEnum
Related Topics
Visual C++: #Include Files from Other Projects in the Same Solution
How to Handle Failure in Constructor in C++
C++14 Variable Templates: What Is Their Purpose? Any Usage Example
What's the Meaning of * and & When Applied to Variable Names
Calling Python from a C++ Program for Distribution
Inheritance: 'A' Is an Inaccessible Base of 'B'
How to Use Visual Studio 2010's C++ Compiler with Visual Studio 2008's C++ Runtime Library
Convert Between String, U16String & U32String
Can a Pointer to Base Point to an Array of Derived Objects
What Are the Signs of Crosses Initialization
Double Free or Corruption After Queue::Push
How to Use Visual Studio Code to Compile Multi-Cpp File
How to Add a Library Path in Cmake
When to Use Const and Const Reference in Function Args
Moving Elements from Std::Vector to Another One