Get first N elements in a C++ multiset
问题 How can I get the first N elements from a multiset structure, without constantly getting the first (.begin()) element and then erasing it? I just want to sum the first N elements without affecting the multiset. 回答1: I just want to sum the first N elements without affecting the multiset. #include <numeric> #include <iterator> // ... int sum = std::accumulate(my_set.begin(), std::next(my_set.begin(), N)); std::next is a C++11 library addition. Here is a solution for older compilers: std: