注册 登录
编程论坛 C++教室

关于multimap问题

tfg0116 发布于 2008-10-25 21:34, 546 次点击
#include<iostream>
#include<vector>
#include<string>
#include<map>

using namespace std;

int main()
{
    multimap<string, string> authors;
    string author, work, searchItem;
    do
    {
    cout<<"Enter author:"<<endl;
    cin>>author;
    if(!cin)
        break;
    cout<<"Enter author's work"<<endl;
    while(cin>>work)
    authors.insert(make_pair(author, work));
    cin.clear();
    }while(cin);
    typedef multimap<string, string>::iterator itType;
    itType iter = authors.begin();
    if(iter == authors.end())
    {
        cout<<"empty multimap"<<endl;
        return 0;
    }
    string currAuthor, preAuthor;
    do
    {
        currAuthor = iter->first;
        if(preAuthor.empty() || currAuthor[0] != preAuthor[0])
            cout<<"Author Name Beginning with'"<<iter->first[0]<<"':"<<endl;
        cout<<currAuthor;
        pair<itType, itType> pos = authors.equal_range(iter->first);
        while(pos.first != pos.second)
        {
            cout<<", "<<pos.first->second;
            ++pos.first;
        }
        cout<<endl;
        iter = pos.second;
        preAuthor = currAuthor;
    }while(iter != authors.end());
    return 0;
}
加颜色的那行代码为什么可以这么写啊?iter是multimap<string, string>::iterator 类型,而pos.second是键currAuthor关联的最后一个实例的下一位置,这样赋值可以吗?
0 回复
1