如何重置迭代器,再次使用该迭代器访问数据
#include <iostream>
#include <iterator>
#include <vector>
#include <numeric>
using namespace std;
int main(int argc,char* argv[])
{
vector<int> numbers;
istream_iterator<int> numbersInput(cin),numbersEnd;
while (numbersInput!=numbersEnd)
{
numbers.push_back(*numbersInput++);
}
cout<<accumulate(numbers.begin(),numbers.end(),0)<<endl;
cin.ignore(1024,'\t');
cin.clear();
numbersInput=istream_iterator<int>(cin); //如何重置该迭代器,再次从cin中读取字符?
cout<<accumulate(numbersInput,numbersEnd,0)<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}相关推荐
C++迭代器简介:C++迭代器
C++迭代器 C++迭代器 C++迭代器 C++迭代器 C++迭代器
PHP迭代器和迭代的实现与使用方法分析
本文实例讲述了PHP迭代器和迭代的实现与使用方法。分享给大家供大家参考,具体如下: PHP的面向对象引擎提供了一个非常聪明的特性,就是,可以使用foreach()方法通过循环方式取出一个对象的所有属性,就像数组方式一样,代码如下: class Myclass{ public $a = 'php'; public $b = 'onethink'; public $c = 'thinkphp'; } $myclass = new Myclass(); //用foreach()将对象的属性循环出来 foreach($myclass as $key.'=>'.$val){ echo
深入了解c# 迭代器和列举器
大家好,这是 [C#.NET 拾遗补漏] 系列的第 07 篇文章。 在 C# 中,大多数方法都是通过 return 语句立即把程序的控制权交回给调用者,同时也会把方法内的本地资源释放掉。而包含 yield 语句的方法则允许在依次返回多个值给调用者的期间保留本地资源,等所有值都返回结束时再释放掉本来资源,这些返回的值形成一组序列被调用者使用。在 C# 中,这种包含 yield 语句的方法、属性或索引器就是迭代器。 迭代器中的 yield 语句分为两种: yeild return,把程序控制权交回调用者并保留本地状态,调用者拿到返回的值继续往后执行。 yeild break,用于告诉程序当
PHP迭代器的内部执行过程详解
复制代码 代码如下:class myIterator implements Iterator { private $position = 0; private $array = array( “first_element”, “second_element”, “last_element”, ); public function __construct() { $this->position = 0; } function rewind() { var_dump(__METHOD_
PHP迭代器和生成器用法实例分析
主要介绍了PHP迭代器和生成器用法,结合实例形式分析了PHP迭代器和生成器的相关概念、原理、应用技巧与使用注意事项,需要的朋友可以参考下


