四道简单的C++小题.

2024-12-28 18:02:12
推荐回答(5个)
回答1:

OK终于写好了,不好意思拖了那么多天......

1、

#include
#include
#include
#include

class String
{
public:
typedef std::string::size_type size_type;

String();
String(const String& _Str);
String(const char* cstr);
String(const std::string str);
String(const char* begin, const char* end);
template
String(const T& t);

String operator + (const String& rhs);
String operator + (const char* rhs);
String& operator = (const String& rhs);
String& operator = (const char* rhs);
String& operator += (const String& rhs);
String& operator += (const char* rhs);

bool operator > (const String& rhs);
bool operator < (const String& rhs);
bool operator >= (const String& rhs);
bool operator <= (const String& rhs);
bool operator != (const String& rhs);

char operator [] (int index)const;
char& operator [] (int index);

const char* c_str()const;
size_type size()const;
String to_upper()const;
String to_lower()const;
String trim()const;
template
T to_type()const;

friend std::ostream& operator << (std::ostream& os, const String& _String);
friend std::istream& operator >> (std::istream& is, String& _String);
private:
std::string Str;
template
T static lexical_cast(const U& u);
};

template
T String::lexical_cast(const U& u)
{
std::stringstream sstrm;
sstrm << u;
T t;
sstrm >> t;
return t;
}

String::String() : Str(std::string())
{
}

String::String(const String& _String) : Str(_String.Str)
{
}

String::String(const char* cstr) : Str(std::string(cstr))
{
}

String::String(const std::string str) : Str(str)
{
}

String::String(const char* begin, const char* end) : Str(begin, end)
{
}

template
String::String(const T& t) : Str(String::lexical_cast(t))
{
}

String String::operator + (const String& rhs)
{
String temp(this->Str + rhs.Str);
return temp;
}

String String::operator + (const char* rhs)
{
String temp(this->Str + std::string(rhs));
return temp;
}

String& String::operator = (const String& rhs)
{
if(this == &rhs)
return *this;
this->Str = rhs.Str;
return *this;
}

String& String::operator = (const char* rhs)
{
if(this->Str == std::string(rhs))
return *this;
this->Str = std::string(rhs);
return *this;
}

String& String::operator += (const String& rhs)
{
if(this == &rhs)
return *this;
this->Str += rhs.Str;
return *this;
}

String& String::operator += (const char* rhs)
{
this->Str += std::string(rhs);
return *this;
}

bool String::operator > (const String& rhs)
{
return this->Str > rhs.Str;
}

bool String::operator >= (const String& rhs)
{
return this->Str >= rhs.Str;
}

bool String::operator < (const String& rhs)
{
return this->Str < rhs.Str;
}

bool String::operator <= (const String& rhs)
{
return this->Str <= rhs.Str;
}

bool String::operator != (const String& rhs)
{
return this->Str != rhs.Str;
}

const char* String::c_str()const
{
return this->Str.c_str();
}

char String::operator [] (int index)const
{
return this->Str[index];
}

char& String::operator [] (int index)
{
return this->Str[index];
}

String::size_type String::size()const
{
return this->Str.size();
}

String String::to_upper()const
{
String temp;
for(String::size_type i = 0; i < this->size(); ++i)
{
temp += static_cast(std::toupper((*this)[i]));
}
return temp;
}

String String::to_lower()const
{
String temp;
for(String::size_type i = 0; i < this->size(); ++i)
{
temp += static_cast(std::tolower((*this)[i]));
}
return temp;
}

String String::trim()const
{
String temp(*this);
temp.Str.erase(0, Str.find_first_not_of(" "));
temp.Str.erase(Str.find_last_not_of(" ") + 1, Str.size() - 1);
return temp;
}

template
T String::to_type()const
{
return String::lexical_cast(this->Str);
}

std::ostream& operator << (std::ostream& os, const String& _String)
{
os << _String.Str;
return os;
}

std::istream& operator >> (std::istream& is, String& _String)
{
is >> _String.Str;
return is;
}

