マイクのおかげで、ここに完全なクラスがあります:
#include <stdio.h>
#include <assert.h>
using namespace std;
#include "map"
#include "deque"
class window{
private:
std::deque<int> numbers;
std::map<int, size_t> unique;
public:
window(){};
void push(int n) {
numbers.push_back(n);
++unique[n];
}
void pop() {
map<int, size_t>::iterator found = unique.find(numbers.front());
assert(found != unique.end());
assert(found->second > 0);
if (--found->second == 0) {
unique.erase(found);
}
numbers.pop_front();
}
size_t count_unique() {
return unique.size();
}
};
そしてこれはテストケースです:
int main(){
// example list '1 2 3 3 4 4 5 5 6 6 7 7 8'
int list[13] = {1, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8};
window w1;
w1.push(list[0]);w1.push(list[1]);
int i=0;
printf("The list is: ");
for(i=0; i<13; i++){
printf("%d ",list[i]);
}
printf("\n");
for(i=0; i<11; i++){
printf("[%d] Unique ones: %d\n",i+1,w1.count_unique());
w1.push(list[i+2]);w1.pop();
}
return 0;
}