在向量/矩阵中组合变量
combine variables in a vector/matrix
我是MATLAB的普通用户,但对c++不熟悉。如果有人能帮助我解决问题,我将不胜感激。
我几乎没有变量和向量。
#include<iostream>
#include<vector>
int main(){
int a=1; int b=1;
vector<int> V1(100,0);
vector<int> V2(100,0);
return 0;
}
我想将所有变量(a,b,V1,V2)组合在一个 2x101 矩阵(比如 M)中,其中 M 的第一行和第二行是
M[0] = {a,V1};
M[1] = {V2,b};
如何定义 M 并分配变量?任何帮助,不胜感激。
如果您希望
能够在前面或后面插入,那么您应该使用std::deque
。所以你可以做到以下几点
deque<deque<int>> M;
M.push_back(V1);
M.push_front(a);
M.push_back(V2);
M[1].push_back(b);
这将创建一个二维数组或矩阵,其中两个向量作为行。
或者您可以创建一个二维向量并手动填充元素
vector<vector<int>> M;
M.resize(2);
// Reserve space for efficiency reasons, this prevents reallocation
M[0].reserve(V1.size() + 1);
M[0].push_back(a);
for (auto integer : V1) {
M[0].push_back(integer);
}
M[1].reserve(V2.size() + 1);
for (auto integer : V2) {
M[1].push_back(integer);
}
M[1].push_back(b);
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 更改 C++ 中的组合分类变量
- 高速解决方案以循环浏览不同变量的所有组合,并使用启动步骤停止方案(C )
- 计算句子的校验和(XOR)(由不同变量/类型的组合形成)
- 组合构造函数从成员变量中获取变量
- 在类组合中,使包含的类可以访问容器类变量
- 为循环嵌套的变量快速生成数字组合
- C++:组合私有全局变量和模板
- 将多个 int 变量组合成一个 int 变量
- 在向量/矩阵中组合变量
- 将一批变量"abcd..."分组为具有所有可能组合的两批。(递归)
- 如何组合三个变量以使用提升 asio 发送
- 组合预处理器宏和变量
- 将enum和string组合成一个类似map的变量,可以同时用于int和string
- 处理组合关系中的变量
- 如何在openMP中组合n线程的私有变量
- 关于extern和const组合的变量的问题
- 组合存储/加载连续的原子变量
- 如何在类的属性/变量中使用组合类作为向量类型
- 继承、组合和多成员变量的优点/缺点