#include <iostream>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector <string> svec;
string str;
while (cin >> str)
svec.push_back(str);
//创建字符指针数组
char **parr = new char *[svec.size()];
size_t ix = 0;
//处理vector元素
for (vector<string>::iterator iter = svec.begin(); iter != svec.end(); iter++)
{
//创建字符数据
char *p = new char[(*iter).size()+1];
//复制vector元素的数据到字符数组
strcpy(p, (*iter).c_str());
//将指向该字符数组的指针插入到字符指针数组
parr[ix] = p;
}
//输出vector对象的内容
for (vector<string>::iterator iter2 = svec.begin(); iter2 != svec.end(); iter2++)
cout << *iter2 << 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;
}
C++primer 4.34/4.35
最新推荐文章于 2024-01-02 19:08:18 发布