新人求助:关于 c++ primer 第4版习题的问题

ysysbaobei 2008-10-13 05:15:18
习题3.8
读入一段文本到vector对象,每个单词存储为vector中的一个元素。把vector对象中的每个单词转化为大写字母。输出vector对象中转化后的元素,每八个单词为一行输出。

望高手指点下,给出vc6.0环境下能编译通过的程序
...全文
182 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
ysysbaobei 2008-10-14
  • 打赏
  • 举报
回复
谢谢LS各位朋友的帮助,问题解决了
ysysbaobei 2008-10-14
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 libinsuaige 的回复:]
兄台,你输入字符串的结束标志是什么,我怎么解释不了呀,即使是null也结束不了!!
[/Quote]
输入字符串结束后,按ctrl+z,回车;再按ctrl+z,再按一次回车,就退出了
ysysbaobei 2008-10-14
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 taodm 的回复:]
扔了VC6吧,别用它浪费生命了。
[/Quote]
此话怎讲?
libinsuaige 2008-10-14
  • 打赏
  • 举报
回复
兄台,你输入字符串的结束标志是什么,我怎么解释不了呀,即使是null也结束不了!!
rcbblgy 2008-10-14
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 taodm 的回复:]
扔了VC6吧,别用它浪费生命了。
[/Quote]
这话怎么说?
taodm 2008-10-14
  • 打赏
  • 举报
回复
扔了VC6吧,别用它浪费生命了。
gjlstc 2008-10-14
  • 打赏
  • 举报
回复
呵呵 学习下
cs258448 2008-10-13
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
using namespace std;
int main()
{
vector<string> svec;
string str;
// 读入文本到vector 对象
cout << "Enter text(Ctrl+Z to end):" << endl;
while (cin>>str)
svec.push_back(str);
//将vector 对象中每个单词转化为大写字母,并输出
if (svec.size() == 0) {
cout << "No string?!" << endl;
return -1;
}
cout << "Transformed elements from the vector:"
<< endl;
for (vector<string>::size_type ix = 0; ix != svec.size(); ++ix) {
for (string::size_type index = 0; index != svec[ix].size();
++index)
if (islower(svec[ix][index]))
//单词中下标为index 的字符为小写字母
svec[ix][index] = toupper(svec[ix][index]);
cout << svec[ix] << " ";
if ((ix + 1) % 8 == 0)//每8 个单词为一行输出
cout << endl;
}
return 0;
}
习题解答上的答案
woaiyu_liang 2008-10-13
  • 打赏
  • 举报
回复
楼主,你的问题在于看书不认真,或者不用心总结

我看容器时的心得是,看完后要回顾一下有关容器的所有操作:如何新建容器、如何插入元素、如何遍历容器、如何删除元素等等。

如果不用心总结的话,是无法系统把握到知识的。

其实你说的问题在c++ primer上甚至直接就能找到答案,好好看书吧,不然照这么下去,你学什么都是半桶水的
warden325 2008-10-13
  • 打赏
  • 举报
回复
刚开始这些可能多多自己尝试会更有好处……额……
xf_pan 2008-10-13
  • 打赏
  • 举报
回复
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;
int main()
{
vector<string> vContent;
string word;
while(cin>>word)
{
transform(word.begin(),word.end(),word.begin(),toupper);
vContent.push_back(word);
}

int count =1;
for(vector<string>::size_type i=0;i<vContent.size();i++)
{
cout<<vContent[i]<<" ";
if(count++%8 ==0)
{
cout<<endl;
}
}
getchar();
return 0;
}
xf_pan 2008-10-13
  • 打赏
  • 举报
回复
要装换为大写。。再加一句
transform(word.begin(),word.end(),word.begin(),toupper);
在svec.push_back(word); 前。。
头文件夹上:#include <algorithm>
ysuliu 2008-10-13
  • 打赏
  • 举报
回复
1,用文件输入,在C++ Primer里有fstream的用法
2,最简单的换行控制
int i=0;
for(it = v.begin(); it!=v.end(); ++v)
{
cout<<*it;
if(++i % 8 == 0)cout<<endl;
}
3,代码可不就是满足需求嘛,别说那么难听,其实都一样~
xf_pan 2008-10-13
  • 打赏
  • 举报
回复
word.size(); 改为
svec.size();
xf_pan 2008-10-13
  • 打赏
  • 举报
回复
1.没有实现任意输入多个单词
while(cin>>word)
{
svec.push_back(word);
}

2.8个单词一行输出 没实现

int count =1;
for(int i=0;i<word.size();i++)
{
cout<<word;
if(count++%8==0)
cout<<endl;

}
kkndciapp 2008-10-13
  • 打赏
  • 举报
回复
#include <iostream> 
#include <string>
#include <vector>
#include <cctype>
using namespace std;

void main()
{
vector<string> vec;
char ch[20];
while(cin>>ch)
{
string str=ch;
vec.push_back(str);
}
for(int i=0;i<vec.size();i++)
for(int j=0;j<strlen(vec[i].data());j++)
{
if(islower(vec[i][j]))
vec[i][j]=toupper(vec[i][j]);
}
for(int k=0;k<vec.size();k++)
cout<<vec[k]<<endl;
}

abc
edf
dfe
^Z
^Z
ABC
EDF
DFE
Press any key to continue
ysysbaobei 2008-10-13
  • 打赏
  • 举报
回复
我写了好久没写出来,呵呵,所以求助

#include <iostream>
#include <string>
#include <vector>
#include <cctype>
using namespace std;
int main()
{
vector<string> svec;
string word("wo ai ni ,888,777,666,baobei!!!");
for (string::size_type ix = 0; ix != word.size(); ++ix)
{
word[ix] = toupper(word[ix]);
}
cout << word << endl;
for (vector<string>::size_type ix2 = 0; ix2 != 1; ++ix2)
{
svec.push_back(word);
cout << svec[ix2] ;
}
cout << endl;
return 0;
}

这是我写的:
1.没有实现任意输入多个单词
2.8个单词一行输出 没实现
3.我的代码有凑要求的嫌疑
忘大家指点下!
ysuliu 2008-10-13
  • 打赏
  • 举报
回复
你是在学习吗?如果是的话干嘛不自己写?相信看了C++ Primer写这个程序应该没问题的。。
ysysbaobei 2008-10-13
  • 打赏
  • 举报
回复
顶下

65,211

社区成员

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

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