std::adjacent_find

 原型:

#include <algorithm>
 forward_iterator adjacent_find( forward_iterator start, forward_iterator end );
 forward_iterator adjacent_find( forward_iterator start, forward_iterator end, BinPred pr );

函数adjacent_find()搜索start和end之间的相邻的两个相同元素. 如果二元谓词pr没有被指定, 则使用它检查两个元素是否相等. 如果被指定,则使用这个pr做operator来对start和end两个iterator指向的元素来做运算。返回值是一个迭代器, 它指向被发现的元素对的第一个元素. 如果没有匹配的元素, 则返回迭代器end.

看下面的例子:

#include <vector>
#include <iostream>
#include <algorithm>
#include <assert.h>

using namespace std;

class TwiceOver
{
public:
	bool operator () (int val1, int val2)
	{
		return val1 == (val2 / 2) ? true: false;
	}
};

int main(int argc, char* argv[] )
{
	int ia[] = { 1, 2, 4, 4, 8 };
	std::vector<int> vec( ia, ia+5);
	int *piter = NULL;
	int *piter2 = NULL;
	vector<int>::iterator iter;
	piter = adjacent_find(ia, ia + 5);
	assert(*piter == ia[2]);

	cout << "The two equal ints' value is:" << *piter << endl;
	iter = adjacent_find(vec.begin(), vec.end(), TwiceOver() );
	//piter2 = adjacent_find(ia,ia+4, TwiceOver() );
	//assert( *piter2 == ia[2] );
	assert( *iter = vec[1] );
	cout << "OK: adjacent_find() succeeded!" << endl;
	cout << "The values of the first occurence of TwiceOver is: "<< *iter << ". " << endl;

	system("pause");
	return 0;
}



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
template <typename PointT> void fromPCLPointCloud2 (const pcl::PCLPointCloud2& msg, pcl::PointCloud<PointT>& cloud, const MsgFieldMap& field_map) { // Copy info fields cloud.header = msg.header; cloud.width = msg.width; cloud.height = msg.height; cloud.is_dense = msg.is_dense == 1; // Copy point data cloud.resize (msg.width * msg.height); std::uint8_t* cloud_data = reinterpret_cast<std::uint8_t*>(&cloud[0]); // Check if we can copy adjacent points in a single memcpy. We can do so if there // is exactly one field to copy and it is the same size as the source and destination // point types. if (field_map.size() == 1 && field_map[0].serialized_offset == 0 && field_map[0].struct_offset == 0 && field_map[0].size == msg.point_step && field_map[0].size == sizeof(PointT)) { const auto cloud_row_step = (sizeof (PointT) * cloud.width); const std::uint8_t* msg_data = &msg.data[0]; // Should usually be able to copy all rows at once if (msg.row_step == cloud_row_step) { memcpy (cloud_data, msg_data, msg.data.size ()); } else { for (uindex_t i = 0; i < msg.height; ++i, cloud_data += cloud_row_step, msg_data += msg.row_step) memcpy (cloud_data, msg_data, cloud_row_step); } } else { // If not, memcpy each group of contiguous fields separately for (uindex_t row = 0; row < msg.height; ++row) { const std::uint8_t* row_data = &msg.data[row * msg.row_step]; for (uindex_t col = 0; col < msg.width; ++col) { const std::uint8_t* msg_data = row_data + col * msg.point_step; for (const detail::FieldMapping& mapping : field_map) { memcpy (cloud_data + mapping.struct_offset, msg_data + mapping.serialized_offset, mapping.size); } cloud_data += sizeof (PointT); } } } }
05-16

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值