各位大侠,帮忙解决两个菜鸟级的问题,谢谢!

yuyeshijie 2012-08-09 11:40:17
第一个问题是把一个string类型里面的单词存到一个vector<string> 里面;
第二个问题:
string str = "";
vector<string> test2;
while( (cin>>str)&& (str != "end"))
{
test2.push_back( str );
}
const int lenth = test2.size();
char*chr[] = new char[lenth];
for( int i = 0; i != lenth; ++i)
{
chr[i] = test2[i];
cout<<"chr[i] = "<<chr[i]<<endl;;
}
//cout<<"str = "<<str<<endl;

return 0;

这段代码报错error C2440: “类型转换”: 无法从“char *”转换为“char *[]”;
该怎么处理呢?
...全文
180 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuyeshijie 2012-08-13
  • 打赏
  • 举报
回复
谢谢啊
IVERS0N 2012-08-11
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 的回复:]

引用 12 楼 的回复:
引用 10 楼 的回复:

引用 8 楼 的回复:
引用 6 楼 的回复:

3楼第二题是对了,我也做出来了,但是第一题你理解错误了,是这样的
比如:string str = “nbua nuioa ,dsnaio bfduai ! dadf... fdniu !”;
vector< string >test 里面要存的是 test[0] = "nbu……
[/Quote]



vector<string> vstr;

string src_str = "this is my world!hello,my world!";
string temp_str;

int cur_pos = 0;
int pos = 0;

//标点替换空格
while(1)
{
if(ispunct(src_str[cur_pos]))
{
src_str = src_str.replace(cur_pos,1,1,' ');
}
if(cur_pos++ == src_str.length())
break;
}
cur_pos = 0;
//遇到空格切割
while(1)
{
pos = src_str.find(' ',cur_pos);
if(pos == string::npos)
break;
temp_str = src_str.substr(cur_pos,pos - cur_pos);
cur_pos = (pos + 1);
printf("pos:%d cur:%d",pos,cur_pos);
vstr.push_back(temp_str);
}


yuyeshijie 2012-08-11
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 的回复:]
引用 10 楼 的回复:

引用 8 楼 的回复:
引用 6 楼 的回复:

3楼第二题是对了,我也做出来了,但是第一题你理解错误了,是这样的
比如:string str = “nbua nuioa ,dsnaio bfduai ! dadf... fdniu !”;
vector< string >test 里面要存的是 test[0] = "nbua" ; test[1] = "……
[/Quote]
能演示一下吗?
yuyeshijie 2012-08-11
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 的回复:]
第一个的话,你如果要是想传入字符,可以用vector<char>,貌似vector<string>要传入string数组呀!
第二个的话,吧char*chr[] = new char[lenth]; 中的[]去掉应该就ok了
[/Quote]
你说的全错啊,怀疑你的水平
willYanwill 2012-08-11
  • 打赏
  • 举报
回复
第一个的话,你如果要是想传入字符,可以用vector<char>,貌似vector<string>要传入string数组呀!
第二个的话,吧char*chr[] = new char[lenth]; 中的[]去掉应该就ok了
IVERS0N 2012-08-10
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

3楼第二题是对了,我也做出来了,但是第一题你理解错误了,是这样的
比如:string str = “nbua nuioa ,dsnaio bfduai ! dadf... fdniu !”;
vector< string >test 里面要存的是 test[0] = "nbua" ; test[1] = "nuioa"; test[2] = "dsnaio";
[/Quote]

string str = “nbua nuioa ,dsnaio bfduai ! dadf... fdniu !”;

你这个字符串的规律是什么
yuyeshijie 2012-08-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
1.你问题2不是实现了吗

string str = "";
vector<string> test2;
while( (cin>>str)&& (str != "end"))
{
test2.push_back( str );
}

2.先new一个动态指针数组

string str = "";
vector<string> test2;
whil……
[/Quote]
3楼。。。。。。。
yuyeshijie 2012-08-10
  • 打赏
  • 举报
回复
3楼第二题是对了,我也做出来了,但是第一题你理解错误了,是这样的
比如:string str = “nbua nuioa ,dsnaio bfduai ! dadf... fdniu !”;
vector< string >test 里面要存的是 test[0] = "nbua" ; test[1] = "nuioa"; test[2] = "dsnaio";
IVERS0N 2012-08-10
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

引用 8 楼 的回复:
引用 6 楼 的回复:

3楼第二题是对了,我也做出来了,但是第一题你理解错误了,是这样的
比如:string str = “nbua nuioa ,dsnaio bfduai ! dadf... fdniu !”;
vector< string >test 里面要存的是 test[0] = "nbua" ; test[1] = "nuioa"; test[2……
[/Quote]

用ispunct判断标点,把字符串的全部标点用string.replace替换成空格
用string.find找到空格,string.substr拷贝新字符串
DBFNO 2012-08-10
  • 打赏
  • 举报
回复
char*chr[] = new char[lenth];

改为:char*chr = new char[lenth];

chr指向动态申请内存的首地址就行。
yuyeshijie 2012-08-10
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]
引用 6 楼 的回复:

3楼第二题是对了,我也做出来了,但是第一题你理解错误了,是这样的
比如:string str = “nbua nuioa ,dsnaio bfduai ! dadf... fdniu !”;
vector< string >test 里面要存的是 test[0] = "nbua" ; test[1] = "nuioa"; test[2] = "dsnaio";
……
[/Quote]
没有规律,也就是把一段话里面去掉空格和标点符号,把各个单词加到vector里面
wanglu343280746 2012-08-10
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
char*chr[] = new char[lenth];
问题在此!!
改为
char*chr= new char[lenth];
[/Quote]
++
titer1 2012-08-09
  • 打赏
  • 举报
回复
char*chr[] = new char[lenth];
问题在此!!
改为
char*chr= new char[lenth];
IVERS0N 2012-08-09
  • 打赏
  • 举报
回复
1.你问题2不是实现了吗

string str = "";
vector<string> test2;
while( (cin>>str)&& (str != "end"))
{
test2.push_back( str );
}

2.先new一个动态指针数组

string str = "";
vector<string> test2;
while( (cin>>str)&& (str != "end"))
{
test2.push_back( str );
}

const int lenth = test2.size();
char **chr = new char*[lenth];
for( int i = 0; i != lenth; ++i)
{
chr[i] = (char*)test2[i].c_str();
cout<<"chr[i] = "<<chr[i]<<endl;;
}
delete []chr;

Qyee16 2012-08-09
  • 打赏
  • 举报
回复
char*chr[] = new char[lenth];

new 出来的是 char*类型,当然不能转换为 *char[]le

char *chr new char【lenth】;
dahaiI0 2012-08-09
  • 打赏
  • 举报
回复
string有个函数叫什么c_str();转成const char*

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