How to return 2 vectors of different types in C++

Question:

I perform a calculation and my result is saved in a

vector<int>

it is a

vector<string>

How do I make my function return these two parameters in my main() ?

int  cracking(string chemform) {

    vector<int> pos;
    vector<int> multi;  // Vector of multipliers
    vector<string> parte; // Vector of atoms
    int conta, i, conta2, local, chk;
    string temp, temp2;
...
return 0;
}

Wanted to return multi and part. I saw some people talking about templates but it seems to me that it only works when I want to choose a type, I want to use both. Is such a thing possible?

One option would be to transform the integer vector into a string vector, but I don't know if it's possible to pass more than one vector to a function.

Answer:

There are many ways to make the function return vectors, the simplest I believe is to return a structure containing these 2 vectors.

#include <iostream>
#include <vector>
using namespace std;

struct retVec{
    vector<int> multi;  // Vector of multipliers
    vector<string> parte; // Vector of atoms
};

retVec cracking(string chemform) {

    vector<int> pos;
    vector<int> multi;  // Vector of multipliers
    vector<string> parte; // Vector of atoms
    int conta, i, conta2, local, chk;
    string temp, temp2;
    retVec retv = {multi,parte};
    return retv;
}
int main()
{
    //cout<<"Hello World";

    retVec vec=cracking("");
    vector<int> multi = vec.multi;
    vector<string> parte = vec.parte; 
    return 0;
}
Scroll to Top