Re: STL question
 
In article <a1bbefdf-903f-4fae-83a5-1272298e0393
@q27g2000prf.googlegroups.com>, foolsmart2005@gmail.com says...
[ ... ]
// using vector<int>::iterator;
Here's (probably) the source of the problem: this wasn't originally 
commented out.
 
int main()
{
    vector<int> container;
    for(int i=1;i<=4;i++)
    {
            container.push_back(i);
    }
    cout << "Here is what is in the container:\n";
    iterator p;
The original intent (with the previous line still "executable") was that 
this refer to 'std::vector<int>::iterator'. With the previous line 
commented out, it refers to 'std::iterator', which doesn't work.
copied from the book <<Absolute C++>>, but I cannot compile, why?
I'd say "because you ended up with a lousy book. Even when/if you fix 
the code so it compiles, I'd consider it a long ways from exemplary 
code. If I was going to do what it does, I'd write it something like 
this:
#include <iostream>
#include <vector>
void show(std::vector<int> const &container) { 
    std::copy(container.begin(), container.end(),
        std::ostream_iterator<int>(std::cout, " "));
    std::cout << "\n";
}
int main()
{
    std::vector<int> container;
    for(int i=1;i<=4;i++)
        container.push_back(i);
    std::cout << "Here is what is in the container:\n";
    show(container);
    std::cout << "Setting entries to 0:\n";
    std::fill_n(container.begin(), container.size(), 0);
    std::cout <<"Container now contains:\n";
    show(container);
    return 0;
}
As a generalization, I'd say almost anytime you have something like:
some_type::iterator x;
for (x=container.begin(); x!=container.end(); ++x)
    do_something();
You've _probably_ made a mistake. You should probably be using an 
algorithm instead. In this case, he duplicated two algorithms already in 
the standard library: std::fill_n and std::copy. Far better to use than 
duplicate them. Another principle is often phrased as "don't repeat 
yourself" -- in the previous code, he had two repetitions of identical 
code to show the contents of the container. I've moved that into a 
function of its own.
-- 
    Later,
    Jerry.
The universe is a figment of its own imagination.