I have a list of framerates and a list of associated screen resolutions. The problem is that I get loads of duplicates in my resoltuions list. Actually, I get loads of duplicates in the framerates list as well (thanks a lot, Windows!), but those are eliminated by a simple comparison when I sort through the raw list. I guess that I want to use std::algorithm to help me eliminate my duplicates, but I can't get it to work.
Code:
#include <algorithm>
#include <vector>
#include <iostream>
struct resolution {
int w;
int h;
};
int main(void) {
std::vector<std::pair<int, std::vector<resolution>>> vec;
for (int i = 0; i < 5; i++) {
std::pair<int, std::vector<resolution>> p;
p.first = i;
for (int j = 0; j < 5; j++) {
resolution res;
res.w = j;
res.h = j;
p.second.push_back(res);
}
vec.push_back(p);
}
for (std::vector<std::pair<int, std::vector<resolution>>>::iterator it = vec.begin();
it != vec.end();
++it) {
it->second.erase(std::unique(it->second.begin(), it->second.end()), it->second.end());
}
for (std::vector<std::pair<int, std::vector<resolution>>>::iterator it1 = vec.begin();
it1 != vec.end();
++it1) {
for (std::vector<resolution>::iterator it2 = it1->second.begin();
it2 != it1->second.end();
++it2) {
std::cout << it1->first << ": " << it2->w << " x " << it2->h << std::endl;
}
}
return 0;
}
I get the following compiler errors from algorithm:
Error C2672 'operator __surrogate_func': no matching overloaded function found dummy_erase c:\program files (x86)\microsoft visual studio 14.0\vc\include\algorithm 1503
Error C2893 Failed to specialize function template 'unknown-type std::equal_to<void>::operator ()(_Ty1 &&,_Ty2 &&) const' dummy_erase c:\program files (x86)\microsoft visual studio 14.0\vc\include\algorithm 1503
Error C2672 'operator __surrogate_func': no matching overloaded function found dummy_erase c:\program files (x86)\microsoft visual studio 14.0\vc\include\algorithm 1506
Error C2893 Failed to specialize function template 'unknown-type std::equal_to<void>::operator ()(_Ty1 &&,_Ty2 &&) const' dummy_erase c:\program files (x86)\microsoft visual studio 14.0\vc\include\algorithm 1506
I've not much experience with <algorithm> but I suspect the problem is that std::unique doesn't understand how to compare my resolution objects. If this is in fact the case, is there something I can add to my resolution class to make it nice for std::unique? Or, is there any other way to do this elegantly?
I could always just do it by hand with another pass on the list and some conditionals, but avoiding doing that kind of thing is exactly one reason to use C++ as opposed to C. If there is a trick to this, I'd love to know it.
Aucun commentaire:
Enregistrer un commentaire