public
anonymous / autom.cpp
Created

How to write a copy constructor for a template class that stores pointers in a std::map?

  • Download Gist
autom.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
#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 );
}
automap.h
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.