如何计算向量中的唯一编号而不删除它?

Ale*_*lex -1 c++ vector

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;
}
Run Code Online (Sandbox Code Playgroud)

我想计算除0以外的不同数字.在这种情况下,1 3 4 5 = 4个唯一数字.我没有得到正确的答案,它试图删除矩阵的几行.

sna*_*yle 5

我建议使用std :: set进行以下实现,它将每个值存储一次:

#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);
}
Run Code Online (Sandbox Code Playgroud)