我正在使用Armadillo在C++中进行线性代数计算。如何计算C++中向量的重复条目
例如,有一个
vector a = (1,1,2,2,0,2,1,0)
我想返回一个矩阵
(0, 2) //means 0 shows 2 times in the vector
(1, 3) //1 shows 3 times
(2, 3) //2 shows 3 times
有什么功能可以实现这样的工作吗?
我正在使用Armadillo在C++中进行线性代数计算。如何计算C++中向量的重复条目
例如,有一个
vector a = (1,1,2,2,0,2,1,0)
我想返回一个矩阵
(0, 2) //means 0 shows 2 times in the vector
(1, 3) //1 shows 3 times
(2, 3) //2 shows 3 times
有什么功能可以实现这样的工作吗?
正如评论中所述,您可以使用std::map
来收集结果。然后,您可以根据需要转换为矩阵。你可以跳过地图步骤,如果它已经用你之前的行预先初始化,可以直接使用矩阵。
至于执行此操作的函数,您可以使用<algorithm>
中的std::for_each
以及lambda表达式,尽管当循环很好时它看起来过度。
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
using namespace std;
int main()
{
vector<int> v{1,1,2,2,0,2,1,0};
map<int,int> dup;
for_each(v.begin(), v.end(), [&dup](int val){ dup[val]++; });
for(auto p : dup) {
cout << p.first << ' ' << p.second << endl;
}
return 0;
}
这里的另一个解决方案中,仅使用Armadillo的函数,以及C++ 11编译器:
vec a = {1,1,2,2,0,2,1,0}; // vec holds elements of type 'double'
vec b = unique(a);
uvec c = hist(a,b); // uvec holds unsigned integers
mat X(b.n_rows, 2);
X.col(0) = b;
X.col(1) = conv_to<vec>::from(c);
X.print("X:");
说明:
@JohnBupit,但更好地使用'地图' – Lol4t0
@ Lol4t0的确。我没有通过阅读这个问题。 –
使用[unique()](http://arma.sourceforge.net/docs.html#unique)和[hist()](http://arma.sourceforge.net/docs)的组合很容易实现。 HTML#HIST)。请参阅下面我的详细答案。 – mtall