Reputation: 20895
I am trying to use an unordered_set
from the C++ std library. I am using the std namespace.
using namespace std;
The unordered_set
is within a function of mine. I would like to use it to memoize some values.
int do_crazy_calculations(int n) {
static unordered_set<int> done_before;
done_before::iterator node_found = done_before.find(n);
// n has not been seen before, so do calculations and memoize the result.
if (node_found == done_before.end()) {
int result = actually_do_calculations(n);
done_before.insert(n, result);
return result;
}
// n has already been seen before, just return the memoized value.
else {
return node_found.get();
}
}
However, I am getting this compilation error:
CplusplusExperiment.cpp: In function
'int do_crazy_calculations(int)'
:
CplusplusExperiment.cpp:10:10: error:'unordered_set'
does not name a type
make: *** [CplusplusExperiment.o] Error 1
However, I did assign a type to unordered_set
- int
right? What does this error mean?
Upvotes: 4
Views: 25401
Reputation: 4387
First of all, unordered_set is in TR1 or C++11.
And second, you are declaring the set inside your function and then testing for some value in it. What's the point? The set's gonna be empty each time you call the function. EDIT: sorry, didn't notice it was static.
Upvotes: 2
Reputation: 57545
using namespace std
-- it's a source of a thousand frustrating errors. done_before
really doesn't name a type, it names a variable. To name a type you could use typedef unordered_set<int> done_before_type
, then done_before_type::iterator
will work.<unordered_set>
Upvotes: 14
Reputation: 60004
should be unordered_set<int>::iterator node_found = ...
I usually use a typedef to simplify naming of templated variables:
typedef unordered_set<int> t_done_before;
static t_done_before done_before;
t_done_before::iterator node_found = ...
Upvotes: 4