|
#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; //这里删除会出错:导致main中无法访问index中数据
}
}
int main()
{
vector<keyElement> index;
allc(index);
cout<<index[0].recordKey<<" "<<index[1].recordKey<<endl;
cout<<index.size()<<endl;
// delete [] index[0].recordKey; //这里删除为什么也会出错呢?
// delete [] index[1].recordKey;
return 0;
} |
|