cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
General C++ Programming
Sort and Sum
Sort and Sum
Jun 23, 2014 at 10:15pm UTC
Ozymandias
(5)
Hi.! can anyone tell me what is wrong with this function.?
double sort_sum(std::vector<double>& vec)
{
std::multiset<double> mset(vec.begin(), vec.end());
while (mset.size() > 1) {
std::multiset<double>::const_iterator itA = mset.begin();
std::multiset<double>::const_iterator itB = ++mset.begin();
double sum = *itA + *itB;
mset.erase(itA, itB);
mset.insert(c);
}
return !mset.empty() ? *mset.begin() : 0.0;
}
I want to determine the sum of all elements of vector vec and return it. I have to do it exactly this way, by using sets...However this is an infinite loop.
Jun 23, 2014 at 10:38pm UTC
ne555
(10692)
http://www.cplusplus.com/reference/set/multiset/erase/
the range is [), so in every iteration you erase one element and insert one element.
Topic archived. No new replies allowed.