0

Possible Duplicate:
Pass by Reference / Value in C++

I have the following function. The parameters is index (a ms map) and key (a string). It search for the key and return a set with result or the message "file not found".

How to pass the map index by reference?

If I need search_sucess in another function, Is it better return it by pointer?

 typedef map<string, set<string> > ms;

 set<string> seach_set(ms index, string key)
 {
     ms::iterator result;
     result = index.find(key);
     set<string> search_sucess;

     if (result != index.end())
     {
         cout << key << " in files : " << "{";
         for(set<string>::iterator iter = result->second.begin();
            iter != result->second.end(); ++iter)
          {
            cout << *iter << " ";
            search_sucess.insert(*iter);

        }
    cout << "}";
    }
   else
   {
     cout << key << " not found ";
   }
    cout <<  endl;

    return search_sucess;
 }
Community
  • 1
  • 1
user1871217
  • 35
  • 1
  • 6

1 Answers1

1

That code is insane. I believe it's functionally identical to the following (minus all the printing):

#include <map>
#include <set>
#include <string>

typedef std::map<std::string, std::set<std::string>> ms;

ms::mapped_type search_set(ms & m, ms::key_type const & k)
{
    ms::iterator it = m.find(k);

    return it == m.end() ? ms::mapped_type() : it->second;
}
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084