tmighty
tmighty

Reputation: 11399

Accessing one "item" of a map

I wanted to access one "item" of my map<wstring,wstring>.

But this does not work:

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
}

The error I am getting in the last line is:

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

My class is declared like this:

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);
};

Can somebody tell me how to get these values?

Upvotes: 0

Views: 142

Answers (2)

Iuri Covalisin
Iuri Covalisin

Reputation: 645

You access first and second addresses, instead of values.

Upvotes: 0

us2012
us2012

Reputation: 16253

I would like to iterate through the map and use the first and the second wstring of the map.

C++11:

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

pre C++11:

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

Upvotes: 2

Related Questions