thus iterators predefined functions? I am confuse between function and predefined functions.

I tend to think Iterators are more of an idea than being tied to a single implementation. A predefined function is one that is implemented already.

Stl algorithm: std::random_shuffle( someVector.begin(), someVector.end() );

thus iterators predefined functions?

Iterators are a concept for range access with a design derived from pointers, where a concept is a set of requirements for accessing the range. For example, find() and for_each() can both be written with a minimal set of requirements (assumptions about the "pointer" type):

template <typename Iterator, typename T>
Iterator find(Iterator first, Iterator last, const T& value)
{
    while (first != last && *first != value)
        ++first;

    return first;
}
template <typename Iterator, class Action>
Action for_each(Iterator first, Iterator last, Action f)
{
    while (first != last)
        f(*first++);

    return f;
}

Notice that there's absolutely nothing that forces the Iterator template type to adhere to the concept, but the concept requirements for Iterator in both of these functions are easily seen:

  • Iterator must be copy-assignable (to pass as an argument by value)
  • Iterator must be equality comparable
  • Iterator must support indirection.
  • Iterator must support incrementing (pre and post).

Iterators are thus just a set of requirements sanctioned by the standard for maximum reuse among the standard algorithms and to make extending the standard algorithms with your own functions easier.

It's somewhat confusing because iterators aren't functions or types, or anything more than an idea, really. They don't take shape until you model the concept with a type that conforms to the requirements. You're not forced to model the concept exactly either, just the parts you know will be used.

I am confuse between function and predefined functions.

Let's make it easier then. There's no difference between functions and pre-defined functions. They're all still functions, no magic. :) I can write my own function that uses an iterator concept and the only difference between my function and one of the standard functions is that mine won't be immediately available on any standard compiler:

template <typename RandomAccessIterator>
void bubblesort(RandomAccessIterator first, RandomAccessIterator last)
{
    for (RandomAccessIterator x = first; x < last; ++x) {
        bool done = true;
        
        for (RandomAccessIterator y = last - 1; y > first; --y) {
            if (*y < *(y - 1)) {
                std::swap(*y, *(y - 1));
                done = false;
            }
        }
        
        if (done)
            break;
    }
}
commented: Nice +17

tnx problem solve

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.