64,654
社区成员
发帖
与我相关
我的任务
分享
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;
}
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循环比,性能会更好吗?谢谢大家。
__find_if(_InputIterator __first, _InputIterator __last,
_Predicate __pred, input_iterator_tag)
{
while (__first != __last && !bool(__pred(*__first)))
++__first;
return __first;
如果是这样 和 我5楼的for循环比,也不会提升什么性能吧?
我这就用下find_if。
/**
* @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;
}
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;
}