const_iterator思考

本文介绍了C++标准模板库(STL)中const_iterator的使用方法,并通过实例展示了如何利用const_iterator进行只读访问,同时对比了普通迭代器与常量迭代器的区别。

之前在对于std内iterator的使用并没有太关心,一般是建立容器后,要进行遍历打印,查找之类的操作,但这种无需修改原来容器内的值的操作,比较好的策略是声明为const_iterator;

比如:

 

	vector<int> lVec;
	lVec.push_back(1);
	lVec.push_back(2);
	lVec.push_back(3);
	std::vector<int>::const_iterator lIter = lVec.begin();
	cout << *lIter << endl;
	lIter = lVec.begin()+1;
	cout << *lIter << endl;
	*lIter = 10;//Error will occur, *lIter shouldn't be changed.
	++lIter;


但这样打印是可以的:

 

 

	vector<int> lVec;
	lVec.push_back(1);
	lVec.push_back(2);
	lVec.push_back(3);
	for (vector<int>::const_iterator iter = lVec.begin();
		iter != lVec.end(); ++iter)
	{
		cout << *iter << endl;
	}


但最爽的是利用boost::each,估计被boost优化掉了,等有时间看下它的实现:

 

 

	vector<int> lVec;
	lVec.push_back(1);
	lVec.push_back(2);
	lVec.push_back(3);
	BOOST_FOREACH(int &aData,lVec)
	{
		cout << aData << endl;
	}

 

 

#include<iostream> #include<string>//stod #include<cmath>//fmod pow using namespace std; const long double pi = atan(1.0) * 4; template<typename T> class Stack; template<typename T> class Node { friend class Stack<T>; T m_data; Node* m_next = nullptr; public: Node(const T& val) :m_data(val) {} const T& data()const { return m_data; } T& val() { return m_data; } Node* next() { return m_next; } }; template<typename T> class Stack { Node<T>* m_top = nullptr; public: Stack() = default; Stack(const Stack&) = delete; Stack operator=(const Stack&) = delete; ~Stack(); void clear(); void push(const T& val); void pop(); bool empty()const { return m_top == nullptr; } const T& top()const { return m_top->m_data; } }; //进栈操作 template<typename T> void Stack<T> ::push(const T& val) { Node<T>* node = new Node<T>(val); node->m_next = m_top; m_top = node; } //出栈操作 template<typename T> void Stack<T>::pop() { if (empty())return; Node<T>* p = m_top; m_top = m_top->m_next; delete p; } //清空操作 template<typename T> void Stack<T>::clear() { Node<T>* p = nullptr; while (m_top != nullptr) { p = m_top; m_top = m_top->m_next; delete p; } } //析构-释放开辟的动态内存 template<typename T> Stack<T>::~Stack() { clear(); } class Calculator { private: Stack<double>m_num;//数字 Stack<double>m_num2;//括号数字 Stack<char>m_opr;//运算符 Stack<char>m_opr2;//括号运算符 int precedence(const char& s)const;//比较优先级 double readNum(string::const_iterator& it);//读取操作数 void calculate(); //void calculate2(); //void calculateFunction(const string& func); double calculateParentheses(string::const_iterator& it); bool isNum(string::const_iterator& c)const { return (*c >= '0' && *c <= '9') || *c == '.' || *c == 'π'; } bool isLetter(std::string::const_iterator& c) const { return (*c >= 'a' && *c <= 'z'); } bool isFunction(string::const_iterator& it) const { if (string(it, it + 3) == "sin" || string(it, it + 3) == "cos" || string(it, it + 3) == "tan") return true; if (string(it, it + 4)
最新发布
03-31
void ConvertMatToPointCloudThrust(cv::Mat& depth, pcl::PointCloud<pcl::PointXYZ>& cloud) { const float xInternal = 0.004916f * 4; const float yInternal = 0.005f * 4; const int width = depth.cols; const int height = depth.rows; // 1. 统一内存管理 thrust::device_vector<float> d_depth(height * width); thrust::device_vector<DevicePoint> d_cloud(height * width); thrust::device_vector<bool> d_validmask(height * width); thrust::copy_n(reinterpret_cast<float*>(depth.data), height * width, d_depth.begin()); // 2. 融合计算与压缩 auto zipped = thrust::make_zip_iterator( thrust::make_tuple(d_depth.begin(), thrust::make_counting_iterator(0))); thrust::transform( thrust::cuda::par, zipped, zipped + height * width, thrust::make_zip_iterator( thrust::make_tuple(d_cloud.begin(), d_validmask.begin())), [=] __device__(const auto & in) { const int idx = thrust::get<1>(in); const float z = thrust::get<0>(in); if (z > 800.0f) return thrust::make_tuple(DevicePoint(), false); const int x = idx % width; const int y = idx / width; return thrust::make_tuple( DevicePoint( (x - width / 2) * xInternal * z, (y - height / 2) * yInternal * z, z ), true ); }); // 3. 高效压缩 thrust::device_vector<DevicePoint> compressed_cloud(height * width); auto new_end = thrust::copy_if( thrust::cuda::par, d_cloud.begin(), d_cloud.end(), d_validmask.begin(), compressed_cloud.begin(), [](bool valid) { return valid; } ); // 4. 零拷贝传输 const int valid_count = thrust::distance(compressed_cloud.begin(), new_end); cloud.resize(valid_count); cudaMemcpy( cloud.points.data(), compressed_cloud.data().get(), valid_count * sizeof(pcl::PointXYZ), cudaMemcpyDevi
03-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值