Hi ,

I have a 2 D matrix which is a vector <vector> type. I wan to find out the unique rows. To use std::unique the rows have to be sorted. I tried using std::sort with a predicate. But sorting does not work . Could someone help me

typedef std::vector<float> Vec_float;
typedef std::vector<Vec_float> matrix_float;



//this function is a predicate for sorting the matrix  and then eliminating duplicates
bool sort_pred (Vec_float i , Vec_float j)
{
    bool val=true;
    for(int it=0;it< i.size();++it)
    {
        if (i[it]!=j[it])
        {
            val=false;
            return(val);
        }
    }
    return val;
}
//this is the actual sorting function 

void vec_vec_sort (matrix_float& ext_met)
{
    std::sort(ext_met.begin(),ext_met.end(),sort_pred);
    //unique function removes consecutive duplicates in a range
    //the return value of unique is
    //A forward iterator pointing to the new end of the sequence, which now does not include the elements that were consecutive duplicates.

    matrix_float::iterator it;
    it=std::unique(ext_met.begin(),ext_met.end());
    //removing unwanted duplicate elements
    ext_met.erase(it,ext_met.end());
}

The header <vector> already defines

template< typename T, typename A >
bool operator< ( const std::vector<T,A>& , const std::vector<T,A>& ) ;
//performs a lexicographical comparison of the sequences (uses std::less<T>)

and

template< typename T, typename A >
bool operator== ( const std::vector<T,A>& , const std::vector<T,A>& ) ;

So this would suffice. Subject to comparing two float values f1 and f2 by f1<f2 or f1==f2 being ok for your program.

typedef std::vector<float> Vec_float ;
typedef std::vector<Vec_float> matrix_float ;

void make_unique( matrix_float& m )
{
    std::sort( m.begin(), m.end() ) ;
    m.erase( std::unique( m.begin(), m.end() ), m.end() ) ;
}
Comments
Simple and efficient solution
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <ctime>
#include <cstdlib>
#include <mmsystem.h>
#include <vector>
#include <conio.h>
#include <functional>
#include <stdio.h>
using namespace std;
char option;
int answer;
string theQues;
string theAns;
string ans;
void Start();
void Level1();
void Level2();
//oid Level3();


int point=4;


int main()
{



// system("color CE");
cout<<"\t**   **  ****  ****** **  **      ****  ****   ***"<<endl;
cout<<"\t*** *** ****** ****** **  **      ** ** ** ** *****"<<endl;
cout<<"\t** * ** **  **   **   ******      ***** ***** ** ** "<<endl;
cout<<"\t** * ** ******   **   ******      ****  ****  ** **"<<endl;
cout<<"\t**   ** ******   **   **  **      **    ** ** ** **"<<endl;
cout<<"\t**   ** **  **   **   **  **      **    ** ** *****"<<endl;
cout<<"\t**   ** **  **   **   **  **      **    ** **  ***"<<endl;



cout<<"\n\n"<<endl;
cout << "\t.........HELLO WELCOME TO BE A MATH PRO!!!!!................. " << endl;
cout << "\t.........THIS GAME CONSIST OF THREE(3) LEVELS................"<< endl;
cout << "\t.........FOR YOU GET PAST THE FIRST LEVEL YOU NEED .......... "<< endl;
cout << "\t.........TO ACHIEVE A SCORE OF OVER 75% !!!.................. \n\n\n\n\n"<< endl;


Start();
return 0;
}



void Start(){
// int Number;


cout<< "\tAre you ready to start(y/n): ";
cin>> option;
if(option=='y'||option=='Y'){


system("cls");


Level1();
}
else{
exit(0);
}


}



void Level1(){



float grade=0;
float score;
PlaySound("C:/Users/Orrett/Desktop/Games Programing/Math pro game/SoundTrack.wav",NULL,SND_FILENAME|SND_LOOP|SND_ASYNC);
enum  fields {QUESTION, ANSWER, NUM_FIELDS};
const int QUEST= 4;
vector<int> exist;


const string QUESTIONS[QUEST][NUM_FIELDS]=
{
{"21 x 4= _  ", "84"},
{"74.5 - 21.9=_ ", "52.6"},
{"72 / 6=_ ", "12"},
{"200 + 39.5=_ ", "239.5"}
};
for(int i=0; i<4; i++){
srand(time(0));
int choice = (rand() % QUEST);


theQues= QUESTIONS[choice][QUESTION]; // Questions to Answer
theAns = QUESTIONS[choice][ANSWER];



exist.push_back(choice);
cout<< "Please solve the Following Problem :\n"<<endl;
if(choice == exist[choice]){


}
cout<< theQues;
cin>> ans;


if(ans== theAns){
cout<<"Thats Correct!!!\n\n"<<endl;
grade++;
}
else{
cout<<"Thats Incorrect!!!\n\n"<<endl;
}


}
score= (grade/point)*100;


cout<<"Your score is : "<< score<<"%"<<endl;
cout<<"\n\n\n\n"<<endl;
if(score>= 75 ){
cout<<"Great Job you can move to the next level\n\n\n\n"<<endl;
Sleep(5000);
system("cls");
Level2();
}
else


cout<<"Sorry you cant move on"<<endl;
cout<<"Do you want to try again? (y/n) ";
cin>> option;


if(option=='y'|| option=='Y'){
fflush(stdin);
system("cls");
Level1();
}
else
{
system("cls");
cout<<"Thanks for trying better luck next time!!!!"<<endl;
exit(0);


}
}


void Level2(){
float grade=0;
float score;
cout<<"WELCOME TO LEVEL 2"<<endl;
enum  fields {QUESTION, ANSWER, NUM_FIELDS};
const int QUEST=4;
const string QUESTIONS[QUEST][NUM_FIELDS]=
{
{"x-11 = 36   x=  ", "47"},
{"x+5=15    x=  ", "10"},
{"25+ x= 52    x= ", "27"},
{"54-x= 24    x= ", "30"}
};
for(int i=0; i<4; i++){
srand(time(0));
int choice = (rand() % QUEST);
theQues= QUESTIONS[choice][QUESTION]; // Questions to Answer
theAns = QUESTIONS[choice][ANSWER]; // Matching Answers to the questions
cout<< "Please solve the Following Problem :\n"<<endl;
cout<< theQues;
cin>> ans;
if(ans== theAns){
cout<<"Thats Correct!!!\n\n"<<endl;
grade++;
}
else{
cout<<"Thats Incorrect!!!\n\n"<<endl;
}
}
score= (grade/point)*100;
cout<<"Your score is : "<< score<<"%"<<endl;
cout<<"\n\n\n\n"<<endl;
if(score>= 75){
cout<<"Great Good job you did well\n\n\n\n"<<endl;
Sleep(5000);
system("cls");
exit(0);
//Level2();
}
else
{
cout<<"Sorry you failed!!!!!"<<endl;
cout<<"Do you want to try again? (y/n) ";
cin>> option;


if(option=='y'|| option=='Y'){
system("cls");
Level1();
}
else
{
system("cls");
cout<<"Thanks for trying better luck next time!!!!"<<endl;
exit(0);
}
}
}

Edited 3 Years Ago by Nick Evan: Fixed formatting

Thank you very much . Yes it works. Looks like I complicated a simple one

This article has been dead for over six months. Start a new discussion instead.