question about 2 functions

i'm working on an assignment to make a vector emulator and i'm having problem solving 2 of my functions the last 2 or ( insert and printing ) ..
so i was hoping someone could help me with that ?



string* addEntry(string *dynamicArray, int &size, string newEntry)
{
string *newArray = new string[size + 1];

for(int ix = 0; ix < size; ++ix)
{
newArray[ix] = dynamicArray[ix];
}
newArray[size] = newEntry;
++size;
delete [] dynamicArray;
return newArray;
}
string*deleteEntry(string *dynamicArray, int&size, string entryToDelete)
{

string *newArray = new string[size - 1];
for(int ix = 0; ix < size; ++ix)
{
//copy all except entryToDelete (hér þarf að laga)
newArray[ix] = dynamicArray[ix];
}
--size;
return newArray;
}
int findEntry(string *dynamicArray, int size, string entry)
{
// Finds the first occurrence of an entry in the dynamic array, returning the position (int)
size_t found;
found=entry.find("Aron");
if (found!=string::npos)
{ cout << "the string you entered can be found" << int(found) << endl;
return 0;
}
else return -1;
// where found. If not found, then returns -1
}
string* insertEntry(string *dynamicArray, int &size, string newEntry, int pos);
// Inserts an entry into position pos in the dynamic array, returning a pointer to the
// expanded array. size should contain the size of the array. It
// will be updated when the array is expanded
void print(string *dynamicArray, int size);
Please use code tags.
deleteEntry is doing a pop_back, I suggest you to do a function delete_position
1
2
3
4
string* deleteEntry(string *dynamicArray, int &size, string entryToDelete){
  int pos = findEntry(dynamicArray, size, entryToDelete);
  return delete_position(dynamicArray, size, pos);
}


About find:
http://www.cplusplus.com/forum/general/31115/#msg168528

If you want to emulate vector, maybe will be better if you make a class with size and dynamicArray as members of that class (avoiding data corruption)
Last edited on
Topic archived. No new replies allowed.