c++ - combine variables in a vector/matrix -
i regular matlab user new c++. appreciate if 1 me solve problem.
i have few variables , vectors. say
#include<iostream> #include<vector> int main(){ int a=1; int b=1; vector<int> v1(100,0); vector<int> v2(100,0); return 0; } i want combine variables (a,b,v1,v2) in 2x101 matrix (say m) first , second rows of m are
m[0] = {a,v1}; m[1] = {v2,b}; how define m , assign variables? appreciated.
if want ability insert in front or @ should use std::deque. following
deque<deque<int>> m; m.push_back(v1); m.push_front(a); m.push_back(v2); m[1].push_back(b); this create 2 dimensional array or matrix 2 vectors rows.
or create 2 dimensional vector , fill in elements manually
vector<vector<int>> m; m.resize(2); // reserve space efficiency reasons, prevents reallocation m[0].reserve(v1.size() + 1); m[0].push_back(a); (auto integer : v1) { m[0].push_back(integer); } m[1].reserve(v2.size() + 1); (auto integer : v2) { m[1].push_back(integer); } m[1].push_back(b);
Comments
Post a Comment