Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/articles/cpp-tutorials/)
-   -   Return ANY number of values from a C++ function (http://www.go4expert.com/articles/return-values-cpp-function-t17816/)

SaswatPadhi 1Jun2009 21:52

Return ANY number of values from a C++ function
 

Introduction



Many times you may have wondered how to return several values from a C++ function. I know it's easy for most of you when you know "how many" values your func is going to return. I have seen many people use std::pair to return two values from a func. But what about the case when you don't know how many values the func might return ??

Background



Two years ago, I wanted a func to return all occurrences of a value from an array. I could easily return the number of occurrences, but how do I find out "where" the values occur ? After struggling for a week, (and getting no significant help from my teacher), I I achieve this goal by using a very simple trick.

The code



You must have come across map, I mean in C++ of course ! It simply maps one value (of any type) to another (of any type). The basic difference between a map and an array is that array maps int to any type but map maps any type to any type.

I will use map to return multiple values from a func. The best part is : you don't need to worry about "how many".

I think the code is self-explanatory. So, I present it to you below :
Code: C++

#include <iostream>
#include <map>

using namespace std;

map<int,int> CheckOccurrence (int X[], int Size, int Key)
{
    map<int,int> RetMap;
    int Cntr = 1;

    for(int i = 0; i < Size; ++i)
        if( X[i] == Key )
            RetMap[Cntr++] = i;
    RetMap[0] = Cntr-1;

    return RetMap;
}

int main()
{
    int TestArray[20] = {1,5,5,3,4,1,4,3,5,1,5,3,3,4,1,3,1,5,5,4};
    int TestKey;
    map<int,int> Occurrences;

    for(TestKey = 1; TestKey < 6; ++TestKey)
    {
        Occurrences = CheckOccurrence(TestArray,20,TestKey);
        if( Occurrences[0] )
        {
            cout << TestKey << " occurs " << Occurrences[0] << " times at these locations : \n";
            for(int i = 1; i < Occurrences[0]+1; ++i)
                cout << "    Occurence[" << i << "] = " << Occurrences[i] << ".\n";
        }
        else
            cout << TestKey << " does not occur in the array.\n";
        cout << endl;
    }
    return 0;
}


Simple, isn't it ?? ;)

Output



Code:

1 occurs 5 times at these locations :
    Occurence[1] = 0.
    Occurence[2] = 5.
    Occurence[3] = 9.
    Occurence[4] = 14.
    Occurence[5] = 16.

2 does not occur in the array.

3 occurs 5 times at these locations :
    Occurence[1] = 3.
    Occurence[2] = 7.
    Occurence[3] = 11.
    Occurence[4] = 12.
    Occurence[5] = 15.

4 occurs 4 times at these locations :
    Occurence[1] = 4.
    Occurence[2] = 6.
    Occurence[3] = 13.
    Occurence[4] = 19.

5 occurs 6 times at these locations :
    Occurence[1] = 1.
    Occurence[2] = 2.
    Occurence[3] = 8.
    Occurence[4] = 10.
    Occurence[5] = 17.
    Occurence[6] = 18.

Thanks shabbir for this wonderful forum and thanks all for reading this.

shabbir 2Jul2009 11:21

Re: Return ANY number of values from a C++ function
 
Nomination for article of the month - Jun 2009 Started. Nominate this article for Article of the month - Jun 2009


All times are GMT +5.5. The time now is 13:01.