我是学生,我正在为C ++中的数组做一个静态库,所以我不必每次都在课程中重写代码。
我在中学读二年级,所以我不是专家。我希望我的代码与所有类型(int,float,ecc。)兼容,但我遇到了一些麻烦。
你能看看我的代码吗?
我看过这个页面: Template (C++) - not sure if correct
My_vec.h
/*template My_vec.h
Ben Burk
Header file for a vector data structure.
*/
#include <ostream>
using namespace std;
template <typename T> class My_vec
{
//member variables
int size, capacity;
T *ptr;
public:
//member functions
template <typename T> My_vec<T>::My_vec()
{
size = 0; capacity = 10;
ptr = new T[capacity];
}
template <typename T> My_vec<T>::My_vec(const My_vec<T>& vec)
{
if (!vec.is_empty())
{
size = vec.size;
capacity = vec.capacity;
ptr = new T[capacity];
for (int i = 0; i < capacity; i++)
ptr[i] = vec.ptr[i];
}
}
template <typename T> My_vec<T>::~My_vec()
{
delete[] ptr;
}
template <typename T> My_vec<T>& My_vec<T>::operator=(const My_vec<T>& vec)
{
if (this == &vec)
return *this;
this->size = vec.size; this->capacity = vec.capacity;
this->ptr = new T[vec.capacity];
for (int i = 0; i < this->capacity; i++)
this->ptr[i] = vec.ptr[i];
}
template <typename T> int My_vec<T>::get_size() const
{
return size;
}
template <typename T> int My_vec<T>::get_capacity() const
{
return capacity;
}
template <typename T> T& My_vec<T>::operator[](int i) const
{
if (i < 0 || i > capacity)
{
try { throw i; }
catch (int e)
{
cerr << "An exception occurred at index: " << e << '\n';
cerr << "Index out of bounds\n";
}
}
else
{
return ptr[i];
}
}
template <typename T> T& My_vec<T>::operator[](int i)
{
if (i < 0 || i > capacity)
{
try { throw i; }
catch (int e)
{
cerr << "An exception occurred at index: " << e << '\n';
cerr << "Index out of bounds!\n";
}
}
else
{
return ptr[i];
}
}
template <typename T> bool My_vec<T>::is_empty() const
{
return (size == 0) ? 1 : 0;
}
template <typename T> T& My_vec<T>::elem_at_rank(int r) const
{
if (r <= 0 || r > capacity)
{
try { throw r; }
catch (int e)
{
cerr << "An exception occurred at rank: " << e << '\n';
cerr << "Element could not be found!\n";
}
}
else
return ptr[r-1];
}
template <typename T> void My_vec<T>::insert_at_rank(int r, const T& elem)
{
if (r <= 0 || r > capacity)
{
try { throw r; }
catch (int e)
{
cerr << "An exception occurred at rank: " << e << '\n';
cerr << "Element could not be inserted!\n";
}
}
else
{
if (size + 1 > capacity)
{
capacity *= 2;
ptr = new T[capacity];
}
size++;
for (int i = size - 1; i > r - 1; i--)
{
ptr[i] = ptr[i-1];
}
ptr[r-1] = elem;
}
}
template <typename T> void My_vec<T>::replace_at_rank(int r, const T& elem)
{
if (r <= 0 || r > capacity)
{
try { throw r; }
catch (int e)
{
cerr << "An exception occurred at rank: " << e << '\n';
cerr << "Element could not be replaced!\n";
}
}
else
{
if (ptr[r-1] == NULL)
size++;
ptr[r-1] = elem;
}
}
template <typename T> void My_vec<T>::remove_at_rank(int r)
{
if (r <= 0 || r > capacity)
{
try { throw r; }
catch (int e)
{
cerr << "An exception occurred at rank: " << e << '\n';
cerr << "Element could not be removed!\n";
}
}
else
{
for (int i = r-1; i < size; i++)
{
ptr[i] = ptr[i+1];
}
size--;
}
}
};
ostream& operator<<(ostream& out, const My_vec<T>& vec);
int find_max_index(const My_vec<T>& v, int size);
void sort_max(My_vec<T>& vec);
My_vec.cpp
#include <iostream>
#include "My_vec.h"
using namespace std;
ostream& operator<<(ostream& out, const My_vec<T>& vec)
{
out << "<";
for (int x = 0; x < vec.get_capacity()-1; x++)
out << vec[x] << ",";
out << vec[vec.get_capacity()-1];
out << ">\n";
}
int find_max_index(const My_vec<T>& v, int size)
{
int i = -1;
for (int x = 0; x < v.get_size(); x++)
{
if (v[x+1] > v[x])
i = x+1;
}
return i;
}
void sort_max(My_vec<T>& vec)
{
T c = NULL;
for (int a = 0; a < vec.get_capacity(); a++)
{
for (int x = 0; x < vec.get_capacity()-1; x++)
{
if (vec[x+1] < vec[x])
{
c = vec[x]; vec[x] = vec[x+1]; vec[x+1] = c;
}
}
}
}
编辑:我在编译以下文件时遇到问题。我的任务是创建一个可以采用多个变量类型char,int,double的类。我的问题是为什么我不能编译这个程序?
答案 0 :(得分:1)
您的头文件应如下所示:
/*template My_vec.h
Ben Burk
Header file for a vector data structure.
*/
#include <iostream>
using namespace std;
template <typename T> class My_vec
{
//member variables
int size, capacity;
T *ptr;
public:
//member functions
My_vec()
{
size = 0; capacity = 10;
ptr = new T[capacity];
}
My_vec(const My_vec<T>& vec)
{
if (!vec.is_empty())
{
size = vec.size;
capacity = vec.capacity;
ptr = new T[capacity];
for (int i = 0; i < capacity; i++)
ptr[i] = vec.ptr[i];
}
}
~My_vec()
{
delete[] ptr;
}
My_vec& operator=(const My_vec<T>& vec)
{
if (this == &vec)
return *this;
this->size = vec.size; this->capacity = vec.capacity;
this->ptr = new T[vec.capacity];
for (int i = 0; i < this->capacity; i++)
this->ptr[i] = vec.ptr[i];
}
int get_size() const
{
return size;
}
int get_capacity() const
{
return capacity;
}
T &operator[](int i) const
{
if (i < 0 || i > capacity)
{
try { throw i; }
catch (int e)
{
cerr << "An exception occurred at index: " << e << '\n';
cerr << "Index out of bounds\n";
}
}
else
{
return ptr[i];
}
}
T &operator[](int i)
{
if (i < 0 || i > capacity)
{
try { throw i; }
catch (int e)
{
cerr << "An exception occurred at index: " << e << '\n';
cerr << "Index out of bounds!\n";
}
}
else
{
return ptr[i];
}
}
bool is_empty() const
{
return (size == 0) ? 1 : 0;
}
T& elem_at_rank(int r) const
{
if (r <= 0 || r > capacity)
{
try { throw r; }
catch (int e)
{
cerr << "An exception occurred at rank: " << e << '\n';
cerr << "Element could not be found!\n";
}
}
else
return ptr[r - 1];
}
void insert_at_rank(int r, const T& elem)
{
if (r <= 0 || r > capacity)
{
try { throw r; }
catch (int e)
{
cerr << "An exception occurred at rank: " << e << '\n';
cerr << "Element could not be inserted!\n";
}
}
else
{
if (size + 1 > capacity)
{
capacity *= 2;
ptr = new T[capacity];
}
size++;
for (int i = size - 1; i > r - 1; i--)
{
ptr[i] = ptr[i - 1];
}
ptr[r - 1] = elem;
}
}
void replace_at_rank(int r, const T& elem)
{
if (r <= 0 || r > capacity)
{
try { throw r; }
catch (int e)
{
cerr << "An exception occurred at rank: " << e << '\n';
cerr << "Element could not be replaced!\n";
}
}
else
{
if (ptr[r - 1] == NULL)
size++;
ptr[r - 1] = elem;
}
}
void remove_at_rank(int r)
{
if (r <= 0 || r > capacity)
{
try { throw r; }
catch (int e)
{
cerr << "An exception occurred at rank: " << e << '\n';
cerr << "Element could not be removed!\n";
}
}
else
{
for (int i = r - 1; i < size; i++)
{
ptr[i] = ptr[i + 1];
}
size--;
}
}
};
template <typename T>
ostream& operator<<(ostream& out, const My_vec<T>& vec)
{
out << "<";
for (int x = 0; x < vec.get_capacity() - 1; x++)
out << vec[x] << ",";
out << vec[vec.get_capacity() - 1];
out << ">\n";
}
template <typename T>
int find_max_index(const My_vec<T>& v, int size)
{
int i = -1;
for (int x = 0; x < v.get_size(); x++)
{
if (v[x + 1] > v[x])
i = x + 1;
}
return i;
}
template <typename T>
void sort_max(My_vec<T>& vec)
{
T c = NULL;
for (int a = 0; a < vec.get_capacity(); a++)
{
for (int x = 0; x < vec.get_capacity() - 1; x++)
{
if (vec[x + 1] < vec[x])
{
c = vec[x]; vec[x] = vec[x + 1]; vec[x + 1] = c;
}
}
}
}
我认为这是一个开始,但还有很多需要做的清理工作,例如:
这是什么?
try { throw r; }
catch (int e)
{
cerr << "An exception occurred at rank: " << e << '\n';
cerr << "Element could not be found!\n";
}
使用这样的例外情况没有任何意义。