读书人

一个程序的有关问题

发布时间: 2012-02-29 16:44:10 作者: rapoo

一个程序的问题
这段代码是C++ Primer的一个习题

读入一段文本到vector对象,每个单词存储为vector中的一个元素。把vector对象中每个单词转化为大写字母。输出vector对象中转化后的元素,每8个单词为一行输出。

#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;
}
system( "pause ");
return 0;
}

在这个代码里面,能不能具体解释
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;
}
尤其是svec[ix][index]代表的是什么?

[解决办法]
svec[ix][index]
ix是每个svec元素的下标
index是每一个svec元素中的字母的下标
换句话说,svec里有ix个单词,每个单词有index个字母
[解决办法]

读书人网 >C++

热点推荐