Hi,

I am trying to use the "partition" from STL algorithms. I get the following error:

error: no matching function for call to ‘partition(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, <unresolved overloaded function type>)’
/usr/include/c++/4.2.1/bits/stl_algo.h:2098: note: candidates are: _ForwardIterator std::partition(_ForwardIterator, _ForwardIterator, _Predicate) [with _ForwardIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, _Predicate = bool (test::*)(int)]

I am attaching part of my code where I have used partition. I have included the following three libraries:

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;


bool test::mypred(int i) 
{
	if(flag_equiv[i] == 1)
	return true;
	else
	return false;
}

void test::function1
{
                for(int i=0; i<(int)temp_myvec1_col.size(); i++)	
		{
			if(temp_myvec1_col[i].size() > 1)
			{
				counter_temp_equiv = 0;
				flag_equiv[0] = 0;				
				for(int lk=1; lk<(int)temp_myvec1_col[i].size(); lk++)
				{
					flag_equiv[lk] = 0;
					std::map<int,int>::iterator fbg=equiv_pair.find(temp_myvec1_col[i][lk]);
					if(fbg != equiv_pair.end())
					{
						flag_equiv[lk] = 0;				
					}
					else
					{
						flag_equiv[lk] = 1;
					}
				}
				bound = partition(temp_myvec1_col[i].begin(), temp_myvec1_col[i].end(), mypred);
				
				if(bound != temp_myvec1_col[i].end() && bound != temp_myvec1_col[i].begin())
				counter_temp_equiv++;			

				if(counter_temp_equiv > 0)
				{
					for(it_equiv=temp_myvec1_col[i].begin(); it_equiv!=bound; ++it_equiv)
					{
						temp_equiv_vec_row.push_back(*it_equiv);
					}
					temp_equiv_vec_col.push_back(temp_equiv_vec_row);
					temp_equiv_vec_row.clear();

					for(it_equiv=bound; it_equiv!=temp_myvec1_col[i].end(); ++it_equiv)
					{
						temp_equiv_vec_row.push_back(*it_equiv);
					}
					temp_equiv_vec_col.push_back(temp_equiv_vec_row);
					temp_equiv_vec_row.clear();
				}

			}
		}
}

Thanks

Dani AI

Generated

The compiler error shows the real problem: test::mypred is a non‑static member function (type bool (test::*)(int)), but std::partition needs a callable that can be invoked as predicate(value_type). A pointer‑to‑member needs an object to be called, so it cannot be passed directly. ’s suggestion to use a predicate object is correct — below are practical alternatives and a few cautions.

C++03 functor (captures the flag vector by reference):

struct FlagPred {
    const std::vector<int>& flags;
    FlagPred(const std::vector<int>& f) : flags(f) {}
    bool operator()(int v) const {
        return v >= 0 && v < (int)flags.size() && flags[v] == 1;
    }
};

std::vector<int>::iterator bound =
    std::partition(temp_myvec1_col[i].begin(),
                   temp_myvec1_col[i].end(),
                   FlagPred(flag_equiv));

C++11 options (more concise):

// lambda (needs -std=c++11 or later)
auto bound = std::partition(temp.begin(), temp.end(),
                            [this](int v){ return flag_equiv[v] == 1; });

// or bind a member function
using std::placeholders::_1;
auto bound = std::partition(temp.begin(), temp.end(),
                            std::bind(&test::mypred, this, _1));

Troubleshooting notes and cautions:

  • partition calls the predicate with the element value, not an index; ensure the element can validly index flag_equiv (check bounds).
  • Prefer operator() const on functors so the predicate can be copied safely.
  • partition reorders elements in place and returns the iterator to the first element of the second group — that iterator is what the original code stores in bound.
  • If the predicate must depend on an element’s position rather than its value, partition the indices (or pair indices with values) instead.

This expands on ’s functor idea with alternatives and safety checks that prevent out‑of‑range access and resolve the compilation error.

Is test::mypred a static member function of your class test?

If not then this wont work, you can not pass a class member as a callback like that because the called code in std::partition has no object test on which to call the callback function.

To access the data in your function you need to write a predicate functor (an object that acts like a function) to allow access to your data.

Unfortunately since you have failed to post most of the declarations of all the symbols you use it is quite hard to give an example but assuming that temp_myvec1_col is declard something like vector<vector<int> > temp_myvec1_col; it would be something like

class myPredicate
{
public:
    myPredicate(test& aTest)
    : theTest(aTest)
    {
    }

    bool operator()(int i) 
    {
	if(theTest.flag_equiv[i] == 1)
	    return true;
	else
	    return false;
    }

private:
    test& theTest;
}

assuming that flag_equiv had public access or that myPredicate was a friend of test and you would call it something like

bound = partition(temp_myvec1_col[i].begin(), temp_myvec1_col[i].end(), myPredicate(*this));

myPredicate(*this) creates an instance of the class myPredicate with a reference to the current instance of test held within it. Since it implements operator() this instance of myPredicate can be called just like a function so when it is called operator() has access to the calling parameters and the data members of myPredicate allowing it access to the data in the instance of test.

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.