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*.』
中文版的這樣寫:『一般說來,泛型演算法作用於「用來標示容器元素(或其他序列)區間」的一對iterators身上。這兩個iterators的型別必須完全相同,而且這兩個iterators必須標示出一個有效區間:它們必須指向同一個容器(或容器末端的下一位置),而且當兩個iterators不等時,重複累加第一個iterator必須可以到達第二個iterator。
某些演算法,例如find_first_of(),帶有兩對iterators。每一對的兩個iterators的型別必須完全相同,但兩對iterators彼此不必相同。更明確的說,被操作的所有元素可儲存不同種類的序列中,只要兩序列內的元素可進行比較就沒問題。
因此,上述程式的roster1和roster2,其型別無須相同,roster1可以是個list而roster2可以是個vector、deque或其他序列(本章稍後會出現這種情況)。唯一必須保證的是,兩序列內的元素必須可以使用==運算子比較是否相等。如果roster1是個list<string>,那麼roster2可以是個vector<char *>,因為標準庫為string和char *定義了一個==運算子。』
僅紀錄之。