Created
June 22, 2011 01:31
-
-
Save anonymous/1039341 to your computer and use it in GitHub Desktop.
How to write a copy constructor for a template class that stores pointers in a std::map?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "automap.h" | |
#include <iostream> | |
#include <algorithm> | |
struct CntInteger | |
{ | |
int value; | |
static int obj; | |
public: | |
CntInteger( int v ): value( v ) | |
{ | |
++obj; | |
} | |
CntInteger( const CntInteger& rhs ): value( rhs.value ) | |
{ | |
++obj; | |
std::cout << " copy " << std::endl; | |
} | |
~CntInteger() | |
{ | |
--obj; | |
} | |
int val() const | |
{ | |
return value; | |
} | |
static int cnt() | |
{ | |
return obj; | |
} | |
}; | |
bool operator<( const CntInteger& a, const CntInteger& b ) | |
{ | |
return a.val() < b.val(); | |
} | |
bool operator==( const CntInteger& a, const CntInteger& b ) | |
{ | |
return !( a < b ) && !( b < a ); | |
} | |
bool operator!=( const CntInteger& a, const CntInteger& b ) | |
{ | |
return !( a == b ); | |
} | |
int CntInteger::obj = 0; | |
class Greater | |
{ | |
static int called; | |
public: | |
bool operator()( const int& a, const int& b ) | |
{ | |
++called; | |
return a > b; | |
} | |
static int cnt() | |
{ | |
return called; | |
} | |
}; | |
int Greater::called = 0; | |
const int max = 1000; | |
int main() | |
{ | |
int yourMark = 1; | |
//* 2-es | |
auto_map<std::string, std::string> t; | |
t.insert( new std::string( "A" ), new std::string( "B" ) ); | |
t.insert( new std::string( "B" ), new std::string( "C" ) ); | |
for( int i = 0; i < max; ++i ) | |
{ | |
auto_map<CntInteger, CntInteger> a; | |
a.insert( new CntInteger( 1 ), new CntInteger( 1 ) ); | |
a.insert( new CntInteger( 3 ), new CntInteger( 6 ) ); | |
a.insert( new CntInteger( 3 ), new CntInteger( 2 ) ); | |
} | |
if ( 0 == CntInteger::cnt() ) | |
{ | |
yourMark = t.size(); | |
} | |
//*/ | |
//* 3-as | |
auto_map<int, int> m; | |
m.insert( new int( max ), new int( 2 ) ); | |
for( int i = 0; i < max; ++i ) | |
{ | |
auto_map<CntInteger, CntInteger> x; | |
x.insert( new CntInteger( i ), new CntInteger( i ) ); | |
auto_map<CntInteger, CntInteger> cx = x; | |
} | |
/*if ( 2 == m.at( max ) ) | |
{ | |
const auto_map<int, int> cm = m; | |
// ++m.at( max ); | |
if ( 2 == cm.at( max ) && 0 == CntInteger::cnt() ) | |
{ | |
yourMark = m.at( max ); | |
} | |
}*/ | |
//*/ | |
/* 4-es | |
auto_map<int, int> map; | |
for( int i = 0; i < max; ++i ) | |
{ | |
auto_map<CntInteger, CntInteger> c; | |
c.insert( new CntInteger( i ), new CntInteger( max ) ); | |
c.insert( new CntInteger( max + i ), new CntInteger( max ) ); | |
auto_map<CntInteger, CntInteger>::iterator it = c.begin(); | |
++it; | |
c.erase( c.begin(), it ); | |
map.insert( new int( i % 5 ), new int( i ) ); | |
} | |
if ( 0 == CntInteger::cnt() ) | |
{ | |
map.erase( map.begin(), ++map.begin() ); | |
yourMark = map.size(); | |
} | |
*/ | |
/* 5-os | |
auto_map<int, int, Greater> mg; | |
mg.insert( new int( 4 ), new int( 4 ) ); | |
mg.insert( new int( 2 ), new int( 5 ) ); | |
mg.insert( new int( 7 ), new int( 3 ) ); | |
mg.insert( new int( 4 ), new int( 7 ) ); | |
if ( Greater::cnt() > mg.size() ) | |
{ | |
yourMark = mg.at( 2 ); | |
} | |
*/ | |
std::cout << "Your mark is " << yourMark << " t.size: " << t.size() << " CntInteger count: " << CntInteger::cnt() ; | |
std::endl( std::cout ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef AUTOMAP_H | |
#define AUTOMAP_H | |
#include <map> | |
#include <iostream> | |
template <class K, class V> | |
class auto_map | |
{ | |
public: | |
struct cmp | |
{ | |
bool operator()(K *a, K *b) { return *a < *b; } | |
bool operator== (K *a) { return (*a); } | |
}; | |
auto_map(){} | |
~auto_map() | |
{ | |
for (typename std::map<K*, V*, cmp>::iterator i=m.begin(); i!=m.end(); ++i) | |
{ | |
delete (*i).first; | |
delete (*i).second; | |
m.erase(i); | |
} | |
} | |
auto_map (auto_map& original) | |
{ | |
for (typename std::map<K*, V*, cmp>::iterator i=original.m.begin(); i!=original.m.end(); ++i) | |
{ | |
K newk = (*(*i).first); | |
V newv = (*(*i).second); | |
m.insert(std::make_pair(newk, newv )); | |
//std::cout << newk << " from: " << (*i).first << std::endl; | |
} | |
}; | |
void insert(K* k, V* v) | |
{ | |
for (typename std::map<K*, V*, cmp>::iterator i=m.begin(); i!=m.end(); ++i) | |
{ | |
if ( (*k) == (*(*i).first) ) | |
{ | |
delete (*i).first; | |
delete (*i).second; | |
m.erase(i); | |
} | |
} | |
m.insert(std::make_pair(k,v)); | |
} | |
int size() { return m.size(); } | |
V at(K k) const | |
{ | |
for (typename std::map<K*, V*, cmp>::const_iterator i=m.begin(); i!=m.end(); ++i) | |
{ | |
} | |
} | |
void operator++ (int j) | |
{ | |
for (typename std::map<K*, V*, cmp>::iterator i=m.begin(); i!=m.end(); ++i) | |
{ | |
} | |
} | |
private: | |
std::map<K*, V*, cmp> m; | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment