首页 > 其他分享 >迭代器的妙用

迭代器的妙用

时间:2023-02-01 07:44:05浏览次数:36  
标签:node 妙用 迭代 val int value using type

例如:实现数列求和

#include<bits/stdc++.h>
using namespace std;
struct node{
	int val;
	node& operator = (int v){
		val += v;
		return *this;
	}
};
struct iter{

	using value_type = node;
	using reference = value_type&;
	using pointer = value_type*;
	using iterator_category = output_iterator_tag;
	using difference_type = int;
	
	node* val;
	iter(node& pos) : val(&pos){}
	node& operator *(){
		return *val;
	}
	void operator ++(){}
};
int main(){
	node s = {0};
	iter it(s);
	cout << s.val << endl;
	vector<int> v = {1, 2, 4, 5};
	copy(v.begin(), v.end(), it);
	cout << s.val <<endl;
	return 0;
}

只要坚持良好码风,再也不用担心别人能看懂我的代码了。

标签:node,妙用,迭代,val,int,value,using,type
From: https://www.cnblogs.com/cdsidi/p/17081331.html

相关文章