0

I have 2000 vector<vector<bool>> and each vector<bool> contains 200 elements , I am going to sort this vector of vectors. Supposing that the elements in vector<bool> are a one binary number.

original data:

vector 1: 1,1,1
vector 2: 1,0,1
vector 3: 0,0,0
vector 4: 1,0,0

After sorting:

vector 3: 0,0,0
vector 4: 1,0,0
vector 2: 1,0,1
vector 1: 1,1,1

It is possible to use sort with a special predicate but surprisingly when I call sort without a predicate it seems to work anyway.

    vector<bool> A = {1, 1, 1};
    vector<bool> B = {1, 0, 1};
    vector<bool> C = {0, 0, 0};
    vector<bool> D = {1, 0, 0};

    vector < vector<bool> > v = {A,B,C,D};

    sort(v.begin(),v.end());

and the order is as "after sorting" above.

Why does it work without a special predicate?

CashCow
  • 30,981
  • 5
  • 61
  • 92
user1436187
  • 3,252
  • 3
  • 26
  • 59
  • 5
    What is your question exactly? – Thomas Russell Aug 29 '14 at 12:03
  • Would 0,1,0,0 compare before or after 0,0,1,0? What is your bit endianess? – eerorika Aug 29 '14 at 12:09
  • First 0,1,0,0 then 0,0,1,0. – user1436187 Aug 29 '14 at 12:10
  • This is not a duplicate, it is just a badly worded question. – CashCow Aug 29 '14 at 12:46
  • 1
    I have rephrased the question as to what I think the user is asking – CashCow Aug 29 '14 at 12:49
  • @CashCow It was certainly a duplicate in its original form. – juanchopanza Aug 29 '14 at 13:11
  • @juanchopanza "how to sort a vector of user defined type" is certainly a part of his question and the duplicate answers that. But "how to write a predicate to sort vectors of bool as if they were interpreted as a binary number" is also part of the original question (not with those exact words, but because the answer to the first part is "use std::sort and a predicate") and that is not answered in the duplicate. – eerorika Aug 29 '14 at 13:14
  • The duplicate shows that you can use std::sort and a predicate. It would be a simple step to figure out why and when the predicate is needed, and what the default behaviour does. I must have forgotten for a moment that we're in the business of spoon-feeding answers to people. – juanchopanza Aug 29 '14 at 13:18

3 Answers3

5

It is enough simply to apply standard algorithm std::sort declared in header <algorithm> because there is defined operator < for vectors provided that all boolean vectors have the same size.

Here is an example

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

int main() 
{
    std::vector<std::vector<bool>> v =
    {
        { 1, 1, 1 }, { 1, 0, 1 }, {0, 0, 0 }, { 1, 0, 0 }
    };

    std::sort( v.begin(), v.end() );

    for ( const std::vector<bool> &v1 : v )
    {
        for ( bool b : v1 ) std::cout << b << ' ';
        std::cout << std::endl;
    }

    return 0;
}

The output is

0 0 0 
1 0 0 
1 0 1 
1 1 1 

Otherwise you could use algorithm std::accumulate in a predicate of std::sort

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
3

std::sort can take a Compare function. So just define a compare function on std::vector<bool> doing what you expect.

Hence you need to define a convention about how to compare two std::vector<bool> of different lengths.

BTW, it looks like you are re-inventing bignums, why don't you use some existing library like gmplib?

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

Use a simple bubble sorting algorithm on the elements of the outer vector (which are vectors as well), and the condition of swapping would be this:

for (int i=0; i<200; i++)
    if (vec1[i] > vec2[i]) vec1.swap(vec2);
    /* vec1 and vec2 are the two vectors you
    compare when bubble sorting the outer vector */

Because if you take two binary numbers, iterate through them, and compare each corresponding digit, then the greater of the two numbers is the one that has the highest digit that's a one and the corresponding digit in the other number is a 0. Like this:

0011010110101010000111011011

0010101011101000011101101010

Without converting them to decimal, it's easy to tell that the top one is larger. The first three digits are the same, but the fourth one is greater in the top number. The rest don't matter, this makes the top number bigger. Therefore, it's simple to use this as a swapping condition in bubble sorting.

notadam
  • 2,754
  • 2
  • 19
  • 35