-1
1       1       5       7       5             
6       3       0       4       0              
6       9       0       4       0             
8       4       3       3       1             
8       2       8       8       0             
7       8       6       4       4              
7       6       4       4       0                   
9       4       2       4       0                  



 void something(vector < vector <int> > v)
{

    sort(v.begin(), v.end());
    int uniqueCount = std::unique(v.begin(), v.end()) - v.begin();
    count = uniqueCount-1;
}

I want to count the different number except 0. In this case is 1 3 4 5 = 4 unique numbers. I don't quite get the correct answer and it's attempting to delete a few lines of the matrix.

Alex
  • 45
  • 1
  • 6
  • 2
    [count_if](https://en.cppreference.com/w/cpp/algorithm/count) – user1810087 Dec 05 '18 at 08:16
  • 2
    Note that you have a vector of vectors here. So `N.begin()` is an iterator on a vector, not an number. So your `sort` pretty much trashes `N`. – Bathsheba Dec 05 '18 at 08:21
  • Can you use `std::set` ? – Jarod42 Dec 05 '18 at 08:22
  • 2
    Possible duplicate of [How to find out if an item is present in a std::vector?](https://stackoverflow.com/questions/571394/how-to-find-out-if-an-item-is-present-in-a-stdvector) – Alex Dec 06 '18 at 02:06

2 Answers2

5

I would suggest the following realisation using std::set, which stores each value once:

#include <set>
#include <vector>
#include <iostream>

using namespace std;

int count_object(vector<vector<int>> &matrix)
{
    std::set<int> s;
    for(auto &v : matrix)
        s.insert(v.begin(), v.end());
    s.erase(0);
    return s.size();
}

int main()
{
    vector<vector<int>> v = { { 1, 2, 3 }, { 3, 2, 1 }, { 0, 4, 1 } };
    std::cout << "Unique numbers: " << count_object(v);
}
snake_style
  • 1,139
  • 7
  • 16
  • @Alex, yours edits (wih std::unique) works only since c++20 and require std::sort. And, of course, you should preserve the goals of the post's owner. – snake_style Dec 06 '18 at 07:07
1

If for some reason you can't use a set, just to expand on the first comment about count_if

// flatten the 1D Vector
vector<int> all_ints;
for(auto& currv : N)
{
    all_ints.insert(all_ints.end(), currv.begin(), currv.end());
}

std::map<int,bool> unique_ints_map;
// use a lambda expression to count new elements
int unique_items = std::count_if(all_ints.begin(), all_ints.end(), 
                                [&unique_ints_map](int i)
                                {
                                    // Element exists already so shouldn't be added
                                    if( unique_ints_map.count(i) > 0 )
                                    {
                                        return false;
                                    }
                                    // Throw out zeros too since you don't want to count it
                                    if( i == 0)
                                    {
                                        return false;
                                    }
                                    // This is a new unique number, add it to our map so we don't count again
                                    unique_ints_map[i] = true;
                                    return true;
                                }
                                );
std::cout << "unique_items: " << unique_items << '\n';
Molly J
  • 519
  • 5
  • 15