访问地图的一个“项目”

时间:2013-09-22 18:42:30

标签: c++ map

我想访问map<wstring,wstring>的一个“项目”。

但这不起作用:

for (unsigned int i=0;i<m_ValidCharacterTranslations.Content().size();i++)
{
    wstring wsFirst = &m_ValidCharacterTranslations.Content()[i]->first;
    wstring wsSecond = &m_ValidCharacterTranslations.Content().at(i)->second;
    //do something with these 2 wstrings
}

我在最后一行得到的错误是:

No binary operator accepts the right-handed operand of type 'unsigned int'.

我的课程声明如下:

clsTranslations m_ValidCharacterTranslations;

class clsTranslations : public CBaseStructure
{
private:
    map<wstring,wstring> m_content;
protected:
    virtual void ProcessTxtLine(string line);
public:
    map<wstring,wstring> &Content();
    void LoadTranslations(string file);
};

有人可以告诉我如何获得这些价值吗?

2 个答案:

答案 0 :(得分:2)

  

我想遍历地图并使用第一个和   地图的第二个wstring。

C ++ 11:

for (auto& kvpair : somemap) {
    cout << kvpair.first << " has value " << kvpair.second << std::endl;
}

预C ++ 11:

for (map<wstring,wstring>::iterator it = somemap.begin(); it != somemap.end(); it++) {
    cout << it->first << " has value " << it->second << std::endl;
}

答案 1 :(得分:0)

您可以访问第一个和第二个地址,而不是值。