views:

63

answers:

4
+1  Q: 

Displaying map stl

Declared a map early on:

map<char*,char*>    rtable; // used to store routing information

Now I'm attempting to display the contents of the map:

void Routes::viewroutes(){
    typedef map<char*, char*>::const_iterator iter;
    for (iter=rtable.begin(); iter != rtable.end(); ++iter) {
        cout << iter->second << " " << iter->first << endl;
    }
}

Receiving the error "expected primary-expression before '!=' token and for '->' token. Can't seem to understand the error I'm making here. Any ideas?

+1  A: 

Remove the typedef. You're not declaring a variable with that statement, you're defining a type and then assigning to it. That's what the error is.

bshields
What is the point of the typedef then? It appears in a sample code I have.
BSchlinker
The typedef can save you some typing but not where you have it. If you want to use it, define it where you declare your map. But give it a name that looks like a type, not a variable. Something like:typedef map<char*,char*>::const_iterator MapCIter;
bshields
+4  A: 

iter is a type in your code. Should be a variable.

typedef map<char*,char*> my_map_t;  // alias for a specialized map

// declare a variable of needed type
my_map_t    rtable;

// declare iter of type my_map_t::const_iterator
for (my_map_t::const_iterator iter=rtable.begin(); iter != rtable.end(); ++iter) {
    cout << iter->second << " " << iter->first << endl;
}
// scope of the iter variable will be limited to the loop above
Kirill V. Lyadvinsky
preserving the typedef is just a little convoluted, don't you think?
Ken Bloom
it is not very handy to write a full type of the iterator each time. typedef's make life easier.
Kirill V. Lyadvinsky
A: 

I always access my map iterators like so (*iter).first and (*iter).second.

Chris Kaminski
Why? Got something against the -> operator?
Fred Larson
Ouch, people really don't like my answer. :-) No, no, I don't have anything against operator->(). That's just how I learned it way back in the day, and I've just never changed. Creature of habit and all that.
Chris Kaminski
+1  A: 

Declare a variable of type iter:

void Routes::viewroutes(){
    typedef map<char*, char*>::const_iterator iter;
    for (iter i =rtable.begin(); i != rtable.end(); ++i) {
        cout << i->second << " " << i->first << endl;
    }
}

Just for fun :), you can use the following functions which I wrote to stream the contents of a map or a multimap to any standard stream whether it is the standard output, a file stream. It deals with all types of streams like cout or wcout for example:

    template <class Container, class Stream>
    Stream& printPairValueContainer(Stream& outputstream, const Container& container)
    {
        typename Container::const_iterator beg = container.begin();

        outputstream << "[";

        while(beg != container.end())
        {
            outputstream << " " << "<" << beg->first << " , " << beg->second << ">";
            beg++;
        }

        outputstream << " ]";

        return outputstream;
    }

template
    < class Key, class Value
    , template<class KeyType, class ValueType, class Traits = std::less<KeyType>,
    class Allocator = std::allocator<std::pair<const KeyType, ValueType> > > 
    class Container
    , class Stream
    >
Stream& operator<<(Stream& outputstream, const Container<Key, Value>& container)
{
    return printPairValueContainer(outputstream, container);
}
AraK