Iterator Argument Types

C++ Primer 4/e在A First Look at the Algorithms這個地方有一個關鍵觀念:『In general, the generic algorithms operate on iterator pairs that denote a range of elements in a container (or other sequence). The types of the arguments that denote the range must match exactly, and the iterators themselves must denote a range: They must refer to elements in the same container (or to the element just past the end of that container), and if they are unequal, then it must be possible to reach the second iterator by repeatedly incrementing the first iterator.

Some algorithms, such as find_first_of, take two pairs of iterators. The iterator types in each pair must match exactly, but there is no requirement that the type of the two pairs match each other. In particular, the elements can be stored in different kinds of sequences. What is required is that we be able to compare elements from the two sequences.

In our program, the types of roster1 and roster2 need not match exactly: roster1 could be a list while roster2 was a vector, deque, or other sequence that we’ll learn about later in this chapter. What is required is that we be able to compare the elements from these two sequences using the == operator. If roster1 is a list<string>, then roster2 could be a vector<char*> because the string library defines == on a string and a char*.』



因此,上述程式的roster1和roster2,其型別無須相同,roster1可以是個list而roster2可以是個vector、deque或其他序列(本章稍後會出現這種情況)。唯一必須保證的是,兩序列內的元素必須可以使用==運算子比較是否相等。如果roster1是個list<string>,那麼roster2可以是個vector<char *>,因為標準庫為string和char *定義了一個==運算子。』