Return ANY number of values from a C++ function

Discussion in 'C++' started by SaswatPadhi, Jun 1, 2009.

  1. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com

    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:
    #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:
    [FONT="Fixedsys"]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.[/FONT]
    
    Thanks shabbir for this wonderful forum and thanks all for reading this.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    389
    Trophy Points:
    83

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice