c++编程练习时遇到的问题(附上程序)

zzzwilliam 2008-12-11 03:10:52
//读入一段文本到vector 对象,每个单词存储为vector 中的一个元素。
//把vector 对象中每个单词转化为大写字母。
//输出vector 对象中转化后的元素,每8 个单词为一行输出。
//使用迭代器访问vector 中的元素
#include "iostream"
#include "vector"
#include "string"
using namespace std;

int main()
{
vector<string> svec;
string str;
while (cin >> str)
svec.push_back(str);
if (svec.size() == 0){
cout << "NO" << endl;
return -1;
}
vector<string>::size_type cnt = 0;
vector<string>::iterator iter = svec.begin();
for (iter = svec.begin(); iter != svec.end(); ++iter)
string::size_type index = 0;{
for (index = 0; index != (*iter).size(); ++index)
if(islower ( (*iter)[index] ))
(*iter)[index] = toupper((*iter)[index]);
cout << *iter << " ";
++cnt;

if (svec.size() % 8 == 0)
cout << endl;
}
system("PAUSE");
return 0;
}



为什么会出现未定义?string::size_type index = 0;不是定义INDEX么?
还有能不能在FOR语句里定义?例如:
for (vector<string>::iterator iter = svec.begin();
iter != svec.end(); ++iter) {
for (string::size_type index = 0; index != (*iter).size();
++index)
我这样编译也提示未定义变量.
正确的代码是什么?
还有最好能解释下上面那段代码,我怕是我理解有误...自己解释的不正确...新人=.=正在自学:)希望大家帮帮我!
谢谢!
...全文
93 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzzwilliam 2008-12-11
  • 打赏
  • 举报
回复
啊,!!!!谢谢
rejoice914 2008-12-11
  • 打赏
  • 举报
回复
#include "iostream"
#include "vector"
#include "string"
using namespace std;

int main()
{
vector <string> svec;
string str;
while (cin >> str)
svec.push_back(str);
if (svec.size() == 0){
cout << "NO" << endl;
return -1;
}
vector <string>::size_type cnt = 0;
vector <string>::iterator iter = svec.begin();
for (iter = svec.begin(); iter != svec.end(); ++iter)
#############
{
string::size_type index = 0;//你写到外面去了!
for (index = 0; index != (*iter).size(); ++index)
if(islower ( (*iter)[index] ))
(*iter)[index] = toupper((*iter)[index]);
cout << *iter << " ";
++cnt;

if (svec.size() % 8 == 0)
cout << endl;
}
system("PAUSE");
return 0;
}
rejoice914 2008-12-11
  • 打赏
  • 举报
回复
#include "iostream"
#include "vector"
#include "string"
using namespace std;

int main()
{
vector <string> svec;
string str;
while (cin >> str)
svec.push_back(str);
if (svec.size() == 0){
cout << "NO" << endl;
return -1;
}
vector <string>::size_type cnt = 0;
vector <string>::iterator iter = svec.begin();
for (iter = svec.begin(); iter != svec.end(); ++iter)
#############
{
string::size_type index = 0;//你写到外面去了!
for (index = 0; index != (*iter).size(); ++index)
if(islower ( (*iter)[index] ))
(*iter)[index] = toupper((*iter)[index]);
cout << *iter << " ";
++cnt;

if (svec.size() % 8 == 0)
cout << endl;
}
system("PAUSE");
return 0;
}
rejoice914 2008-12-11
  • 打赏
  • 举报
回复
#include "iostream"
#include "vector"
#include "string"
using namespace std;

int main()
{
vector <string> svec;
string str;
while (cin >> str)
svec.push_back(str);
if (svec.size() == 0){
cout << "NO" << endl;
return -1;
}
vector <string>::size_type cnt = 0;
vector <string>::iterator iter = svec.begin();
for (iter = svec.begin(); iter != svec.end(); ++iter)
#############
{
string::size_type index = 0;//你写到外面去了!
for (index = 0; index != (*iter).size(); ++index)
if(islower ( (*iter)[index] ))
(*iter)[index] = toupper((*iter)[index]);
cout << *iter << " ";
++cnt;

if (svec.size() % 8 == 0)
cout << endl;
}
system("PAUSE");
return 0;
}

65,211

社区成员

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

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