把vector中的string对象导入到字符指针数组中
2016-10-03 08:47
225 查看
#include <iostream>
#include <string>
#include <vector>
//#include <cctype>
#include <cstring>
//#include "Sales_item.h"
using namespace std;
//把vector中的string对象导入到字符指针数组中
int main()
{
vector<string> svec;
string str;
cout << " Enter string to svector,( 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){
char *cp = new char[(*iter).size() + 1];
strcpy(cp, (*iter).c_str());
parr[ix] = cp;
++ix;
}
//输出vector内容
for (vector<string>::iterator iter = svec.begin(); iter != svec.end(); ++iter)
cout << *iter << " ";
cout << endl;
//输出数组内容
for (size_t i = 0; i < svec.size(); i++)
cout << parr[i] << " ";
//释放各个字符数组
for (ix = 0; ix !=svec.size(); ix++)
{
delete[] parr[ix];
}
//释放指针数组
delete[] parr;
cout << endl;
system("pause");
return 0;
}
#include <string>
#include <vector>
//#include <cctype>
#include <cstring>
//#include "Sales_item.h"
using namespace std;
//把vector中的string对象导入到字符指针数组中
int main()
{
vector<string> svec;
string str;
cout << " Enter string to svector,( 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){
char *cp = new char[(*iter).size() + 1];
strcpy(cp, (*iter).c_str());
parr[ix] = cp;
++ix;
}
//输出vector内容
for (vector<string>::iterator iter = svec.begin(); iter != svec.end(); ++iter)
cout << *iter << " ";
cout << endl;
//输出数组内容
for (size_t i = 0; i < svec.size(); i++)
cout << parr[i] << " ";
//释放各个字符数组
for (ix = 0; ix !=svec.size(); ix++)
{
delete[] parr[ix];
}
//释放指针数组
delete[] parr;
cout << endl;
system("pause");
return 0;
}
相关文章推荐
- 用string类型的vector对象创建一个字符指针数组
- 编写程序读入一组string类型的数据,并将它们存储在vector中,再将vector对象复制给一个字符指针数组。
- 编写程序定义一个vector 对象,其每个元素都是指向string 类型的指针,读 取该vector 对象,输出每个string 的内容及其相应的长度。
- Delphi的字符(Char),字符串(String),字符串指针(PChar),字符数组arrayofchar(来自http://delphi.cjcsoft.net/论坛)
- Delphi的字符(Char), 字符串(String), 字符串指针( PChar ), 字符数组 array of char
- 字符串string 字符数组与指向字符串的指针pchar的区别与联系
- leetcode -- 557. Reverse Words in a String III 【字符反转 + 字符数组 + 双指针 + 状态记录】
- 『C++ Primer学习笔记』Chapter 3——string对象、vector对象以及数组的配套类型,C风格字符串问题和处理区别
- cpp如何快速安全地将文件读入字符串string或字符数组vector<string>的方法
- 关于C中字符数组,字符指针以及C++中string类型的两两转换及排序
- 指针动态创建数组,二维指针处理字符数组,链表对象排序
- 『C++ Primer学习笔记』Chapter 3——string对象、vector对象以及数组的不同处理方法
- 【编程练习】将string容器保存在字符指针数组中
- VS2008中CString对象与char型数组(或字符指针)间的转换
- string对象与字符数组互操作
- c: string 字符指针与字符数组的区分
- string对象中字符的处理
- [导入]将Byte数组转化为String
- c++笔记--数组对象、vector对象和构造函数
- 字符数组名与字符串指针变量名的区别(转)