I am trying to create a 2-D vector that as column 0 it would have integers and column 1 would have vectors.
For example:
column[0] column[1]
2 {2}
5 {1,2,0}
12 {4,7,9,0,0,1,6,0,0}
3 {6}
column 0 would be user input while column 1 I will calculate it on my own - I don't mind if it has dummy vectors for now and after the user exits then it would print the 2-D vector. What I tried is:
int main() {
unsigned int input;
vector<vector<unsigned int>> v;
while (cin >> input) // enter non int to exit
for(int i=0; i<input;i++){
vector<int> factorial; //calc in a separate function - dummy vector for now
for (int j=0; j<factorial.size(); j++){
v[i].push_back(input); //set column 0 as input
v[i][j].push_back(factorial); //set column 1 as the vector containing the factorial of input
}
}
return 0;
}
class
:#include <iostream>
#include <vector>
using namespace std;
template<class T1, class T2>
class Columns{
public:
vector<T1> input;
vector<T2> output;
};
int main(){
Columns<int, int> columns; //Or, if you need to have vector in "output": \
Columns<int, vector<int>> columns;
//Usage:
columns.input.push_back(2);
columns.output.push_back(2);
return 0;
}
map
:#include <iostream>
#include <vector>
#include <map>
using namespace std;
int main(){
map<int, vector<int>> result;
for(int i = 0; i < 10; i++)
result[i] = factorial(i); //Where factorial() is function that returns vector
for(auto res: result){
cout << "Input: " << res.first << "; Result: {";
for(auto res2: res.second)
cout << res2 << "; ";
cout << "};" << endl;
}
}
So in "map
" case, output should be like this:
Input: 0; Result: {Numbers here};
...
Input: 9; Result: {Numbers here};
vectors
:Maybe, it's the best option for you now.
#include <iostream>
#include <vector>
using namespace std;
template<class T>
vector<T> FactorialNumbers(T input){
vector<T> result;
for((input < 0) ? return result : input = input; input != 0; input--)
result.push_back(input);
return result;
}
int main() {
vector<int> input;
vector<vector<int>> output;
int usrInput = 0;
while(cin >> usrInput){
input.push_back(usrInput);
output.push_back(FactorialNumbers(usrInput));
}
}
And if you work with int
only, you can change FactorialNumbers
like this:
int FactorialNumbers(int input){
vector<int> result;
for((input < 0) ? return result : input = input; input != 0; input--)
result.push_back(input);
return result;
}
You can also try: vector<pair<int, std::vector<int>>>