int main()
{
using namespace std;

String Str1("hello");
String Str2("WORLD!");
String Str3(" ");
Str3 += (Str1 + ", " + Str2);
Str3 += " ";

cout << "Str1: " << Str1 << '\n';
cout << "Str2: " << Str2 << '\n';
cout << "Str3: " << Str3 << "\n\n";

cout.setf(ios_base::boolalpha);
cout << "Str1 > Str2: " << (Str1 > Str2) << endl;
cout << "Str1 < Str2: " << (Str1 < Str2) << '\n';
cout << "Str1 >= Str2: " << (Str1 >= Str2) << '\n';
cout << "Str1 <= Str2: " << (Str1 <= Str2) << '\n';
cout << "Str1 != Str2: " << (Str1 != Str2) << "\n\n";
cout.unsetf(ios_base::boolalpha);

cout << "Str1 to upper: " << Str1.to_upper() << endl;
cout << "Str2 to lower: " << Str2.to_lower() << '\n';
cout << "Size of Str3: " << Str3.size() << '\n';
cout << "Trim Str3: " << Str3.trim() << "\n\n";

String pi(3.14159);

char pi_char(pi.to_type());
int pi_int(pi.to_type());
float pi_float(pi.to_type());
double pi_double(pi.to_type());

cout << "String: " << pi << '\n';
cout << "char: " << pi_char << '\n';
cout << "int: " << pi_int << '\n';
cout << "float: " << pi_float << '\n';
cout << "double: " << pi_double << endl;
}

测试结果:

Str1: hello
Str2: WORLD!
Str3: hello, WORLD!

Str1 > Str2: true
Str1 < Str2: false
Str1 >= Str2: true
Str1 <= Str2: false
Str1 != Str2: true

Str1 to upper: HELLO
Str2 to lower: world!
Size of Str3: 19
Trim Str3: hello, WORLD!

String: 3.14159
char: 3
int: 3
float: 3.14159
double: 3.14159

2、

#include
#include
#include
#include

template >
class Vector
{
public:
typedef std::size_t size_type;
typedef T* pointer;
typedef T& reference;
typedef T value_type;

typedef Vector Vector_type;

Vector();
explicit Vector(size_type num, const T& val = T());
explicit Vector(const T* _begin, const T* _end);
Vector(const Vector_type& v);

void reserve(size_type n);
void resize(size_type n, const T& val = T());
void append(const T& val);
size_type size()const;
size_type capacity()const;
~Vector();

Vector_type operator + (const Vector_type& v)const;
Vector_type operator - (const Vector_type& v)const;
Vector_type operator * (const Vector_type& v)const;
Vector_type operator / (const Vector_type& v)const;
Vector_type& operator = (const Vector_type& v);
Vector_type& operator += (const Vector_type& v);
Vector_type& operator -= (const Vector_type& v);
Vector_type& operator *= (const Vector_type& v);
Vector_type& operator /= (const Vector_type& v);

reference operator [] (size_type index);
value_type operator [] (size_type index)const;
pointer begin()const;
pointer end()const;

template
friend std::ostream& operator << (std::ostream& os, Vector_type& v);

template
friend std::istream& operator >> (std::istream& is, Vector_type& v);

private:
Allocator alloc;
T* elems;
size_type numElems;
size_type sizeElems;
};

template
Vector::Vector() : numElems(0), sizeElems(0)
{
}
template
Vector::Vector(size_type num, const T& val = T())
{
sizeElems = numElems = num;
elems = alloc.allocate(sizeElems);

std::uninitialized_fill(elems, elems + numElems, val);
}

template
Vector::Vector(const T* _begin, const T* _end)
{
sizeElems = numElems = _end - _begin;

elems = alloc.allocate(sizeElems);

for(size_type i = 0; i < numElems; ++i)
{
(*this)[i] = _begin[i];
}
}

template
Vector::Vector(const Vector_type& v)
{
numElems = v.numElems;
sizeElems = v.sizeElems;

elems = alloc.allocate(sizeElems);

for(size_type i = 0; i < numElems; ++i)
{
(*this)[i] = v[i];
}
}

template
Vector::~Vector()
{
for (size_type i = 0; i < numElems; ++i)
{
alloc.destroy(&elems [i]);
}

alloc.deallocate(elems, sizeElems);
}

template
void Vector::reserve(size_type size)
{
if(size <= sizeElems)
{
return;
}

T* newmem = alloc.allocate(size);

for(size_type i = 0; i < numElems; ++i)
{
newmem[i] = (*this)[i];
}

for(size_type i = 0; i < numElems; ++i)
{
alloc.destroy(&elems [i]);
}

alloc.deallocate(elems, sizeElems);

sizeElems = size;
elems = newmem;
}

template
void Vector::resize(size_type n, const T& val = T())
{
if(n < numElems)
{
for(size_type i = 0; i < numElems - n; ++i)
{
alloc.destroy(&elems[numElems - i - 1]);
}
alloc.deallocate(&elems[n - 1], numElems - n);
numElems = n;
}
else if(n > numElems)
{
if(n <= sizeElems)
{
std::uninitialized_fill(elems + numElems, elems + numElems + n - numElems, val);
numElems += n - numElems;
}
else
{
this->reserve(n);
std::uninitialized_fill(elems + numElems, elems + numElems + n - numElems, val);
numElems += n - numElems;
}
}
}

