< Home
How to iterate over a STL map full of strings in C++

I have the following issue related to iterating over an associative array of strings defined using std::map.

-- snip --
class something 
{
//...
   private:
      std::map<std::string, std::string> table;
//...
}

In the constructor I populate table with pairs of string keys associated to string data. Somewhere else I have a method toString that returns a string object that contains all the keys and associated data contained in the table object(as key=data format).

std::string something::toString() 
{
        std::map<std::string, std::string>::iterator iter;
        std::string* strToReturn = new std::string("");

        for (iter = table.begin(); iter != table.end(); iter++) {
           strToReturn->append(iter->first());
           strToReturn->append('=');
           strToRetunr->append(iter->second());
           //....
        }
       //...
}

When I'm trying to compile I get the following

error: "error: no match for call to ‘(std::basic_string, std::allocator >) ()’".

Could somebody explain to me what is missing, what I'm doing wrong? I only found some discussion about a similar issue in the case of hash_map where the user has to define a hashing function to be able to use hash_map with std::string objects. Could be something similar also in my case?

Thank you!

by crazybyte
on 6/30/2009 11:51:54 PM

iter->first and iter->second are variables, you are attempting to call them as methods.

by Nick Lewis on 6/30/2009 11:59:46 PM

Stackoverflow.mobi is not affiliated with stackoverflow.com but uses their public data dump to provide mobile access to stackoverflow.com's data