首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

请问 “返回数组指针” 和“数组指针赋值” 的有关问题

2012-02-20 
请教“返回数组指针” 和“数组指针赋值” 的问题std::string*getSiteType(){std::string*_typenewstd::strin

请教 “返回数组指针” 和“数组指针赋值” 的问题
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> 同时返回地址和长度

热点排行