Thread: Sorting a vector of structs....

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    4

    Sorting a vector of structs....

    When I try to compile the following code,
    insert
    Code:
    #include<iostream>
    #include<vector>
    #include<algorithm>
    
    using namespace std;
    class c
    {
    public:
    struct match
    {
      int index;
      int count;
    
    }n;
    
    bool func(const match& a,const match& b)
    {
      return a.count<b.count;
    }
    
    void compare()
    {
      vector<match> chk;
      for(int i=5;i>=0;i++)
        {
          n.index=i;
          n.count=i;
          chk.push_back(n);
        }
    
      sort(chk.begin(),chk.end(),func);
      for(int i=0;i<chk.size();i++)
        {
          cout<<chk[i].index<<"   "<<chk[i].count<<endl;
        }
    
      //  return 0;
    
    
    }
    };
    int main()
    {
      c obj;
      obj.compare();
      return 0;
    }
    I get the following error....

    sample.cpp: In member function ‘void c::compare()’:
    sample.cpp:31: error: no matching function for call to ‘sort(__gnu_cxx::__normal_iterator<c::match*, std::vector<c::match, std::allocator<c::match> > >, __gnu_cxx::__normal_iterator<c::match*, std::vector<c::match, std::allocator<c::match> > >, <unresolved overloaded function type>)’
    /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:2735: note: candidates are: void std::sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<c::match*, std::vector<c::match, std::allocator<c::match> > >, _Compare = bool (c::*)(const c::match&, const c::match&)]


    Aren't the iterators for a vector random access by default????Kindly help!!!!!

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    func is a member function of c and as such needs to be called with an instance of c.

    Code:
    C c;
    c.func(match_a, match_b);
    As such it cannot be used as a predicate.

    However, since it doesn't require any knowledge of a c, you might make it a static class function.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By the way, I suggest that you format your code properly, e.g.,
    Code:
    #include<iostream>
    #include<vector>
    #include<algorithm>
    
    using namespace std;
    class c
    {
    public:
        struct match
        {
            int index;
            int count;
        }n;
    
        bool func(const match& a,const match& b)
        {
            return a.count<b.count;
        }
    
        void compare()
        {
            vector<match> chk;
            for (int i=5;i>=0;i++)
            {
                n.index=i;
                n.count=i;
                chk.push_back(n);
            }
    
            sort(chk.begin(),chk.end(),func);
            for (int i=0;i<chk.size();i++)
            {
                cout<<chk[i].index<<"   "<<chk[i].count<<endl;
            }
    
            //  return 0;
        }
    };
    
    int main()
    {
        c obj;
        obj.compare();
        return 0;
    }
    This way it is easier to even see that func and compare are member functions of c.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I'd think it would be more useful to put the comparison operator inside match and call it operator less-than. No need for the last parameter to sort unless you want to override the less-than operator of that class.
    Code:
    #include<iostream>
    #include<vector>
    #include<algorithm>
    
    using namespace std;
    class c
    {
    public:
        struct match
        {
            int index;
            int count;
            bool operator < (const match& a, const match& b)
            {
                return a.count < b.count;
            }
        } n;
    
        void compare()
        {
            vector<match> chk;
            for (int i=5; i>=0; i++)
            {
                n.index = i;
                n.count = i;
                chk.push_back(n);
            }
    
            sort(chk.begin(), chk.end());
            for (int i=0; i<chk.size(); i++)
            {
                cout << chk[i].index << "   " << chk[i].count << endl;
            }
        }
    };
    
    int main()
    {
        c obj;
        obj.compare();
        return 0;
    }
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    4
    Well ... I put the bool function and struct declaration outside class and it worked....

    Anyway the above sample code is just a part of my program... in the original code there are three integer variables in struct (percent, totalpercent & score being the three variables). I've sorted them using three bool functions.... I'd like to know if there is a more object oriented way of doin the same(like iMalc's method).

    Also thank you for your replies....

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Nobody noticed this problem?

    Code:
    for(int i=5;i>=0;i++)
        {
          n.index=i;
          n.count=i;
          chk.push_back(n);
        }
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by brewbuck View Post
    Nobody noticed this problem?

    Code:
    for(int i=5;i>=0;i++)
        {
          n.index=i;
          n.count=i;
          chk.push_back(n);
        }
    Hmm... no I didn't.
    Oddly enough I haven't written any backwards loops recently.

  8. #8
    Registered User
    Join Date
    Jul 2009
    Posts
    4

    Wink

    Oh sorry ... I forgot to mention .. I corrected the mistake but dint quite bother about it till it successfully got compiled ....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-31-2009, 12:34 PM
  2. Sorting an Array of Structs not working
    By ashcan1979 in forum C++ Programming
    Replies: 9
    Last Post: 09-28-2006, 03:07 PM
  3. sorting containers of structs
    By bigSteve in forum C++ Programming
    Replies: 2
    Last Post: 01-06-2004, 02:50 PM
  4. sorting an array of structs
    By jo_jo2222 in forum C++ Programming
    Replies: 2
    Last Post: 04-08-2002, 11:52 PM
  5. sorting an array of structs
    By singletrackmind in forum C++ Programming
    Replies: 8
    Last Post: 11-13-2001, 03:33 AM