Program 1
#include
#include
#include
#include
int main()
{
using namespace std;
#include
#include
int main()
{
using namespace std;
cout << boolalpha;
cout << "max(short): " << numeric_limits::max() << endl;
cout << "max(int): " << numeric_limits::max() << endl;
cout << "max(long): " << numeric_limits::max() << endl;
cout << endl;
cout << "max(float): "
<< numeric_limits::max() << endl;
cout << "max(double): "
<< numeric_limits::max() << endl;
cout << "max(long double): "
<< numeric_limits::max() << endl;
cout << endl;
cout << "is_signed(char): "
<< numeric_limits::is_signed << endl;
cout << endl;
cout << "is_specialized(string): "
<< numeric_limits::is_specialized << endl;
}
cout << "max(short): " << numeric_limits
cout << "max(int): " << numeric_limits
cout << "max(long): " << numeric_limits
cout << endl;
cout << "max(float): "
<< numeric_limits
cout << "max(double): "
<< numeric_limits
cout << "max(long double): "
<< numeric_limits
cout << endl;
cout << "is_signed(char): "
<< numeric_limits
cout << endl;
cout << "is_specialized(string): "
<< numeric_limits
}
Program 2
#include
#include
#ifndef counted_ptr_hpp
#define counted_ptr_hpp
template
class countedptr
#define counted_ptr_hpp
template
class countedptr
{
private:
t* ptr;
long* count;
public:
private:
t* ptr;
long* count;
public:
explicit countedptr (t* p=0): ptr(p), count(new long(1))
{
}
}
countedptr(const countedptr& p) throw() : ptr(p.ptr), count(p.count)
{
++*count;
}
~countedptr () throw()
++*count;
}
~countedptr () throw()
{
release();
}
countedptr& operator= (const countedptr& p) throw()
release();
}
countedptr
{
if (this != &p)
if (this != &p)
{
release();
ptr = p.ptr;
count = p.count;
++*count;
}
return *this;
}
t& operator*() const throw()
release();
ptr = p.ptr;
count = p.count;
++*count;
}
return *this;
}
t& operator*() const throw()
{
return *ptr;
}
return *ptr;
}
t* operator->() const throw()
{
return ptr;
}
private:
void release()
return ptr;
}
private:
void release()
{
++*count;
++*count;
if (*count == 0)
{
delete count;
delete ptr;
delete count;
delete ptr;
}
}
};
}
};
Program
3
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
void printcountedptr(countedptr elem)
{
cout << *elem << ' ';
}
int main()
{
using namespace std;
void printcountedptr(countedptr
{
cout << *elem << ' ';
}
int main()
{
typedef countedptr intptr;
deque coll1;
list coll2;
static int values[] = { 3, 5, 9, 1, 6, 4 };
list
static int values[] = { 3, 5, 9, 1, 6, 4 };
for (unsigned i=0; i
{
intptr ptr(new int(values[i]));
coll1.push_back(ptr);
coll2.push_front(ptr);
}
for_each(coll1.begin(), coll1.end(), printcountedptr);
cout << endl;
for_each(coll2.begin(), coll2.end(), printcountedptr);
cout << endl << endl;
coll1[2] *= *coll1[2]; (**coll1.begin()) *= -1; (**coll2.begin()) = 0;
for_each(coll1.begin(), coll1.end(), printcountedptr);
cout << endl;
for_each(coll2.begin(), coll2.end(), printcountedptr);
cout << endl;
}
intptr ptr(new int(values[i]));
coll1.push_back(ptr);
coll2.push_front(ptr);
}
for_each(coll1.begin(), coll1.end(), printcountedptr);
cout << endl;
for_each(coll2.begin(), coll2.end(), printcountedptr);
cout << endl << endl;
coll1[2] *= *coll1[2]; (**coll1.begin()) *= -1; (**coll2.begin()) = 0;
for_each(coll1.begin(), coll1.end(), printcountedptr);
cout << endl;
for_each(coll2.begin(), coll2.end(), printcountedptr);
cout << endl;
}
No comments:
Post a Comment