template
void Vector::append(const T& val)
{
if(numElems == 0)
{
this->reserve(1);
(*this)[0] = val;
++numElems;
}
else
{
this->reserve(numElems + 1);
(*this)[numElems] = val;
++numElems;
}
}

template
typename Vector::size_type Vector::size()const
{
return numElems;
}

template
typename Vector::size_type Vector::capacity()const
{
return sizeElems;
}

template
T& Vector::operator [] (size_type index)
{
return elems[index];
}

template
T Vector::operator [] (size_type index)const
{
return elems[index];
}

template
T* Vector::begin()const
{
return &(*this)[0];
}

template
T* Vector::end()const
{
return &(*this)[numElems];
}

template
Vector Vector::operator + (const Vector& v)const
{
if(this->numElems != v.numElems)
throw std::domain_error("Range not equal!");
Vector vtemp(v.numElems);
for(size_type i = 0; i < v.numElems; ++i)
{
vtemp[i] = (*this)[i] + v[i];
}
return vtemp;
}

template
Vector Vector::operator - (const Vector& v)const
{
if(this->numElems != v.numElems)
throw std::domain_error("Range not equal!");
Vector vtemp(v.numElems);
for(size_type i = 0; i < v.numElems; ++i)
{
vtemp[i] = (*this)[i] - v[i];
}
return vtemp;
}

template
Vector Vector::operator * (const Vector& v)const
{
if(this->numElems != v.numElems)
throw std::domain_error("Range not equal!");
Vector vtemp(v.numElems);
for(size_type i = 0; i < v.numElems; ++i)
{
vtemp[i] = (*this)[i] * v[i];
}
return vtemp;
}

template
Vector Vector::operator / (const Vector& v)const
{
if(this->numElems != v.numElems)
throw std::domain_error("Range not equal!");
Vector vtemp(v.numElems);
for(size_type i = 0; i < v.numElems; ++i)
{
vtemp[i] = (*this)[i] / v[i];
}
return vtemp;
}

template
Vector& Vector::operator = (const Vector& v)
{
if(this->numElems != v.numElems)
throw std::domain_error("Range not equal!");
for(size_type i = 0; i < v.numElems; ++i)
{
(*this)[i] = v[i];
}
return *this;
}

template
Vector& Vector::operator += (const Vector& v)
{
if(this->numElems != v.numElems)
throw std::domain_error("Range not equal!");
for(size_type i = 0; i < v.numElems; ++i)
{
(*this)[i] += v[i];
}
return *this;
}

template
Vector& Vector::operator -= (const Vector& v)
{
if(this->numElems != v.numElems)
throw std::domain_error("Range not equal!");
for(size_type i = 0; i < v.numElems; ++i)
{
(*this)[i] -= v[i];
}
return *this;
}

template
Vector& Vector::operator *= (const Vector& v)
{
if(this->numElems != v.numElems)
throw std::domain_error("Range not equal!");
for(size_type i = 0; i < v.numElems; ++i)
{
(*this)[i] *= v[i];
}
return *this;
}

template
Vector& Vector::operator /= (const Vector& v)
{
if(this->numElems != v.numElems)
throw std::domain_error("Range not equal!");
for(size_type i = 0; i < v.numElems; ++i)
{
(*this)[i] /= v[i];
}
return *this;
}

template
std::ostream& operator << (std::ostream& os, Vector& v)
{
for(Vector::size_type i = 0; i < v.size(); ++i)
{
os << v[i] << ' ';
}
os << std::endl;
return os;
}

template
std::istream& operator >> (std::istream& is, Vector& v)
{
for(Vector::size_type i = 0; i < v.size(); ++i)
{
is >> v[i];
}

return is;
}

template
void print(T t, const char* msg = "")
{
std::cout << msg;
std::cout << t;
}
int main()
{
using namespace std;
Vector V1, V2;

for(int i = 1; i <= 10; ++i)
{
V1.append(i* 1.1);
V2.append((11-i)* 1.1);
}

print(V1, "V1: ");
print(V2, "V2: ");
cout << endl;

Vector V3(V1 + V2);
print(V3, "V1 + V2: ");

V3 = V1 - V2;
print(V3, "V1 - V2: ");

V3 = V1 * V2;
print(V3, "V1 * V2: ");

V3 = V1 / V2;
print(V3, "V1 / V2: ");
cout << endl;

double carray[] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10};
Vector V4(carray, carray + sizeof carray/sizeof carray[0]), V5(V4.size(), 1);

print(V4, "V4: ");
print(V5, "V5: ");
cout << endl;

V5 += V4;
print(V5, "V5 += V4: ");

