迭代器除了在STL中遍历序列对象外,还有其他更多的迭代器被iterator所定义。iterator头文件定义迭代器的几个模板将数据从源传到目的地。流迭代器(stream iterator)作为指向输入或输出流的指针,它们可以用来在流和任何使用迭代器的源或目的地之间传输数据,如算法。插入迭代器(inserter iterator)可以将数据传输给一个基本的序列容器。Iterator头文件定义了两个流迭代器模板,其中istream_iterator
输入流迭代器
创建输入流迭代器:
istream_iterator<int> input(cin);
此代码创建了一个istream_iterator
istream_iterator<int> inputEnd;
现在有了一对迭代器,定义cin中一个int类型的值的序列,可以用这些迭代器将cin中的值加载到vector
vector<int> numbers;
cout<<”input numbers , end by zero”<<endl;
istream_iterator<int> input(cin),inputEnd;
while(input != inputEnd)
{
numbers.push_back(*input++);
}
可以看出,只要不是cin的end-of-stream,就会继续执行while输入数据,那么怎样才会产生end-of-stream的条件了,可以输入Ctrl+Z或输入一个无效的字符(如字母),就会产生end-of-stream条件。
当然,不只限于只能使用输入流迭代器作为循环的控制变量。它们可以向算法传递数据,如numeric头文件中的accumulate()。
cout<<”input numbers separated by spaces and a letter to end”<<endl;
istream_iterator<int> input(cin),inputEnd;
cout<<”the sum of you inputed numbers is “
<<accumulate(input,inputEnd,0)<<endl;
sstream头文件定义basic_istringstream
string data[] = {“1.2 12.6 3.6 98 5.3 7.1”};
Istringstream input(data);
Istream_iterator<double> begin(input),end;
cout<<”the sum of you inputed numbers is “
<<accumulate(begin,end,0.0)<<endl;
由于从string对象data中创建istringstream对象,因此可以像流一样从data中读取数据。此处accumulate()的第三个参数应为0.0,因为第三个参数确定结果的类型,此处保证返回double类型的值。
实例:
#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <iterator>
using std::cout;
using std::cin;
using std::endl;
using std::string;
int main()
{
std::map<string, int> words; // Map to store words and word counts
cout << "Enter some text and press Enter followed by Ctrl+Z then Enter to end:"
<< endl << endl;
std::istream_iterator<string> begin(cin); // Stream iterator
std::istream_iterator<string> end; // End stream iterator
while(begin != end ) // Iterate over words in the stream
words[*begin++]++; // Increment and store a word count
// Output the words and their counts
cout << endl << "Here are the word counts for the text you entered:" << endl;
for(auto iter = words.begin() ; iter != words.end() ; ++iter)
cout << std::setw(5) << iter->second << " " << iter->first << endl;
return 0;
}
插入迭代器
插入迭代器(inserter iterator)是一个可以访问序列容器vector
Back_insert_iterator
Front_insert_iterator
Insert_iterator
前两个插入迭代器类型的构造函数接受一个指定要在其中插入元素的容器的实参。如:
list<int> numbers;
front_insert_iterator<list<int> > iter(numbers);
向容器中插入值:
*iter = 99;
也可以将front_inserter()函数用于numbers容器:
front_inserter(numbers) = 99;
这几行代码为numbers列表创建了一个前段插入器,并用它在开头插入99。front_inserter()函数的实参是运用迭代器的容器。
insert_iterator
insert_iterator<vector<int> > iter(numbers,numbers.begin());
该构造函数的第二个实参是一个指定在何处插入数据的迭代器。向此容器赋值:
for (int i = 0; i < 100; i++)
*iter = i + 1;
代码执行后,前100个元素的值依次为100,99,…,1。
输出流迭代器
为了补充输入流迭代器模板,ostream_iterator
ostream_iterator<int> out(cout);
该模板的实参int指定要处理的数据类型,构造函数实参cout指定将作为数据的目的地的流,以便cout迭代器能将int类型的值写到标准输出流中。如:
int data [] = {1,2,3,4,5,6,7,8,9};
vector<int> numbers(data,data+9);
copy(numbers.begin(),numbers.end(),out);
在algorithm头文件中定义的copy()算法将由前两个迭代器实参指定的对象序列复制到第三个实参指定的输出迭代器。此代码执行结果为:123456789.
但现在写到标准输出流中的值没有空格。第二个输出流迭代器构造函数能解决这一点:
ostream_iterator<int> out(cout,” ”);
现在将输出1 2 3 4 5 6 7 8 9
实例:
#include <iostream>
#include <numeric>
#include <vector>
#include <iterator>
using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::istream_iterator;
using std::ostream_iterator;
using std::back_inserter;
using std::accumulate;
int main()
{
vector<int> numbers;
cout << "Enter a series of integers separated by spaces"
<< " followed by Ctrl+Z or a letter:" << endl;
istream_iterator<int> input(cin), input_end;
ostream_iterator<int> out(cout, " ");
copy(input, input_end, back_inserter<vector<int>>(numbers));
cout << "You entered the following values:" << endl;
copy(numbers.begin(), numbers.end(), out);
cout << endl << "The sum of these values is "
<< accumulate(numbers.begin(), numbers.end(), 0) << endl;
return 0;
}
原文链接: https://www.cnblogs.com/L-hq815/archive/2012/08/28/2660714.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/61030
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!