![]() |
#2
stop12042017-12-14 05:32
|
再去重头想,一点头绪都没了
书上说不建议使用数组与指针
数组可以用 vector 替代 ,指针呢?

int main()
{
int ia[3][4] = {
{0, 1, 2, 3} ,
{4, 5, 6, 7} ,
{8, 9, 10, 11} };
for(int (*p)[4]=ia;p!=ia+3;++p){
for(int *q=*p;q!=*p+4;++q)
cout<<*q<< " ";
cout<<endl;
}
}
//编写程序读入一组 string 类型的数据,并将它们存储在 vector 中。接着,把该 vector 对象复制给一个字符指针数组。为 vector 中的每个元素创建一个新的字符数组,并把该 vector 元素的数据复制到相应的字符数组中,最后把指向该数组的指针插入字符指针数组。
int main()
{
vector<string> svec;
string str;
while(cin>>str)
svec.push_back(str);
char **p=new char*[svec.size()];
size_t ix=0;
for(vector<string>::iterator ster=svec.begin();ster!=svec.end();++ster,ix++){
char *ptmp=new char[(*ster).size()+1];
strcpy(ptmp,(*ster).c_str());
p[ix] = ptmp;
}
cout<<*(*p+1)<<" "<<*p<<" "<<*(p+1)<<" "<<(p[1]+1)<<*p+1;//观察
cout<<endl<<p;
for(ix=0;ix<svec.size();++ix)
delete [] p[ix];
delete [] p;
return 0;
}
[此贴子已经被作者于2017-12-14 04:59编辑过]