new and delete!

tjj5203 tjj5203 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string>vec;
string a;
cout<<"input the string (ctrl+z to end)"<<endl;
while(cin>>a)
{
vec.push_back(a);
}
char **p=new char*[vec.size()];
vector<string>::iterator iter=vec.begin();
int i(0);
while(iter!=vec.end())
{
char *p1=new char [(*iter).size()];
strcpy(p1,(*iter).c_str());
p[i]=p1;
++iter;
++i;
}
for(int i(0);i!=vec.size();++i)
delete p[i];//这里参考答案上是delete []p[i],我感觉是delete p[i],哪个对呢!?
delete []p;
system("pause");
return 0;
}
...全文
评论 8
收藏 已收藏
分享
返回顶部
tjj5203
C++ 语言 65,213 成员 250,513 内容