|
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
typedef struct
{
int keyLen;
char * recordKey;
} keyElement;
void allc (vector <keyElement>&idx)
{
string names [] = {"jack", "mike"};
for (int i = 0; i <2; i ++)
{
keyElement tmp;
tmp.keyLen = names [i] .length ();
tmp.recordKey = new char [tmp.keyLen];
strcpy (tmp.recordKey, (const char *) names [i] .c_str ());
idx.push_back (tmp);
// delete [] tmp.recordKey; // delete here will cause an error: the data in index cannot be accessed in main
}
}
int main ()
{
vector <keyElement> index;
allc (index);
cout << index [0] .recordKey << "" << index [1] .recordKey << endl;
cout << index.size () << endl;
// delete [] index [0] .recordKey; // Why is it wrong to delete here?
// delete [] index [1] .recordKey;
return 0;
} |
|