1

I'm new to cpp as from today, moved from matlab to run simulations faster.

I would like to build a function that receives a vector and returns vectors that contain the indexes of a given value and the number of appearance.

for example, if I have:

A={0 , 1, 1, 1,  0};

I would like to get

vec1={0, 4}; // 0
a=2;
vec2=(1,2,3); // 1
b=3;

so far I have:

# include <iostream>
# include <string>
# include <vector>
using namespace std;
const int nTot = 10;

void get_index(vector<int> FVec, vector<int> &vec1, vector<int> &vec2)
{

    for (unsigned int i=0; i < FVec.size(); i++)
    {
        if (FVec[i] == 0)
        {
            vec1.push_back(i);
        }
        if (FVec[i] == 1)
        { 
            vec2.push_back(i);
        }
    }
}


int main()

{
    int nf = nTot/2;
    vector<int> FVec(nf);

    vector<int> vec1;
    vector<int> vec2;


    get_index(FVec,vec1,vec2);


    system (" pause");
    return 0;
}

The problem here now is that it does not change the vectors, probably due to the assignment in the function

jarhead
  • 1,821
  • 4
  • 26
  • 46
  • The only way that could compile is if `vec1` and `vec2` are global variables, in which case `main()` already has access to them. – Jonathan Potter Sep 24 '15 at 09:46
  • @JonathanPotter could you elaborate on this more? the general problem here is not how to do it, it is more technical issues of how to have it written so it would work ( what should be with * or & and defined in or out of main...) – jarhead Sep 24 '15 at 10:06
  • 1
    slightly off question, but i would change `vector FVec` in get_index() to `const vector &FVec` – Quest Sep 24 '15 at 11:32
  • @Quest , why is it better? what is the difference? – jarhead Sep 24 '15 at 11:33
  • 1
    you are passing new copy of vector (that can be really expensive) and const & will pass unmodifiable reference – Quest Sep 24 '15 at 11:38
  • I've just compiled your code you posted in question and it runs without problem. At the end vec1 has 5 elements (all 0) – Quest Sep 24 '15 at 11:40
  • yes, I got it right as well now, had some debugging mistake, tnx a lot for the help! – jarhead Sep 24 '15 at 11:41
  • 1
    for futher reading about const ref vs by value see [this link](http://stackoverflow.com/questions/270408/is-it-better-in-c-to-pass-by-value-or-pass-by-constant-reference) – Quest Sep 24 '15 at 11:46

1 Answers1

1

The c++ way to "return" multiple things from a single function is actually to pass those things by address to the function and updating their pointed value into the function. Then you don't have to return anything.

For example here: void get_indexes(Vector * vec1, Vector* vec2, int *a, int *b)

floppy12
  • 1,045
  • 6
  • 12
  • than it suppose to be int* a and int* b as well,right? and how do I define in this case vec1 and vec2, if their size changes every iteration – jarhead Sep 24 '15 at 10:16
  • 1
    @jarhead yes. and your function call should be like `func_name(&a, &b);` – Quest Sep 24 '15 at 10:21
  • 1
    @jarhead chec [`std::vector::resize`](http://www.cplusplus.com/reference/vector/vector/resize/) – Quest Sep 24 '15 at 10:24
  • 1
    Instead of resizing or updating each "case content" you could only push_back a new value to vec1/vec2... you won't have to know the size in advance. By the way you can also access the size of vector, don't need a et b anymore – floppy12 Sep 24 '15 at 11:04
  • @floppy12, I've edited the code in the question, using the push_back, still I have a slight problem with the assignment of the &/*. – jarhead Sep 24 '15 at 11:24
  • vec1 and vec2 don't change after calling them o the function – jarhead Sep 24 '15 at 11:27
  • @floppy12 , I got it right now, tnx for the help anyhow :) – jarhead Sep 24 '15 at 11:42