Calling values from a function

I am trying to get sp, ep and ep-sp+1 values printed using this function. I am able to get only one value printed.Please help to write best way to get these values from the function.


int Tree::Count(const char *p, int m)
{
std::vector<char>::iterator itLower = lower_bound(sigma.begin(), sigma.end(), p[m-1]);
if ((*itLower) != p[m-1])
{
return 0;
}
register unsigned int i = m-1;
int sp = count[(*itLower)-SIGMAMIN];
int ep;
if(itLower<sigma.end()-1){
ep = count[(*(itLower+1))-SIGMAMIN]-1;
}
else
ep = len-1;

while (sp<=ep && i>0)
{

char c = p[i-1];
sp = count[c-SIGMAMIN]+Occ(c, sp-1);
ep = count[c-SIGMAMIN]+Occ(c, ep)-1;

i--;
}

return ep-sp+1;
}

and in main function I call this function to get the value of return as
main(){
int occ = t.Count(input.c_str(), input.size());
}


Firstly, if you have code to post, it's easier on the eyes if you use code tags: http://www.cplusplus.com/forum/articles/42672/

Secondly, I assume your problem is your function is computing three values and you would like all three of them returned. Well, for this example, you could have an array, A storing three integer elements, where A[0] would be sp, A[1] would be ep, and A[2] would be ep-sp+1 (optional to store this as it can be computed from the first two).

You could use the array like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int Tree::Count(const char *p, int m, int arraySPAndEP[2])
{
   ...
   //replace assignments to ep and sp with your array
   arraySPAndEP[0] = count[(*itLower)-SIGMAMIN]; //sp
   if(itLower<sigma.end()-1)
   {
      arraySPAndEP[1] = count[(*(itLower+1))-SIGMAMIN]-1; //ep
   }
   else
      arraySPAndEP[1]  = len-1; //ep
   ...
}

int main()
{
   int arraySPAndEP[2];
   int occ = t.Count(input.c_str(), input.size(), arraySPAndEP);
}


Also, you could use pass by reference. See the following two articles (pass by reference is mentioned in the second article):
http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/
Thank you very much .
Topic archived. No new replies allowed.