简体   繁体   English

C ++中模板函数计数的语法

[英]Syntax of template function count in c++

I was following and example about function template Count and I am missing something. 我正在关注关于函数模板Count的示例,但我缺少了一些东西。 Surely I did not understand in full iterator_traits and i am confused: 当然,我不完全了解iterator_traits并感到困惑:

Template Function COUNT has been declared as: 模板功能COUNT已声明为:

template <class InputIterator, class T>
  typename iterator_traits<InputIterator>::difference_type  
   count (InputIterator first, InputIterator last, const T& val);

My questions : 我的问题:

1) why using iterator_traits::difference_type ? 1)为什么使用iterator_traits :: difference_type? The function returns an object why don't use a simple : 该函数返回一个为什么不使用简单对象的对象:

template <class InputIterator, class T>
 T count(InputIterator first, InputIterator last, const T& val); 

I have tried and worked. 我已经尝试和工作了。

2) in case i MUST use iterator_traits, why should I use a "different_type" (subtracting one iterator from another) and not "value_type" (the value the iterator can point to) ? 2)如果我必须使用iterator_traits,为什么我应该使用“ different_type”(从另一个迭代器中减去一个迭代器)而不是“ value_type”(迭代器可以指向的值)?

i have tried and worked even in this case. 即使在这种情况下,我也曾尝试过并工作过。

I supposed iterator_traits should be used when a function returns an Iterator but then i have seen the template function FIND and it confused me more. 我想当一个函数返回一个Iterator时应该使用iterator_traits,但是后来我看到了模板函数FIND,这让我更加困惑。

template <class InputIterator, class T>
 InputIterator find (InputIterator first, InputIterator last, const T& val);

whereas i would have coded like this: 而我本来会这样编码:

template <class InputIterator, class T>
 typename iterator_traits<InputIterator>::value_type 
  find( nputIterator first, InputIterator last, const T& val);

Please can someone clarify this points ? 请有人可以澄清这一点吗?

Thank you so much in advance for any help. 提前非常感谢您的帮助。

1) std::count is supposed to count things that are equal to something. 1) std::count应该用来计算等于某物的东西。 So if your container holds, for example, apples, it would not be useful to return an apple object, because an apple is not useful for counting apples (at least not the kind of apples I have in mind) 因此,例如,如果您的容器中装有苹果,则返回苹果对象将没有用,因为苹果对计算苹果(至少不是我所想到的那种苹果)没有用处

std::vector<Apple> apples = ...;
Apple an_apple;
auto n = std::count(apples.begin(), apples.end(), an_apple);

Here, the type of n must be something that allows you to count a potentially large number of things. 在这里, n的类型必须是允许您计算潜在大量事物的事物。 You are counting how many Apple objects are the same as an_apple . 您正在计算与an_apple相同的Apple对象an_apple

2) std::find has to give you back something that can be used as a handle to an element of a container and to check whether the element exists or not. 2) std::find必须给您一些可以用作容器元素句柄的东西, 检查该元素是否存在。 An iterator provides a good means to do exactly this. 迭代器提供了一种很好的方法来做到这一点。 It would make no sense to return a value because a) you already have a copy of what you are looking for and b) you cannot generally return a "null" value to indicate that an element has not been found. 返回值没有任何意义,因为a)您已经拥有所寻找内容的副本,并且b)通常无法返回“空”值来表示未找到元素。

std::count应该返回某个元素的计数 ,因此返回类型必须是整数类型,但是T不一定是整数类型,因为它可以是任何东西(它是元素的类型,准确)。

1) The value the iterator points to can be anything, and not necessarily a value that can represent a count. 1)迭代器指向的值可以是任何值,不一定是可以表示计数的值。 count returns a numeric type, and the numeric type of choice is the difference_type of the iterator. count返回一个数字类型,选择的数字类型是迭代器的difference_type。

2) Again, value_type is the type of the thing the iterator points to, and not necessarily a numeric value. 2)同样,value_type是迭代器指向的事物的类型,不一定是数字值。

Why would you want find() to return object you've just specified in find() function? 您为什么要find()返回刚在find()函数中指定的对象? What find does is looking for a place inside container where the object you're are looking is stored. find所做的是在容器内寻找要存储对象的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM