漠上寒沙上腐书:C++ iterotor
来源:百度文库 编辑:偶看新闻 时间:2024/04/26 21:45:02
例 vector
const vector
*cit = 1; // ok: cit can change its underlying element
++cit; // error: can't change the value of cit
例:读入一段文本到 vector 对象,每个单词存储为 vector 中的一个元素。把 vector 对象中每个单词转化为小写字母。输出 vector 对象中转化后的元素,每八个单词为一行输出
--摘自C++primer 3.14
//自己写的一个代码,于VS2008测试通过,因是VC6.0对标准C++支持不好,若要测试,需要更改预处理.
//用下标操作
#include
#include
#include
using std::vector;
using std::string;
using std::cout;
using std::cin;
using std::endl;
int main()
{
vector
string word;
while (cin>>word)
svec.push_back(word);
for (vector
{
for (string::size_type index=0;index!=svec[ix].size();++index) //注: svec[ix].size()返回类型为 string::size_type
{
svec[ix][index]=tolower(svec[ix][index]);
}
}
for (vector
{
cout<
{
cout<
}
getchar();
return 0;
}
//用iterator改进...
int main()
{
vector
string word;
while (cin>>word)
svec.push_back(word);
for (vector
for (string::iterator iter2=(*iter).begin();iter2!=(*iter).end();++iter2)
*iter2=tolower(*iter2);
//上面两行代码亦改为: for (string::size_type index=0;index!=(*iter).size();++index)
//但不建议 (*iter)[index]=tolower((*iter)[index]);
int ix=0;
for (vector
{
cout<<*iter<<' ';
if ((ix+1)%8==0)
{
cout<
}
getchar();
return 0;
}