请教 “返回数组指针” 和“数组指针赋值” 的问题
std::string* getSiteType(){
std::string *_type = new std::string[2];
int j = 0;
//Langure的定义是std::map <std::string, std::vector <FromTo *> > *Langure 并且已经初始化的, 肯定有值
for(std::map <std::string, std::vector <FromTo *> > ::iterator i = Langure-> begin(); i != Langure-> end(); ++i){
_type[j] = i-> first; //这里赋值有问题, 会出现“断错误”
j++;
}
return _type;
}
void Create(GtkWidget *vbox)
{
std::string *_Type = getSiteType();
int _typeSize = _Type-> size();
int i = 0;
while(i < _typeSize){
std::string *a = _Type[i]; //这里不知道怎么把数组的元素挨个读出来
printf( "111111111%s ", a.c_str());
i++;
}
}
谢谢大家。
[解决办法]
std::string *_type = new std::string[2];
for(std::map <std::string, std::vector <FromTo *> > ::iterator i = Langure-> begin(); i != Langure-> end(); ++i){
_type[j] = i-> first; //这里赋值有问题, 会出现“断错误”
j++; //j ++ ,j都到几了? std::string *_type = new std::string[2];只有两个string.....
}
[解决办法]
std::string *_Type = getSiteType();
int _typeSize = _Type-> size();
这个size()可不是数组里string的个数,是第一个string的长度。
要么用vector <string>
要么返回pair <string *, int> 同时返回地址和长度