四道简单的C++小题.
1.设计一个字符串类,要求具有计算字符串长度、两个字符串大小比较、两个字符串连接等功能。细节要求:(1)构造:可利用字符串对象,指针,整数,字符,浮点数构造一个字符串以及...
1. 设计一个字符串类,要求具有计算字符串长度、两个字符串大小比较、
两个字符串连接等功能。
细节要求:
(1)构造:可利用字符串对象,指针,整数,字符,浮点数构造一个字符串以及
拷贝构造对象;
(2)运算:提供字符串加法,关系运算(即比较字符串大小运算),赋值运算;
(3)转换:转换为C 语言字符串,字符串长度,大小写转换,删除字符串前后空
格,字符串与整数和浮点数转换等。
(4)输入输出:支持输入输出插入运算>>和<<。
2.设计一个能存放整数的一维向量(数组)类Vector,具有以下功能:
(1)数组的建立和输入输出。
(2)对向量的运算符+,-,+=,-=进行重载。
(3)一般操作。
细节要求:
构造:可利用向量对象和C的一维数组构造向量对象以及拷贝构造对象;
向量支持一般的数据类型
(3)运算:提供重新设置向量长度,向量加、减、乘(内积)运算以及拷贝构造对象
(4)输入输出:支持输入输出插入运算>>和<<。
3.设计一个能存放整数的列表(链表)类List,具有以下功能:
(1)利用双向链表形成List类并可进行输入输出。
(2)对列表的运算符+,-,+=,-=进行重载。
(3)一般操作。
细节要求:
构造:可利用链表对象和C 的一维数组构造链表对象以及拷贝构造对象;
链表支持一般的数据类型
(3)运算:提供诸如添加、删除节点等操作
(4)输入输出:支持输入输出插入运算>>和<<。
4.定义一个学生类,使其具有学号、姓名(长度不定)、高数、外语和C++
程序设计三门课的成绩属性,以及各属性访问方法、求总成绩、求平均成绩、设置
和显示学生信息方法。此外,在main函数中以定义学生数组方法模拟一个班的学生
信息,利用独立定义函数方式给出常见情况的统计,如最高成绩、最低成绩、平均
成绩、学生信息列表等。
不错,加油哦。
如果地方不够就给我发消息吧,我一收到就会给分的,谢谢了。 展开
两个字符串连接等功能。
细节要求:
(1)构造:可利用字符串对象,指针,整数,字符,浮点数构造一个字符串以及
拷贝构造对象;
(2)运算:提供字符串加法,关系运算(即比较字符串大小运算),赋值运算;
(3)转换:转换为C 语言字符串,字符串长度,大小写转换,删除字符串前后空
格,字符串与整数和浮点数转换等。
(4)输入输出:支持输入输出插入运算>>和<<。
2.设计一个能存放整数的一维向量(数组)类Vector,具有以下功能:
(1)数组的建立和输入输出。
(2)对向量的运算符+,-,+=,-=进行重载。
(3)一般操作。
细节要求:
构造:可利用向量对象和C的一维数组构造向量对象以及拷贝构造对象;
向量支持一般的数据类型
(3)运算:提供重新设置向量长度,向量加、减、乘(内积)运算以及拷贝构造对象
(4)输入输出:支持输入输出插入运算>>和<<。
3.设计一个能存放整数的列表(链表)类List,具有以下功能:
(1)利用双向链表形成List类并可进行输入输出。
(2)对列表的运算符+,-,+=,-=进行重载。
(3)一般操作。
细节要求:
构造:可利用链表对象和C 的一维数组构造链表对象以及拷贝构造对象;
链表支持一般的数据类型
(3)运算:提供诸如添加、删除节点等操作
(4)输入输出:支持输入输出插入运算>>和<<。
4.定义一个学生类,使其具有学号、姓名(长度不定)、高数、外语和C++
程序设计三门课的成绩属性,以及各属性访问方法、求总成绩、求平均成绩、设置
和显示学生信息方法。此外,在main函数中以定义学生数组方法模拟一个班的学生
信息,利用独立定义函数方式给出常见情况的统计,如最高成绩、最低成绩、平均
成绩、学生信息列表等。
不错,加油哦。
如果地方不够就给我发消息吧,我一收到就会给分的,谢谢了。 展开
展开全部
OK终于写好了,不好意思拖了那么多天......
1、
#include <iostream>
#include <string>
#include <sstream>
#include <cctype>
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<class T>
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<class T>
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<class T, class U>
T static lexical_cast(const U& u);
};
template<class T, class U>
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<class T>
String::String(const T& t) : Str(String::lexical_cast<std::string>(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<char>(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<char>(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<class T>
T String::to_type()const
{
return String::lexical_cast<T>(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<char>());
int pi_int(pi.to_type<int>());
float pi_float(pi.to_type<float>());
double pi_double(pi.to_type<double>());
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 <iostream>
#include <memory>
#include <exception>
#include <cstddef>
template<class T, class Allocator = std::allocator<T> >
class Vector
{
public:
typedef std::size_t size_type;
typedef T* pointer;
typedef T& reference;
typedef T value_type;
typedef Vector<T, Allocator> 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<class T, class Allocator>
friend std::ostream& operator << (std::ostream& os, Vector_type& v);
template<class T, class Allocator>
friend std::istream& operator >> (std::istream& is, Vector_type& v);
private:
Allocator alloc;
T* elems;
size_type numElems;
size_type sizeElems;
};
template<class T, class Allocator>
Vector<T, Allocator>::Vector() : numElems(0), sizeElems(0)
{
}
template<class T, class Allocator>
Vector<T, Allocator>::Vector(size_type num, const T& val = T())
{
sizeElems = numElems = num;
elems = alloc.allocate(sizeElems);
std::uninitialized_fill(elems, elems + numElems, val);
}
template<class T, class Allocator>
Vector<T, Allocator>::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<class T, class Allocator>
Vector<T, Allocator>::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<class T, class Allocator>
Vector<T, Allocator>::~Vector()
{
for (size_type i = 0; i < numElems; ++i)
{
alloc.destroy(&elems [i]);
}
alloc.deallocate(elems, sizeElems);
}
template <class T, class Allocator>
void Vector<T, Allocator>::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<class T, class Allocator>
void Vector<T, Allocator>::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<class T, class Allocator>
void Vector<T, Allocator>::append(const T& val)
{
if(numElems == 0)
{
this->reserve(1);
(*this)[0] = val;
++numElems;
}
else
{
this->reserve(numElems + 1);
(*this)[numElems] = val;
++numElems;
}
}
template<class T, class Allocator>
typename Vector<T, Allocator>::size_type Vector<T, Allocator>::size()const
{
return numElems;
}
template<class T, class Allocator>
typename Vector<T, Allocator>::size_type Vector<T, Allocator>::capacity()const
{
return sizeElems;
}
template<class T, class Allocator>
T& Vector<T, Allocator>::operator [] (size_type index)
{
return elems[index];
}
template<class T, class Allocator>
T Vector<T, Allocator>::operator [] (size_type index)const
{
return elems[index];
}
template<class T, class Allocator>
T* Vector<T, Allocator>::begin()const
{
return &(*this)[0];
}
template<class T, class Allocator>
T* Vector<T, Allocator>::end()const
{
return &(*this)[numElems];
}
template<class T, class Allocator>
Vector<T, Allocator> Vector<T, Allocator>::operator + (const Vector<T, Allocator>& v)const
{
if(this->numElems != v.numElems)
throw std::domain_error("Range not equal!");
Vector<T, Allocator> vtemp(v.numElems);
for(size_type i = 0; i < v.numElems; ++i)
{
vtemp[i] = (*this)[i] + v[i];
}
return vtemp;
}
template<class T, class Allocator>
Vector<T, Allocator> Vector<T, Allocator>::operator - (const Vector<T, Allocator>& v)const
{
if(this->numElems != v.numElems)
throw std::domain_error("Range not equal!");
Vector<T, Allocator> vtemp(v.numElems);
for(size_type i = 0; i < v.numElems; ++i)
{
vtemp[i] = (*this)[i] - v[i];
}
return vtemp;
}
template<class T, class Allocator>
Vector<T, Allocator> Vector<T, Allocator>::operator * (const Vector<T, Allocator>& v)const
{
if(this->numElems != v.numElems)
throw std::domain_error("Range not equal!");
Vector<T, Allocator> vtemp(v.numElems);
for(size_type i = 0; i < v.numElems; ++i)
{
vtemp[i] = (*this)[i] * v[i];
}
return vtemp;
}
template<class T, class Allocator>
Vector<T, Allocator> Vector<T, Allocator>::operator / (const Vector<T, Allocator>& v)const
{
if(this->numElems != v.numElems)
throw std::domain_error("Range not equal!");
Vector<T, Allocator> vtemp(v.numElems);
for(size_type i = 0; i < v.numElems; ++i)
{
vtemp[i] = (*this)[i] / v[i];
}
return vtemp;
}
template<class T, class Allocator>
Vector<T, Allocator>& Vector<T, Allocator>::operator = (const Vector<T, Allocator>& 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<class T, class Allocator>
Vector<T, Allocator>& Vector<T, Allocator>::operator += (const Vector<T, Allocator>& 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<class T, class Allocator>
Vector<T, Allocator>& Vector<T, Allocator>::operator -= (const Vector<T, Allocator>& 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<class T, class Allocator>
Vector<T, Allocator>& Vector<T, Allocator>::operator *= (const Vector<T, Allocator>& 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<class T, class Allocator>
Vector<T, Allocator>& Vector<T, Allocator>::operator /= (const Vector<T, Allocator>& 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<class T, class Allocator>
std::ostream& operator << (std::ostream& os, Vector<T, Allocator>& v)
{
for(Vector<T, Allocator>::size_type i = 0; i < v.size(); ++i)
{
os << v[i] << ' ';
}
os << std::endl;
return os;
}
template<class T, class Allocator>
std::istream& operator >> (std::istream& is, Vector<T, Allocator>& v)
{
for(Vector<T, Allocator>::size_type i = 0; i < v.size(); ++i)
{
is >> v[i];
}
return is;
}
template<class T>
void print(T t, const char* msg = "")
{
std::cout << msg;
std::cout << t;
}
int main()
{
using namespace std;
Vector<double> 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<double> 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<double> 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 <iostream>
#include <memory>
#include <vector>
#include <cstddef>
#include <functional>
#include <algorithm>
#include <exception>
template <class T, class Allocator = std::allocator<T> >
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 <class U>
void init(const U beg, const U end);
List();
template <class U>
List(const U beg, const U end);
template <class U>
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 <class U, class X>
List OpNumber(const List& lhs, U rhs, X op);
template <class U, class Y>
List OpList(const List& lhs, const U& rhs, Y op);
List& negate();
template <class U>
List plus(U rhs);
template <class U>
List operator + (const U& rhs);
template <class U>
List minus(U rhs);
template <class U>
List operator - (const U& rhs);
template <class U>
List multiplies(U rhs);
template <class U>
List operator * (const U& rhs);
template <class U>
List divides(U rhs);
template <class U>
List operator / (const U& rhs);
template <class U>
List modulus(U rhs);
template <class U>
List operator % (const U& rhs);
template <class U>
List& assign(U rhs);
template <class U>
List& operator = (const U& rhs);
template <class U>
List& PlusAssign(U rhs);
template <class U>
List& operator += (const U& rhs);
template <class U>
List& MinusAssign(U rhs);
template <class U>
List& operator -= (const U& rhs);
template <class U>
List& MultipliesAssign(U rhs);
template <class U>
List& operator *= (const U& rhs);
template <class U>
List& DividesAssign(U rhs);
template <class U>
List& operator /= (const U& rhs);
template <class U>
List& ModulusAssign(U rhs);
template <class U>
List& operator %= (const U& rhs);
private:
typedef typename Allocator::rebind<dbNode>::other NodeAlloc;
NodeAlloc alloc;
size_type numElems;
struct dbNode
{
value_type value;
Node prev;
Node next;
}*first, *last;
};
template <class T, class Allocator>
List<T, Allocator>::iterator::iterator() : node(0)
{
}
template <class T, class Allocator>
List<T, Allocator>::iterator::iterator(const iterator& it) : node(it.node)
{
}
template <class T, class Allocator>
List<T, Allocator>::iterator::iterator(typename List::Node _node) : node(_node)
{
}
template <class T, class Allocator>
typename List<T, Allocator>::iterator&
List<T, Allocator>::iterator::operator = (const iterator& rhs)
{
if(&rhs == this)
return *this;
node = rhs.node;
return *this;
}
template <class T, class Allocator>
typename List<T, Allocator>::iterator&
List<T, Allocator>::iterator::operator ++ ()
{
node = node->next;
return *this;
}
template <class T, class Allocator>
typename List<T, Allocator>::iterator
List<T, Allocator>::iterator::operator ++ (int)
{
iterator temp(node);
node = node->next;
return temp;
}
template <class T, class Allocator>
typename List<T, Allocator>::iterator&
List<T, Allocator>::iterator::operator -- ()
{
node = node->prev;
return *this;
}
template <class T, class Allocator>
typename List<T, Allocator>::iterator
List<T, Allocator>::iterator::operator -- (int)
{
iterator temp(node);
node = node->prev;
return temp;
}
template <class T, class Allocator>
bool List<T, Allocator>::iterator::operator == (const iterator& rhs)const
{
return node == rhs.node;
}
template <class T, class Allocator>
bool List<T, Allocator>::iterator::operator != (const iterator& rhs)const
{
1、
#include <iostream>
#include <string>
#include <sstream>
#include <cctype>
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<class T>
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<class T>
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<class T, class U>
T static lexical_cast(const U& u);
};
template<class T, class U>
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<class T>
String::String(const T& t) : Str(String::lexical_cast<std::string>(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<char>(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<char>(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<class T>
T String::to_type()const
{
return String::lexical_cast<T>(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<char>());
int pi_int(pi.to_type<int>());
float pi_float(pi.to_type<float>());
double pi_double(pi.to_type<double>());
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 <iostream>
#include <memory>
#include <exception>
#include <cstddef>
template<class T, class Allocator = std::allocator<T> >
class Vector
{
public:
typedef std::size_t size_type;
typedef T* pointer;
typedef T& reference;
typedef T value_type;
typedef Vector<T, Allocator> 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<class T, class Allocator>
friend std::ostream& operator << (std::ostream& os, Vector_type& v);
template<class T, class Allocator>
friend std::istream& operator >> (std::istream& is, Vector_type& v);
private:
Allocator alloc;
T* elems;
size_type numElems;
size_type sizeElems;
};
template<class T, class Allocator>
Vector<T, Allocator>::Vector() : numElems(0), sizeElems(0)
{
}
template<class T, class Allocator>
Vector<T, Allocator>::Vector(size_type num, const T& val = T())
{
sizeElems = numElems = num;
elems = alloc.allocate(sizeElems);
std::uninitialized_fill(elems, elems + numElems, val);
}
template<class T, class Allocator>
Vector<T, Allocator>::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<class T, class Allocator>
Vector<T, Allocator>::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<class T, class Allocator>
Vector<T, Allocator>::~Vector()
{
for (size_type i = 0; i < numElems; ++i)
{
alloc.destroy(&elems [i]);
}
alloc.deallocate(elems, sizeElems);
}
template <class T, class Allocator>
void Vector<T, Allocator>::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<class T, class Allocator>
void Vector<T, Allocator>::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<class T, class Allocator>
void Vector<T, Allocator>::append(const T& val)
{
if(numElems == 0)
{
this->reserve(1);
(*this)[0] = val;
++numElems;
}
else
{
this->reserve(numElems + 1);
(*this)[numElems] = val;
++numElems;
}
}
template<class T, class Allocator>
typename Vector<T, Allocator>::size_type Vector<T, Allocator>::size()const
{
return numElems;
}
template<class T, class Allocator>
typename Vector<T, Allocator>::size_type Vector<T, Allocator>::capacity()const
{
return sizeElems;
}
template<class T, class Allocator>
T& Vector<T, Allocator>::operator [] (size_type index)
{
return elems[index];
}
template<class T, class Allocator>
T Vector<T, Allocator>::operator [] (size_type index)const
{
return elems[index];
}
template<class T, class Allocator>
T* Vector<T, Allocator>::begin()const
{
return &(*this)[0];
}
template<class T, class Allocator>
T* Vector<T, Allocator>::end()const
{
return &(*this)[numElems];
}
template<class T, class Allocator>
Vector<T, Allocator> Vector<T, Allocator>::operator + (const Vector<T, Allocator>& v)const
{
if(this->numElems != v.numElems)
throw std::domain_error("Range not equal!");
Vector<T, Allocator> vtemp(v.numElems);
for(size_type i = 0; i < v.numElems; ++i)
{
vtemp[i] = (*this)[i] + v[i];
}
return vtemp;
}
template<class T, class Allocator>
Vector<T, Allocator> Vector<T, Allocator>::operator - (const Vector<T, Allocator>& v)const
{
if(this->numElems != v.numElems)
throw std::domain_error("Range not equal!");
Vector<T, Allocator> vtemp(v.numElems);
for(size_type i = 0; i < v.numElems; ++i)
{
vtemp[i] = (*this)[i] - v[i];
}
return vtemp;
}
template<class T, class Allocator>
Vector<T, Allocator> Vector<T, Allocator>::operator * (const Vector<T, Allocator>& v)const
{
if(this->numElems != v.numElems)
throw std::domain_error("Range not equal!");
Vector<T, Allocator> vtemp(v.numElems);
for(size_type i = 0; i < v.numElems; ++i)
{
vtemp[i] = (*this)[i] * v[i];
}
return vtemp;
}
template<class T, class Allocator>
Vector<T, Allocator> Vector<T, Allocator>::operator / (const Vector<T, Allocator>& v)const
{
if(this->numElems != v.numElems)
throw std::domain_error("Range not equal!");
Vector<T, Allocator> vtemp(v.numElems);
for(size_type i = 0; i < v.numElems; ++i)
{
vtemp[i] = (*this)[i] / v[i];
}
return vtemp;
}
template<class T, class Allocator>
Vector<T, Allocator>& Vector<T, Allocator>::operator = (const Vector<T, Allocator>& 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<class T, class Allocator>
Vector<T, Allocator>& Vector<T, Allocator>::operator += (const Vector<T, Allocator>& 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<class T, class Allocator>
Vector<T, Allocator>& Vector<T, Allocator>::operator -= (const Vector<T, Allocator>& 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<class T, class Allocator>
Vector<T, Allocator>& Vector<T, Allocator>::operator *= (const Vector<T, Allocator>& 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<class T, class Allocator>
Vector<T, Allocator>& Vector<T, Allocator>::operator /= (const Vector<T, Allocator>& 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<class T, class Allocator>
std::ostream& operator << (std::ostream& os, Vector<T, Allocator>& v)
{
for(Vector<T, Allocator>::size_type i = 0; i < v.size(); ++i)
{
os << v[i] << ' ';
}
os << std::endl;
return os;
}
template<class T, class Allocator>
std::istream& operator >> (std::istream& is, Vector<T, Allocator>& v)
{
for(Vector<T, Allocator>::size_type i = 0; i < v.size(); ++i)
{
is >> v[i];
}
return is;
}
template<class T>
void print(T t, const char* msg = "")
{
std::cout << msg;
std::cout << t;
}
int main()
{
using namespace std;
Vector<double> 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<double> 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<double> 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 <iostream>
#include <memory>
#include <vector>
#include <cstddef>
#include <functional>
#include <algorithm>
#include <exception>
template <class T, class Allocator = std::allocator<T> >
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 <class U>
void init(const U beg, const U end);
List();
template <class U>
List(const U beg, const U end);
template <class U>
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 <class U, class X>
List OpNumber(const List& lhs, U rhs, X op);
template <class U, class Y>
List OpList(const List& lhs, const U& rhs, Y op);
List& negate();
template <class U>
List plus(U rhs);
template <class U>
List operator + (const U& rhs);
template <class U>
List minus(U rhs);
template <class U>
List operator - (const U& rhs);
template <class U>
List multiplies(U rhs);
template <class U>
List operator * (const U& rhs);
template <class U>
List divides(U rhs);
template <class U>
List operator / (const U& rhs);
template <class U>
List modulus(U rhs);
template <class U>
List operator % (const U& rhs);
template <class U>
List& assign(U rhs);
template <class U>
List& operator = (const U& rhs);
template <class U>
List& PlusAssign(U rhs);
template <class U>
List& operator += (const U& rhs);
template <class U>
List& MinusAssign(U rhs);
template <class U>
List& operator -= (const U& rhs);
template <class U>
List& MultipliesAssign(U rhs);
template <class U>
List& operator *= (const U& rhs);
template <class U>
List& DividesAssign(U rhs);
template <class U>
List& operator /= (const U& rhs);
template <class U>
List& ModulusAssign(U rhs);
template <class U>
List& operator %= (const U& rhs);
private:
typedef typename Allocator::rebind<dbNode>::other NodeAlloc;
NodeAlloc alloc;
size_type numElems;
struct dbNode
{
value_type value;
Node prev;
Node next;
}*first, *last;
};
template <class T, class Allocator>
List<T, Allocator>::iterator::iterator() : node(0)
{
}
template <class T, class Allocator>
List<T, Allocator>::iterator::iterator(const iterator& it) : node(it.node)
{
}
template <class T, class Allocator>
List<T, Allocator>::iterator::iterator(typename List::Node _node) : node(_node)
{
}
template <class T, class Allocator>
typename List<T, Allocator>::iterator&
List<T, Allocator>::iterator::operator = (const iterator& rhs)
{
if(&rhs == this)
return *this;
node = rhs.node;
return *this;
}
template <class T, class Allocator>
typename List<T, Allocator>::iterator&
List<T, Allocator>::iterator::operator ++ ()
{
node = node->next;
return *this;
}
template <class T, class Allocator>
typename List<T, Allocator>::iterator
List<T, Allocator>::iterator::operator ++ (int)
{
iterator temp(node);
node = node->next;
return temp;
}
template <class T, class Allocator>
typename List<T, Allocator>::iterator&
List<T, Allocator>::iterator::operator -- ()
{
node = node->prev;
return *this;
}
template <class T, class Allocator>
typename List<T, Allocator>::iterator
List<T, Allocator>::iterator::operator -- (int)
{
iterator temp(node);
node = node->prev;
return temp;
}
template <class T, class Allocator>
bool List<T, Allocator>::iterator::operator == (const iterator& rhs)const
{
return node == rhs.node;
}
template <class T, class Allocator>
bool List<T, Allocator>::iterator::operator != (const iterator& rhs)const
{
展开全部
yun ,一楼还真能写,我都不想做了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我还刚刚学,这个就答不上咯
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |