1

早い段階でマップを宣言しました:

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

今、マップの内容を表示しようとしています:

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

「'!=' トークンおよび '->' トークンの前にプライマリ式が必要です。ここで作成しているエラーを理解できないようです。何かアイデアはありますか?

4

4 に答える 4

4

iterコード内の型です。変数である必要があります。

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
于 2010-06-10T19:52:38.690 に答える
1

タイプの変数を宣言します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;
    }
}

楽しみのために:)、私が書いた次の関数を使用して、マップまたはマルチマップのコンテンツを、標準出力、ファイルストリームのいずれであっても、任意の標準ストリームにストリーミングできます。たとえば、coutやwcoutなどのすべてのタイプのストリームを処理します。

    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);
}
于 2010-06-10T19:53:10.250 に答える
1

typedef を削除します。そのステートメントで変数を宣言しているのではなく、型を定義してからそれに割り当てています。それがエラーです。

于 2010-06-10T19:52:16.040 に答える
-4

私はいつも(* iter).firstと(* iter).secondのようにマップイテレータにアクセスします。

于 2010-06-10T19:52:48.393 に答える