我如何将一组整数集变成C 中的3D矢量
How can I turn a set of set of integers into 3D vectors in C++?
我在data[12][51]
阵列中具有一个数据矩阵,该矩阵是12 x 51矩阵。
如果我有 set<set<int>> rows
,例如
{(1,2,5),(3,7,8),(9,11)}
将表示数据矩阵的行索引。
我想形成一组行索引的矩阵,因此(1,2,5(的元素将是由第1,第二和第5行组成的3 x 51矩阵。
vector<double> features[12];
for (int i = 0; i < 12; i++){
for (int j = 0; j < 51; j++){
features[i].push_back(data[i][j]);
}
}
因此,在上面的部分中,features[i]
将返回数据矩阵的整个i
'。并给定任意尺寸的set<set<int>> rows
,
vector<vector<vector<double>>> sets;
for (auto & it : rows){
vector<vector<double>> temp;
for (int i : it){
temp.push_back(features[i]);
}
sets.push_back(temp);
}
但是,当我尝试打印矩阵的这种向量时,
for (int i = 0; i < sets.size(); i++){
for (int j = 0; j < sets[0].size(); j++){
for (int z = 0; z < sets[0][0].size(); z++){
cout << sets[i][j][z] << " , ";
}
cout << endl;
}
cout << " === Matrix === " << endl;
}
可执行的冻结,只是停止。
因此,如果以上方法不应该工作,那么我如何将一组整数变成3D向量?
在一个人的工具箱中动态尺寸的矩阵总是很高兴的可以随时将其弹出。我敢肯定那里有成千上万的地方。
根据需要添加浇头,并将全面的文件保持在安全的位置。
您可以添加的东西:获取subsatrix,加法等
#include <vector>
#include <algorithm>
template <typename T>
class Matrix
{
private:
typedef std::vector<std::vector<T>> _Mat;
_Mat matrix_;
public:
Matrix(int rows, int cols)
{
matrix_.resize(rows);
for (auto& v : matrix_)
{
v.resize(cols);
}
}
Matrix(int rows, int cols, const T* data)
{
matrix_.resize(rows);
for (auto& v : matrix_)
{
v = std::vector<T>(data, data + cols);
data += cols;
}
}
size_t rowCount() const
{
return matrix_.size();
}
size_t colCount() const
{
return (rowCount()) ? matrix_[0].size() : 0;
}
const std::vector<T>& getRow(int n) const
{
return matrix_.at(n);
}
void setRow(int row, const std::vector<T>& v)
{
if (row >= rowCount() || v.size() != colCount())
{
throw std::exception(); // choose your favorite exception class
}
matrix_.at(row) = v;
}
std::vector<T> getCol(int col)
{
std::vector<T> result;
std::for_each(matrix_.begin(), matrix_.end(),
[&](std::vector<T>& r)
{
result.push_back(r.at(col));
});
return result;
}
void setCol(size_t col, const std::vector<T>& v)
{
if (col >= colCount() || v.size() != rowCount())
{
throw std::exception(); // choose your favorite exception class
}
for (size_t i = 0; i < matrix_.size(); ++i)
{
matrix_[i][col] = v[i];
}
}
std::vector<T>& operator[](size_t row)
{
return matrix_.at(row);
}
const std::vector<T>& operator[](size_t row) const
{
return matrix_.at(row);
}
};
int main()
{
Matrix<double> a(12, 51);
Matrix<double> b(3, 51);
Matrix<double> c(12, 2);
b.setRow(0, a.getRow(1));
b.setRow(1, a.getRow(25));
b.setRow(2, a.getRow(12));
Matrix<double> c(12, 2);
c.setCol(0, a.getRow(3));
c.setCol(1, a.getCol(4)); // << throws!!
std::vector<Matrix<double>> matrices;
matrices.push_back(a);
matrices.push_back(b);
matrices.push_back(c);
Matrix<std::vector<double>> matrixOfVectors;
return 0;
}
为什么不使用这样的样式:
int nMatrix[rowSize][volumnSize][deepSize] = { 0 };
实际上,我们通常使用数组操作3D数据
相关文章:
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 可以打印矢量和矢量中的矢量的非重载C++函数
- 带有 CKKS 的 Microsoft 密封中的矢量点积
- 如何从使用 for 循环中的矢量大小的矢量中删除元素
- 导致Segmentation错误的结构中的矢量
- 从 opencv c++ 中的矢量中检索固定的帧数
- C++ - std::构造函数中的矢量初始化
- C++ 删除存在于另一个矢量中的矢量项,同时保留顺序
- 为什么 C++ 地址中的矢量无法通过原始指针访问
- C++ - 与 Numpy 中的矢量版本相比,Argsort 效率低的矢量版本实现
- C++ 在一个源文件中使用另一个源文件中的矢量定义
- <string> 结构中的矢量无法正常工作
- 从特征求解器中的矢量中检索值
- C++ 中的矢量重复输出输入
- 断言失败错误,C++中的矢量下标超出范围问题
- 您的类型在C++中的矢量
- 同一对象中的矢量插入
- 结构中的矢量指针无法转换主要中的矢量有什么问题?
- 循环中的矢量到数组不起作用
- 为什么 C++ 中的矢量不会自动调整大小