iterator(C++)

iterator(初步学习):
1).const_iterator可以得到一个迭代器,自身的值可以改变,但不能用它来改变其所指向的元素的值.可用于const vector或非const vector。由于不能改写元素,const迭代器基本上没什么用处
2).声明一个const迭代器时,必须初始化迭代器。一旦初始化,则不能被改变.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
 vector<int> ivec(10);
 //equivalent loop using iterators to reset all the elements in ivec 0
 for(vector<int>::iterator iter=ivec.begin();iter!=ivec.end();++iter)
  *iter=15;  //set element to which iter refers to 0
 for(vector<int>::size_type ix=0;ix!=ivec.size();++ix)
  cout<<ivec[ix]<<endl;

 vector<string> svec(10,"you");
 //use const_iterator because we won't change the elements
 for(vector<string>::const_iterator iter1=svec.begin();iter1!=svec.end();++iter1)
 { //*iter1='you';   //error:*iter is const
  cout<<*iter1<<endl;  //print each element in the text
 }

 vector<int> nums(10);  //nums is nonconst
 const vector<int>::iterator cit=nums.begin();
 *cit=1;   //cit can change its underlying element
 //++cit;   //erro:can't change the value of cit;
 cout<<*cit<<endl;

 const vector<int> nines(10,9);  //can't change elements in nines
 //error:cit2 could change the elements it refers to and nines is const
 //const vector<int>::iterator cit2=nines.begin();
 //ok:it can't change an element value,so it can be used with a const vector<int>
 vector<int>::const_iterator it=nines.begin();
 //*it=10; //error: *it is const
 ++it; //ok:it isn't const so we can change its value
 cout<<*it<<endl;
 return 0;
}
3).Can't add two pointers??
例如:#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
 //ok
 vector<int> ivec(10,1);
 vector<int>::iterator mid=ivec.begin()+ivec.size()/2;
 cout<<*mid<<endl;

 //error:can't add two pointers
 //vector<int> ivec1(10,2);
 //vector<int>::iterator mid1=(ivec.begin()+ivec.end())/2;
 //cout<<*mid1<<endl;

 return 0;
}

4).读一组整数到vector对象,计算并输出每对相邻元素的和(使用迭代器访问vector中的元素)
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
 cout<<"Please Enter Numbers(Ctrl+Z to end ): "<<endl;
 //read input
 vector<int> ivec;
 int value;
 while(cin>>value)
 {
  ivec.push_back(value);
 }

 if(ivec.size()==0)
 {
  cout<<"No Numbers?"
   <<endl;
  return -1;
 }
 
 //计算相邻元素的和并输出
 cout<<"Sum of each pair of adjacent elements in the vector:"
  <<endl;
 vector<int>::size_type cnt=0;
 for(vector<int>::iterator iter=ivec.begin();iter<ivec.end()-1;iter+=2)
 {
  cout<<*iter+*(iter+1)<<"/t";
  ++cnt;
  if(cnt%6==0)  //每行输出6个元素
   cout<<endl;
 }
 if(ivec.size()%2!=0)
 {
  cout<<"the mid of the elments in the vector isn't calculating"
   <<" "<<"its value is: "
   <<ivec.begin()+ivec.size()/2<<endl;
 }

 if(ivec.size()%2!=0)  //提示最后一个元素没有求和并输出 
  cout<<endl
   <<"The last elements is not been summed"
   <<"and its value is"
   <<*(ivec.end()-1)<<endl;

 return 0;
}

5).读一组整数到vector对象,计算首尾配对元素的和并输出(使用迭代器访问vector中的元素)
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
 vector<int> ivec;
 int ival;
 //读入数据到vector对象中
 cout<<"Please Enter numbers(Ctrl+Z to end): "<<endl;
 while(cin>>ival)
  ivec.push_back(ival);
 if(ivec.size()==0)
 {
  cout<<"No Elements?!"
   <<endl;
  return -1;
 }
 //计算首尾配对元素的和并输出
 cout<<"Sum of each pair of counterpart elements in the vector: "
  <<endl;
 vector<int>::size_type cnt=0;
 for(vector<int>::iterator first=ivec.begin(),last=ivec.end()-1;first<last;++first,--last)
 {
  cout<<*first+*last<<"/t";
  ++cnt;
  if(cnt%6==0) //每行输出6个和
   cout<<endl;
 }
 if(first==last)  //提示居中元素没有求和并输出
  cout<<endl
   <<"the center element is not been summed"
   <<"and its value is: "
   <<*first<<endl;
 
 return 0;
}
6).读入一段文本到vector对象,每个单词存储为vector中的一个元素,把vector对象中每个单词转换为大写字母,输出转换后的元素,每八个单词一行输出.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
using namespace std;

int main()
{
 vector<string> svec;
 string str;
 //读入文本到vector对象中
 cout<<"Please Enter Strings: "<<endl;
 while(cin>>str)
  svec.push_back(str);
 if(svec.size()==0)
 {
  cout<<"No strings?!"
   <<endl;
  return -1;
 }
 //将vector对象中单词转换为大写字母并输出
 cout<<"Transformed elements from the vector: "
  <<endl;
 vector<string>::size_type cnt=0;
 for(vector<string>::iterator iter=svec.begin();iter!=svec.end();++iter)
 {
  for(string::size_type index=0;index!=(*iter).size();++index)
  {
   if(islower((*iter)[index]))
    //单词中下标为index的字符为小写字母
    (*iter)[index]=toupper((*iter)[index]);
   
  }
  cout<<*iter<<" ";
  ++cnt;
  if(cnt%8==0)//每8个单词输一行
   cout<<endl;
 }

 return 0;
}
7).让用户输入一组整数到vector对象中,用迭代器把每个元素的值改为当前值的2倍,并输出
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
 vector<int> ivec;
 int ival;
 //让用户输入数到vector对象中去
 cout<<"Please Enter numbers(Ctrl+Z to end): "
  <<endl;
 while(cin>>ival)
  ivec.push_back(ival);
 if(ivec.size()==0)
 {
  cout<<"No Element?"
   <<endl;
  return -1;
 }

 //把每个元素的值改为当前值得的2倍
 for(vector<int>::iterator iter=ivec.begin();iter!=ivec.end();++iter)
 {
  (*iter)=(*iter)*2;
  cout<<*iter<<endl;
 }

 return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值