Is there a function to return the size of a multi_index table? If not is there a better way then looping through them? uint64_t count; for (auto itr = table.begin(); itr != table.end(); ++itr) { ++count; }

2 Answers 2


Agree no size is maintained by EOS implementation , the iterators are not random access , so if size state not maintained (which would be best performance compared to calculating size on the fly when needed) then more compact expression to get the size ( if you really need it as I don't see the use case yet) is to use std::count

auto size = std::count(table.cbegin(),table.cend());
  • I had to make that: auto size = std::distance(table.cbegin(),table.cend()); Sep 20, 2018 at 9:34
  • @MirkoLindner - it's going to be same complexity as std::count as std::distance has to iterate it can't just calculate distance because those iterators , as multi_index iterators are not random access like in std::vector where all elements are continuous. If you use std::distance on vector it's just end - begin pretty much . stackoverflow.com/a/12530248/2264070 Sep 23, 2018 at 22:30
  • 1
    This does not compile. std::count requires a third parameter.
    – hailinzeng
    Jun 7, 2020 at 23:40

I didn't find one in the documentation. But maybe you can maintain the variable yourself in the contract state:

Increase on add
Decrease on remove

If you store it in a multi_index you could use a member table which will maintain the variable.

ID (unint64_t) | Attribut (String) | Value (String)
0              | 'mCounter'        | '5' 
  • hmm, that's obnoxious there's no len() or count() for multi-index -- thank you
    – Alex
    Jul 21, 2018 at 22:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.