Hi,
In below function i am not able to figure out why predicate function cmp_str can't be used with for_each algoritham while using this algoritham with map.

    #include <iostream>
    #include <map>
    #include <utility>

    using namespace std;
    struct cmp_str
    {
       bool operator()(string a, string b)
       {
          return a.compare(b)<0;
       }
    };

    int main()
    {
       map<string, int > Employees;

       // Examples of assigning Map container contents

       // 1) Assignment using array index notation
       Employees["Mike C."] = 5234;
       Employees["Charlie M."] = 3374;

       // 2) Assignment using member function insert() and STL pair
       Employees.insert(std::pair<string ,int>("David D.",1923));

       // 3) Assignment using member function insert() and "value_type()"
       Employees.insert(map<string,int>::value_type("John A.",7582));

       // 4) Assignment using member function insert() and "make_pair()"
       Employees.insert(std::make_pair("Peter Q.",5328));

       cout << "Map size: " << Employees.size() << endl;
    for_each(Employees.begin(); Empployees.end(); cmp_str);

    std::cout << '\n';
           //cout << (*ii).first << ": " << (*ii).second << endl;
    return 1;
    }

Recommended Answers

All 7 Replies

Well, your last for_each() statement doesn't do anything. Neither does your last std::cout << '\n'; other than output a newline just before you terminate the program. Why not use std::endl instead? Also, what is the output of the statement before the loop where you display the size of the map?

Finally, what exactly is your problem?

Biggest problem I can see, is that you are trying to pass a binary function to the for_each function that is expecting a unary function.

That's besides the syntax error(semi-colons instead of commas), typing error(2 p's in employee) and missing reference(#include <algorithm>). Also when using an object like that you need and instance:

 struct cmp_str
{
       void operator()(pair<string ,int>)
       {
       //not sure what you want to do, but any return value is ignored.
              return;
       }
}compstr;

for_each(Employees.begin(), Employees.end(), compstr);

Thanks for your answer. I want to print the all the keys in sorted (ascending order) manner through for_each with help of compstr function. apart from this is it possible to print the values as well in sorted manner of associative container. In current implementation i am copying both key and value separately in vector and performing the sort function.

I think you may be over thinking this a bit. A map is already sorted by its keys. Basically all you need to do is print it out. You can still use the for_each if you want:

    struct prt_pairs
    {
       void operator()(pair<string ,int> pr)
       {
          cout << "Index - " << setw(15) << left << pr.first << "\tValue - " << setw(10) << left << pr.second << '\n';
          return;
       }
    }printPairs;

    for_each(Employees.begin(), Employees.end(), printPairs);

Or you might find it easier to use the range based for loop:

     for(auto pr:Employees)
    {
        cout << "Index - " << setw(15) << left << pr.first << "\tValue - " << setw(10) << left << pr.second << '\n';
    }

If a different sorting criteria is necessary you may need to look at unordered_map instead

but it doesn't seems to be possible if we sort the values instead of keeping them in vector.
not sure it is possible because i need both (key and value) pair in sorted manner. key will be by default sorted but values not :(

Are you saying that sometimes you need the sorting by key and sometimes you need the sorting by value? A map only allows sorting by key. Perhaps a vector of pair<string, int> and using a custom comparator with sort will work.

Yes, currently i am implementing same way. so was just curious to see if same can be achieved without using vector. but anyway thanks to suggesting above method as unknowingly i was performing sorting on key by using vector.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.