2021-03-20

Iterator Adapter

迭代器适配器,特殊类型的迭代器,其内部可能重新封装了iterator operator相关的成员函数,从外部来看,依旧保留了所有迭代器的特性,赋予了迭代器抽象更加强大的威力;

 	1.Insert Iterator   //安插型迭代器
 	2.Stream Iterator	//串流迭代器
 	3.Reverse Iterator	//逆向迭代器
 	4.Move Iterator		//搬移迭代器
Insert Iterator
# include <iostream>
# include <iterator>
# include <list>
# include <set>
# include <vector>
# include <algorithm>
int main()
{
	list<int> col1 = {1,2,3,4,5,6,7,8,9};
	vector<int> col2;
	// we need copy col1 to col2;
	// method 1;需要提前知道容器大小
	col2.resize(col1.size());
	copy(col1.begin(),col1.end(),col2.begin();
	// method 2;调用了容器.push_back()方法;
	copy(col1.begin(),col1.end(),back_inserter(col2));
	//method 3;调用了容器.insert(pos)方法;
	copy(col1.begin(),col1.end(),inserter(col12));
	//method4:调用了容器.push_front()方法
	copy(col1.rbegin(),col2.rend(),pop_inserter(col2));
}
insert iteratorback_inserterfront_inserterinserter
containervector,list,deque,stringdeque,list,forward_listall
stream iterator
用来读写stream,使得来自键盘的输入像是个集合,能够从中读取内容,同样道理,也可以把算法的输出结果重新导向到某个文件和屏幕(理解为容器)。
# include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <iterator>

using namespace std;

int main()
{
	vector<string> col1;
	//从标准输入读取字符到col1中,读取以空白分割的字符串(word by word)
	//istream_iterator<string>(),表示“串流结束符”(end of stream)
	copy(istream_iterator<string>(cin),istream_iterator<string>(),back_inserter(col1));
	
	sort(col1.begin(),col1.end());
	
	// 把col1输出到标准输出上(屏幕),不输出重复的字符串
	unique_copy(col1.begin(),col1.end(),ostream_iterator<string>(cout,'\n'));
}

三条语句就完成了从标准输入->vector<string>->排序->标准输出(ctrl+z结束输入);其内部实现大致揣测如下;

	template<class T, class Distance = ptrdiff_T>
	class istream_iterator{
		freind bool;
		operator == __STL_NULL_TMPL_ARGS(const istream_iterator<T,Distance>&x,
										const istream_iterator<T,Distance>&y);
		ptotected:
			istream * stream;
			T value;
			bool end_marker;
			void read(){
				end_marker = (*stream)?true:false;
				if(end_marker) 
					*stream >> value;
				end_marker = (*stream)? true:false;
				}
			public:
				typedef input_iterator_tag iterator_category;
				typedef T vaue_type;
				typedef Distance difference_type;
				typedef T* pointer;
				typedef T& reference ;
				istream_iterator():stream(&cin),end_marker(false){};
				istream_iterator(istream& s)stream(&s){read();}
				reference operator*() const { return value};
				isream_iterator<T, Distance>& operator++(){
					read();
					return *this;
					};
				istream_iterator<T,Distance> operator++(int) {
				istream_iterator<T,Distance> tmp = *this;
				read();
				return tmp;
				};
		}		

ostream_iterator<T>的实现如下(实现相对简单):

template<typename T,class Distance = ptrdiff_T>
class ostream_iterator
{ 
	public:
		typedef input_iterator_tag iterator_category;
		typedef T vaue_type;
		typedef Distance difference_type;
		typedef T* pointer;
		typedef T& reference ;
		ostream_iterator(ostream os,string str):_os(os),_str(str){}:
		ostream_iterator& operator*() { return *this;};
		void operator++ (){};  
		void operator= (T t){_os << t << str;}
	private:
		ostream _os;
		string _str;
}
Reverse Iterator

提供算法的逆向操作,将incresement 转变为decrement。所有提供双向迭代器的容器,都可以通过rbegin()和rend()产生一个反向迭代器,以及crbegin()和crend()以只读的方式产生反向迭代器。

# include <iterator>
# include <algorithm>
# include <vector>
# include <iosream>
using namespace std;
int main()
{
	vector<int> col1;
	for(int i = 0; i < 9; ++i) col1.push_back(i);
	copy(col1.rbegin(),col1.rend(),ostream_iterator<int>(cout,","));
	cout << endl;
}
// rbegin()内部重载了++操作;
Move Iterator

用来将任何“对底层元素”的访问转换为“move操作”,始自c++11;

2021-03-26 20:54:33,596 - Model - INFO - Epoch 1 (1/200): 2021-03-26 20:57:40,380 - Model - INFO - Train Instance Accuracy: 0.571037 2021-03-26 20:58:16,623 - Model - INFO - Test Instance Accuracy: 0.718528, Class Accuracy: 0.627357 2021-03-26 20:58:16,623 - Model - INFO - Best Instance Accuracy: 0.718528, Class Accuracy: 0.627357 2021-03-26 20:58:16,623 - Model - INFO - Save model... 2021-03-26 20:58:16,623 - Model - INFO - Saving at log/classification/pointnet2_msg_normals/checkpoints/best_model.pth 2021-03-26 20:58:16,698 - Model - INFO - Epoch 2 (2/200): 2021-03-26 21:01:26,685 - Model - INFO - Train Instance Accuracy: 0.727947 2021-03-26 21:02:03,642 - Model - INFO - Test Instance Accuracy: 0.790858, Class Accuracy: 0.702316 2021-03-26 21:02:03,642 - Model - INFO - Best Instance Accuracy: 0.790858, Class Accuracy: 0.702316 2021-03-26 21:02:03,642 - Model - INFO - Save model... 2021-03-26 21:02:03,643 - Model - INFO - Saving at log/classification/pointnet2_msg_normals/checkpoints/best_model.pth 2021-03-26 21:02:03,746 - Model - INFO - Epoch 3 (3/200): 2021-03-26 21:05:15,349 - Model - INFO - Train Instance Accuracy: 0.781606 2021-03-26 21:05:51,538 - Model - INFO - Test Instance Accuracy: 0.803641, Class Accuracy: 0.738575 2021-03-26 21:05:51,538 - Model - INFO - Best Instance Accuracy: 0.803641, Class Accuracy: 0.738575 2021-03-26 21:05:51,539 - Model - INFO - Save model... 2021-03-26 21:05:51,539 - Model - INFO - Saving at log/classification/pointnet2_msg_normals/checkpoints/best_model.pth 我有类似于这样的一段txt文件,请你帮我写一段代码来可视化这些训练结果
02-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值