#include<iostream> #include<vector> #include<cstring> using namespace std; int main() { vector<string> svec; string str; cout<<"Enter strings:(Ctrl+Z to end)"<<endl; while(cin>>str) svec.push_back(str); char **parr=new char*[svec.size()]; size_t ix=0; for(vector<string>::iterator iter=svec.begin(); iter!=svec.end();++iter,++ix) { char*p=new char[(*iter).size()+1];/*创建字符数组*/ strcpy(p,(*iter).c_str());/*赋值vector元素的数据到字符数组*/ parr[ix]=p;/*将指向该字符数组的指针插入到字符指针数组*/ } cout<<"Content of Vector:"<<endl; for(vector<string>::iterator iter2=svec.begin(); iter2!=svec.end();++iter2)/*输出vector对象的内容*/ cout<<*iter2<<endl; cout<<"Content of character arrays:"<<endl; for(ix=0;ix!=svec.size();++ix)/*输出字符数组的内容*/ cout<<parr[ix]<<endl; for(ix=0;ix!=svec.size();++ix)/*释放每个字符数组*/ delete [] parr[ix]; delete [] parr;/*释放指针数组*/ return 0; }
用string类型的vector对象创建一个字符指针数组,布布扣,bubuko.com
原文:http://blog.csdn.net/nyist327/article/details/24000837