Sunday 15 July 2012

Implement a LRU Cache in C++ - compilation error -


I need to implement an LRU cache in C ++. I have this code and I have a problem while compiling :

  #include & lt; Iostream & gt; # Include & lt; Vector & gt; # Include & lt; Hash_map> using namespace std; Using namespace stdext; Template & lt; Class K, Class T & gt; Struct LRUCacheEntry {K key; T data; LRUCacheEntry * prev; LRUCacheEntry * Next; }; Template & lt; Class K, Class T & gt; Class LRUCache {Private: hashmap & lt; K, LRUCacheEntry & lt; K, T & gt; * & Gt; _mapping; Of vector & lt; LRUCacheEntry & lt; K, T & gt; * & Gt; _freeEntries; LRUCacheEntry & LT; Kashmir, T & gt; * Head; LRUCacheEntry & LT; Kashmir, T & gt; * Tails; LRUCacheEntry & LT; Kashmir, T & gt; * Entries; Public: (int i = 0; i & lt; size; i ++) _freeEntries.push_back (entries + i) for LRookacht (size_size) (entries = new LRUCentchentry & lt; K, T & gt; ); Head = new LRUCacheEntry & lt; K, T & gt;; tail = new LRUCacheEntry & lt; K, T & gt; Head- gt; prev = NULL; Head-> Next = Tail; tail-> gt; Next = Faucet; Tail-> Tail-> LRUCache () Removal of Head; Removal of the tail; Delete [] Entries;} Void placed (K key, T data) {L. ruecache entranty & lt; k, t & gt; ; * Node = _mapping [key]; if (node) {// link list al Refresh the c (node); node-> data = data; (node);} and {if (_freeEntries.empty ()) {node = tail-> gt; different (node); _mapping.erase ( Node-> keys); node-> data = data; node-> keys = key; (node);} and {node = _freeEntries.back (); _freeEntries.pop_back (); node - & gt; key = key; node-> data = data; _mapping [key] = node; give it;}}} t mill (k key) {lrukkentry lt; k, t & gt; * Node = _mapping [key]; If (node) {separate (node); (Node); Return node-> Data; } And return null; } Private: Zero breakdown (LRUCacheEntry & lt; K, T & gt; * node) {node- & gt; Prev-> next = node-> next; Node-> Next-> gt = previous = node-> Previous; } Zero Enclosed (LRUCacheEntry & lt; K, T & gt; * node) {node- & gt; Next = head-> next; Node-> Prev = head; Head-> gt; Next = node; Node-> Next-> gt = previous = node; }};   

resualt compile:

  /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../ In the file included. /../../include/c++/4.4.7/backward/hash_map:60, from the source. C: 3: /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../include/c++/4.4.7/backward/backward_warning.hjg: Warning: # WARNING: This file contains at least one excluded or outdated header, which can be deleted at any future date without any notice. Instead use a non-exiting interface, instead with equivalent functionality. For a list of replacement headers and interfaces, the back_description. Consult the file. To disable the use of this warning - No-nondescript. Source.C: 6: Error: A stdextà ¢ is not a namespace-name source. C: 6: Error: ¢ A token source before the expected namespace-name. C: 21: Error: The declaration of ISO C ++ is prohibited with hash_map ¢ with no source source. Source: C: 21: Error: Expected ÃÆ'à ¢ â,¬Å¡Ãƒâ € šÃ, · Token Source.C: Member Function Zero LRUCache & lt; K, T & T)  ¢ source: C: 46: Error:  ¢ _mappingà ¢ This field was not declared in the source. C: Member Function ÃÆ'à ¢ â,¬Å¡Ãƒâ € šÃ, LRUCache & lt; Kashmir, T & gt; Source: c. 77: Error: This area was not declared in   

Can anyone tell me how can I fix "stdext" problem? I think this will solve the remaining errors. TNX!

stdext is an MSVC extension, but you are compiling it in GCC With Linux, this is not a GCC extension, but its header is ext / hash_map , and instead of stdext __ gnu_cxx Namespace is in place. Both MSWC and GCC have the same interface as hash_map, but if I miss the right, then there are subtle differences.

If you can, use the C +11 11 unarmed_map, if unsuccessful, you have TR1 available, and is st1 :: tr1 :: unordered_map . As the final fallback, consider Boost's unmadened containers (which works as the basis for the interface of TR1 and C ++ 11).

Edit I see that you are using GCC 4.4. X, TR1 is available there. The only issue would be if you are compiling the same code on windows with MSVC, also, I do not believe TR1 is available (I have VC8 and VC9 available, and are not available in both of them. I believe they directly support C ++ 11 libraries in VC10, start leaving TR1 completely).

No comments:

Post a Comment