std::find_if

zhuyf87 2013-03-13 01:03:56
	
std::vector<Counter>::iterator iter;
for (iter = counterGroup_.begin(); iter != counterGroup_.end(); ++iter)
{
if ((_tcscmp(iter->location, location) == 0) &&
(_tcscmp(iter->materielNumber, materielNumber) == 0))
break;
}


这段代码是对vector对象counterGroup_的元素进行遍历查找,标准库有std::find_if也可以完成这个工作。请问find_if会更高效吗?(counterGroup里面的元素无序,find_if也是一样遍历查找吧)我有必要使用find_if吗?

工作太忙,没有太多时间深入学习c++,有点小问题就跑过来问大家了。多谢,哈哈。
...全文
338 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
sduxiaoxiang 2013-03-13
  • 打赏
  • 举报
回复
counterGroup_.end() 还是比较费时的一个操作的 在size比较大的情况下
肉鸡快跑 2013-03-13
  • 打赏
  • 举报
回复
引用 9 楼 CKnightx 的回复:
我觉得 STL的源码好难看懂啊。求高手支支招,怎么学习看这类源码。
http://en.cppreference.com/w/cpp/algorithm/find 同为刚入行的菜鸟 慢慢看呗
肉鸡快跑 2013-03-13
  • 打赏
  • 举报
回复
引用 6 楼 zhuyf87 的回复:
引用 2 楼 sha_jinhao 的回复: find_if 的具体效率 我倒是没细研究 不过应该比你那个for循环强 看stl 源码剖析! 嗯。等忙完这阵子,好好学习学习。
http://en.cppreference.com/w/cpp/algorithm/find
肉鸡快跑 2013-03-13
  • 打赏
  • 举报
回复
引用 3 楼 akirya 的回复:
你的代码中 counterGroup_.end()调用了多次 find_if的话只有一次。
+1
  • 打赏
  • 举报
回复
引用 5 楼 zhuyf87 的回复:
引用 3 楼 akirya 的回复:你的代码中 counterGroup_.end()调用了多次 find_if的话只有一次。 那我这样呢? ^_^ C/C++ code?12345678 std::vector<Counter>::iterator iter, iterEnd = counterGroup_.end();for (iter = co……
这样就没啥区别的。
意吟 2013-03-13
  • 打赏
  • 举报
回复
其实在我看来,这些在效率上其实都差不多, 只是find_if写出来相对比较简洁。。。 而且C++0x标准可以套用lambada 例如: find_if( xx.beigin(), xx.end(), [](){ .... });
zhuyf87 2013-03-13
  • 打赏
  • 举报
回复

struct Counter
{
	TCHAR location[50];
	TCHAR materielNumber[50];
	UINT scannedCount;
};

struct CounterFind : public std::binary_function<Counter, Counter, bool>  
{  
	bool operator () (const Counter &c1, const Counter &c2) const  
	{  
		return ((_tcscmp(c1.location, c2.location) == 0) && 
			(_tcscmp(c1.materielNumber, c2.materielNumber) == 0));  
	}  
};

...

Counter counter = { 0 };
_tcscpy(counter.location, location);
_tcscpy(counter.materielNumber, materielNumber);

std::vector<Counter>::iterator iter = std::find_if(
	counterGroup_.begin(), counterGroup_.end(), std::bind2nd(CounterFind(), counter));  
...
find_if,我用的有什么问题没?和我5楼的for循环比,性能会更好吗?谢谢大家。
  • 打赏
  • 举报
回复
我觉得 STL的源码好难看懂啊。求高手支支招,怎么学习看这类源码。
zhuyf87 2013-03-13
  • 打赏
  • 举报
回复

__find_if(_InputIterator __first, _InputIterator __last,
          _Predicate __pred, input_iterator_tag)
{
    while (__first != __last && !bool(__pred(*__first)))
        ++__first;
    return __first;
如果是这样 和 我5楼的for循环比,也不会提升什么性能吧? 我这就用下find_if。
意吟 2013-03-13
  • 打赏
  • 举报
回复

  /**
   *  @brief Find the first element in a sequence for which a
   *         predicate is true.
   *  @ingroup non_mutating_algorithms
   *  @param  first  An input iterator.
   *  @param  last   An input iterator.
   *  @param  pred   A predicate.
   *  @return   The first iterator @c i in the range @p [first,last)
   *  such that @p pred(*i) is true, or @p last if no such iterator exists.
  */
  template<typename _InputIterator, typename _Predicate>
    inline _InputIterator
    find_if(_InputIterator __first, _InputIterator __last,
	    _Predicate __pred)
    {
      // concept requirements
      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
      __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
	      typename iterator_traits<_InputIterator>::value_type>)
      __glibcxx_requires_valid_range(__first, __last);
      return std::__find_if(__first, __last, __pred,
			    std::__iterator_category(__first));
    }

  /// This is an overload used by find_if() for the Input Iterator case.
  template<typename _InputIterator, typename _Predicate>
    inline _InputIterator
    __find_if(_InputIterator __first, _InputIterator __last,
	      _Predicate __pred, input_iterator_tag)
    {
      while (__first != __last && !bool(__pred(*__first)))
	++__first;
      return __first;
    }
zhuyf87 2013-03-13
  • 打赏
  • 举报
回复
引用 2 楼 sha_jinhao 的回复:
find_if 的具体效率 我倒是没细研究 不过应该比你那个for循环强 看stl 源码剖析!
嗯。等忙完这阵子,好好学习学习。
zhuyf87 2013-03-13
  • 打赏
  • 举报
回复
引用 3 楼 akirya 的回复:
你的代码中 counterGroup_.end()调用了多次 find_if的话只有一次。
那我这样呢? ^_^
	
std::vector<Counter>::iterator iter, iterEnd = counterGroup_.end();
for (iter = counterGroup_.begin(); iter != iterEnd; ++iter)
{
	if ((_tcscmp(iter->location, location) == 0) && 
            (_tcscmp(iter->materielNumber, materielNumber) == 0))
	    break;
}
图灵狗 2013-03-13
  • 打赏
  • 举报
回复
++
引用 3 楼 akirya 的回复:
你的代码中 counterGroup_.end()调用了多次 find_if的话只有一次。
  • 打赏
  • 举报
回复
你的代码中 counterGroup_.end()调用了多次 find_if的话只有一次。
jimette 2013-03-13
  • 打赏
  • 举报
回复
find_if 的具体效率 我倒是没细研究 不过应该比你那个for循环强 看stl 源码剖析!
zhuyf87 2013-03-13
  • 打赏
  • 举报
回复
大家多关注我发的帖子啊,都是“人傻分多”的。

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