尝试为我的数组分配新内存时,我的程序一直在染色。通过调用添加词函数。任何帮助都会很棒!
完整代码下载@http ://www.johnsoncomputertechnologies.org/pgm2demo.zip
#include <iomanip> //Standard IO Manipulators Library
#include <algorithm>
#include <iostream> //Standard input/output library
#include <string> //Standard string Library
#include <cassert> //Error handeling Library
#include "pgm2.h"
using namespace std;
dictionary::dictionary()
{
wordptr = new value_type[DICTONARY_CAPACITY];
deffptr = new value_type[DICTONARY_CAPACITY];
used = 0;
}
dictionary::dictionary(const dictionary& other)
{
value_type *wordptr;
value_type *deffptr;
wordptr = new value_type[other.capacity];
deffptr = new value_type[other.capacity];
used = other.used;
capacity = other.capacity + 1;
copy(wordptr, other.wordptr + used, other.wordptr);
copy(deffptr, other.deffptr + used, other.deffptr);
}
dictionary::~dictionary()
{
delete [] wordptr;
delete [] deffptr;
}
//Functions
void dictionary::reserve(size_type new_capacity)
{
value_type *larger_word;
value_type *larger_deff;
if(new_capacity == capacity)
return; //There allocated memory is already the correct size
if(new_capacity < used)
new_capacity = used;
larger_word = new value_type[new_capacity];
larger_deff = new value_type[new_capacity];
copy(wordptr, wordptr + used, larger_word);
copy(deffptr, deffptr + used, larger_deff);
delete [] wordptr;
delete [] deffptr;
wordptr = larger_word;
deffptr = larger_deff;
capacity = new_capacity;
}
void dictionary::addword(const value_type& word, const value_type& deff)
{
if(used == capacity)
reserve(used+1);
wordptr[used] = word;
deffptr[used] = deff;
++used;
}
void dictionary::containsPat (value_type patt)
{
size_type i;
size_type finds;
int found = 0;
for(i = 0; i < used; i++)
{
finds = wordptr[i].find(patt);
if (finds != std::string::npos)
{
cout << wordptr[i] << " - " << deffptr[i] << endl;
found = 1;
}
}
if (found == 0)
cout << "*** No words contain the pattern '" << patt << "'" << endl;
}
void dictionary::endPat(value_type patt)
{
size_type i;
size_type finds;
int found = 0;
size_type pos;
for(i = 0; i < used; i++)
{
finds = wordptr[i].find(patt);
pos = wordptr[i].length() - patt.length();
if (finds == pos)
{
cout << wordptr[i] << " - " << deffptr[i] << endl;
found = 1;
}
}
if (found == 0)
cout << "*** No words end with pattern '" << patt << "'" << endl;
}
void dictionary::startPat(value_type patt)
{
size_type i;
size_type finds;
int found = 0;
for(i = 0; i < used; i++)
{
finds = wordptr[i].find(patt);
if (finds == 0)
{
cout << wordptr[i] << " - " << deffptr[i] << endl;
found = 1;
}
}
if (found == 0)
cout << "*** No words begin with pattern '" << patt << "'" << endl;
}
void dictionary::print()
{
size_type i;
cout << "----------------------------------------------------------------------------------------------------------" << endl;
cout << "--- Dictionary Words and Meanings ---" << endl;
cout << "----------------------------------------------------------------------------------------------------------" << endl;
for(i = 0; i < used; i++)
{
cout << wordptr[i] << " - " << deffptr[i] << endl;
}
cout << "There are " << used << " words in the dictionary!" << endl;
}
pgm2.h
#include <iomanip> //Standard IO Manipulators Library
#include <algorithm>
#include <iostream> //Standard input/output library
#include <string> //Standard string Library
#include <cassert> //Error handeling Library
using namespace std;
class dictionary
{
public:
typedef string value_type;
typedef std::size_t size_type;
static const size_type DICTONARY_CAPACITY = 25;
//Default Constructor
dictionary();
dictionary(dictionary& other);
//Destructor
~dictionary();
//Functions
void reserve(size_type new_capacity);
void addword(const value_type& word, const value_type& deff);
void startPat (value_type patt);
void endPat(value_type patt);
void containsPat(value_type patt);
void print();
private:
value_type *wordptr;
value_type *deffptr;
size_type used;
size_type capacity;
};