关于vector的习题(三)

来源:互联网 发布:大数据魔镜官网 编辑:程序博客网 时间:2024/04/28 00:53

庭博网校原创 QQ14280784  86974558

大专以上学历,2500元一年;高中毕业,3000元一年;初中毕业:3500元一年。是学费哦!

/*
读入一段文本到vector对象,每个单词存储为vector中的一个元素。
把vector对象中每个单词转化为大写字母。输出vector对象中转化后
的元素,每八个单词为一行输出。
*/
#include "iostream"
#include "vector"
#include "string"
using namespace std;
int main()
{
 vector<string> text;
 string str;
 while(cin>>str)
  text.push_back(str);
 fflush(stdin);
 vector<string>::size_type x1;
 string::size_type x2;
 for(x1=0;x1!=text.size();x1++)
 {
  for(x2=0;x2!=text[x1].size();x2++)
      text[x1][x2]=toupper(text[x1][x2]);
 }
 for(x1=0;x1!=text.size();x1++)
 {
  if(x1%8==0 && x1!=0)
  {
   cout<<endl;
   cout<<text[x1];
  }
  else cout<<text[x1];
 }
 getchar();
 return 0;
}

//===============================================================================

以下是用迭代器去实现

/*
    读入一段文本到vector对象,每个单词存储为vector中的一个元素。
把vector对象中每个单词转化为大写字母。输出vector对象中转化后的
元素,每八个单词为一行输出。要求用迭代器实现
*/
#include "iostream"
#include "vector"
#include "string"
using namespace std;
int main()
{
 vector<string> a;
 string s1;
 while(cin>>s1)
  a.push_back(s1);
 fflush(stdin);
 vector<string>::iterator i;
 int x;
 for(x=0,i=a.begin();i<a.end();i++,x++)
 {
  if(x%8==0  && x!=0)
  {
   cout<<endl;
   cout<<*i<<" ";
  }
  else cout<<*i<<" ";
 }
 getchar();
 return 0;
}

 

 

原创粉丝点击