V5 -= V4;
print(V5, "V5 -= V4: ");

V5 *= V4;
print(V5, "V5 *= V4: ");

V5 /= V4;
print(V5, "V5 /= V4: ");
}

测试结果:

V1: 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 11
V2: 11 9.9 8.8 7.7 6.6 5.5 4.4 3.3 2.2 1.1

V1 + V2: 12.1 12.1 12.1 12.1 12.1 12.1 12.1 12.1 12.1 12.1
V1 - V2: -9.9 -7.7 -5.5 -3.3 -1.1 1.1 3.3 5.5 7.7 9.9
V1 * V2: 12.1 21.78 29.04 33.88 36.3 36.3 33.88 29.04 21.78 12.1
V1 / V2: 0.1 0.222222 0.375 0.571429 0.833333 1.2 1.75 2.66667 4.5 10

V4: 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 10.1
V5: 1 1 1 1 1 1 1 1 1 1

V5 += V4: 2.1 3.2 4.3 5.4 6.5 7.6 8.7 9.8 10.9 11.1
V5 -= V4: 1 1 1 1 1 1 1 1 1 1
V5 *= V4: 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 10.1
V5 /= V4: 1 1 1 1 1 1 1 1 1 1

3、

#include
#include
#include
#include
#include
#include
#include

template >
class List
{
struct dbNode;
public:
typedef std::size_t size_type;
typedef T value_type;
typedef T* pointer;
typedef dbNode* Node;

class iterator
{
public:
iterator();
iterator(const iterator& it);
iterator(Node _node);

iterator& operator = (const iterator& rhs);
iterator& operator ++ ();
iterator operator ++ (int);
iterator& operator -- ();
iterator operator -- (int);
bool operator == (const iterator& rhs)const;
bool operator != (const iterator& rhs)const;

value_type operator * ()const;
value_type& operator * ();

Node get();
private:
Node node;
};

template
void init(const U beg, const U end);

List();

template
List(const U beg, const U end);

template
List(const U& from);

List(const List& _List);

List& operator = (const List& _List);

~List();

iterator begin();
iterator begin()const;
iterator end();
iterator end()const;

iterator insert(iterator pos, T value);
iterator erase(iterator pos);
iterator erase(iterator from, iterator to);

size_type size()const;

template
List OpNumber(const List& lhs, U rhs, X op);

template
List OpList(const List& lhs, const U& rhs, Y op);

List& negate();

template
List plus(U rhs);
template
List operator + (const U& rhs);

template
List minus(U rhs);
template
List operator - (const U& rhs);

template
List multiplies(U rhs);
template
List operator * (const U& rhs);

template
List divides(U rhs);
template
List operator / (const U& rhs);

template
List modulus(U rhs);
template
List operator % (const U& rhs);

template
List& assign(U rhs);
template
List& operator = (const U& rhs);

template
List& PlusAssign(U rhs);
template
List& operator += (const U& rhs);

template
List& MinusAssign(U rhs);
template
List& operator -= (const U& rhs);

template
List& MultipliesAssign(U rhs);
template
List& operator *= (const U& rhs);

template
List& DividesAssign(U rhs);
template
List& operator /= (const U& rhs);

template
List& ModulusAssign(U rhs);
template
List& operator %= (const U& rhs);

private:
typedef typename Allocator::rebind::other NodeAlloc;

NodeAlloc alloc;
size_type numElems;

struct dbNode
{
value_type value;
Node prev;
Node next;
}*first, *last;
};

template
List::iterator::iterator() : node(0)
{
}

template
List::iterator::iterator(const iterator& it) : node(it.node)
{
}

template
List::iterator::iterator(typename List::Node _node) : node(_node)
{
}

template
typename List::iterator&
List::iterator::operator = (const iterator& rhs)
{
if(&rhs == this)
return *this;
node = rhs.node;
return *this;
}

template
typename List::iterator&
List::iterator::operator ++ ()
{
node = node->next;
return *this;
}

template
typename List::iterator
List::iterator::operator ++ (int)
{
iterator temp(node);
node = node->next;
return temp;
}

template
typename List::iterator&
List::iterator::operator -- ()
{
node = node->prev;
return *this;
}

template
typename List::iterator
List::iterator::operator -- (int)
{
iterator temp(node);
node = node->prev;
return temp;
}

template
bool List::iterator::operator == (const iterator& rhs)const
{
return node == rhs.node;
}

template
bool List::iterator::operator != (const iterator& rhs)const
{

回答2:

yun ,一楼还真能写,我都不想做了

回答3:

怎么我就还看不懂呢?

回答4:

我还刚刚学,这个就答不上咯

回答5:

路过,爱莫能助!