robertgbjones
BAN USERThe object size changes, so incrementing the pointer based on the size of base won't work for der. You either have to fiddle around deciding on the type of the thing pointed to, or more simply just make display(...) into a template.
#include <iostream>
#include <typeinfo>
using namespace std;
class base{
public:
int a;
base():a(0)
{}
int getA(){return a;}
};
class der:public base
{
public:
int b;
der():b(1)
{}
int getB(){return b;}
};
template <typename T> void display(T *obj, int ele)
{
for(int i = 0; i < ele; i++)
{
cout << (obj+i)->getA() <<endl;
}
}
int main()
{
int i = 3;
base arrb[i];
display(arrb, 3);
der arrd[i];
display(arrd, 3);
return 0;
}
std::vector<int> byFrequency( const std::vector<int> & values )
{
using namespace std;
map<int, int> tally;
for ( auto value : values )
{
auto insertion = tally.insert( make_pair( value, 1 ) );
if ( ! insertion.second )
{
++ insertion.first -> second;
}
}
vector<pair<int, int>> v( tally.begin( ), tally.end( ) );
sort( v.begin( ), v.end( ), []( const pair<int, int> & lhs, const pair<int, int> & rhs ) -> bool
{
return lhs.second > rhs.second;
}
);
vector<int> result( v.size( ) );
transform( v.begin( ), v.end( ), result.begin( ), []( const pair<int, int> & p ) -> int { return p.first; } );
return result;
}
Here's a basic solution. Order( n^3 ) runtime. To improve algorithmic complexity the points should be indexed, possibly with a map/set combination.
struct point
{
point( int x, int y ) : x( x ), y( y ) { }
int x { 0 };
int y { 0 };
};
bool operator ==( const point & lhs, const point & rhs ) { return lhs.x == rhs.x && lhs.y == rhs.y; }
int no_of_rectangles( const std::vector<point> & vertices )
{
using namespace std;
int result = 0;
for ( auto anchor = vertices.begin( ); anchor != vertices.end( ); ++ anchor )
{
for ( auto opposite = anchor + 1; opposite != vertices.end( ); ++ opposite )
{
if ( ( anchor -> x != opposite -> x ) && ( anchor -> y != opposite -> y ) )
{
if (
find( vertices.begin( ), vertices.end( ), point( anchor -> x, opposite -> y ) ) != vertices.end( ) &&
find( vertices.begin( ), vertices.end( ), point( opposite -> x, anchor -> y ) ) != vertices.end( ) )
++ result;
}
}
}
return result / 2;
}
//
// Pt 1
//
bool f( int array[ ], unsigned size );
//
// Pt 2
//
std::string asReadableList( const std::vector<int> & v )
{
using namespace std;
ostringstream os;
auto begin = v.begin( ), end = v.end( );
if ( ! v.empty( ) )
os << * begin ++;
for ( ; begin != end; ++ begin )
os << "," << * begin;
return os.str( );
}
//
// Pt 3
//
int minCommonPair( std::vector<int> & v, std::vector<int> & w )
{
using namespace std;
sort( v.begin( ), v.end( ) );
sort( w.begin( ), w.end( ) );
for ( auto iv = v.begin( ), iw = w.begin( ); iv != v.end( ) && iw != w.end( ); )
{
if ( * iv == * iw )
return * iv;
if ( * iv < * iw )
++ iv;
else
++ iw;
}
// There is no common element.
return 0; // Something!
}
//
// Pt 4
//
int minCommonAny( std::vector<std::vector<int>> & v )
{
using namespace std;
// Sorted already.
vector<vector<int>::iterator> begins( v.size( ) );
vector<vector<int>::iterator> ends( v.size( ) );
transform( v.begin( ), v.end( ), begins.begin( ), []( vector<int> & v ) { return v.begin( ); } );
transform( v.begin( ), v.end( ), ends.begin( ), []( vector<int> & v ) { return v.end( ); } );
auto finished = [ & begins, & ends ] ( ) -> bool
{
for ( auto beginit = begins.begin( ), endit = ends.begin( ); beginit != begins.end( ); ++ beginit, ++ endit )
{
if ( * beginit == * endit )
return true;
}
return false;
};
while ( ! finished( ) )
{
vector<int> minimums( v.size( ) );
transform( begins.begin( ), begins.end( ), minimums.begin( ), []( vector<int>::iterator i ) { return * i; } );
cout << "{ " << asReadableList( minimums ) << " }" << endl;
int minmin = * min_element( minimums.begin( ), minimums.end( ) );
int maxmin = * max_element( minimums.begin( ), minimums.end( ) );
if ( minmin == maxmin )
return minmin;
for_each( begins.begin( ), begins.end( ), [maxmin]( vector<int>::iterator & it ) { if ( * it < maxmin ) ++ it; } );
}
// There is no common element... return something.
return 0;
}
void deleteOdd( std::vector<int> & v )
{
auto pred = []( int i ) -> bool { return i % 2; };
auto first = find_if( v.begin( ), v.end( ), pred );
for ( auto last = first; last != v.end( ); ++ last )
if ( ! pred( * last ) )
* first ++ = * last;
v.erase( first, v.end( ) );
}
Is it cheating to use a standard library algorithm?
void deleteOdd( vector<int> & v )
{
v.erase( std::remove_if( v.begin( ), v.end( ), [](int i) {return i%2; } ), v.end( ) );}
- robertgbjones April 17, 2017