Thread: Returning Multiple Tuples?

  1. #1
    Registered User HalNineThousand's Avatar
    Join Date
    Mar 2008
    Posts
    43

    Returning Multiple Tuples?

    I'm pretty sure the answer to this is no, but as I dig into C++ I keep finding things that surprise me.

    I know I can return an object that could contain just about anything (or pointers to everything). Other than that, is there any way to return multiple tuples from a function in C++, like can be done in Perl?

    Thanks!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm not sure what you mean by "multiple tuples" -- to me, a 3-tuple is just three numbers, etc. Anyway, maybe this code will strike some sparks:
    Code:
    #include <iostream>
    #include <vector>
    
    std::vector<int> sample(void) {
    
        std::vector<int> retval;
        retval.push_back(1);
        retval.push_back(2);
        retval.push_back(3);
        return retval;
    }
    
    int main(){
    
        std::vector<int> george;
        george = sample();
        std::cout << george[0] << std::endl;
        std::cout << george[1] << std::endl;
        std::cout << george[2] << std::endl;
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i would say i know a fair bit about c and c++, but like yourself, i can be mistaken often and be surprised of what i discover.

    having said that, a function can return one single object (or value if it is a primitive type). if you are precise in saying you want to return multiple 'tuple''s (a bunch of items of a different type), you would return a collection of either user-defined struct or class. by collection i mean something like an array (pointer as you stated), set, etc.. if you dont actually mean 'tuple' and mean just more than one of the same type, then again some collection-type of object would be used.

    not sure if this is obvious to you or not--hope it helps.

  4. #4
    Registered User HalNineThousand's Avatar
    Join Date
    Mar 2008
    Posts
    43
    Okay, yes, I've done something like that with vectors. I was just wondering if there were something I was totally clueless about that would let me return two items at once. The only language I know that does that is Perl, but I figured it wouldn't hurt to ask.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by HalNineThousand View Post
    Okay, yes, I've done something like that with vectors. I was just wondering if there were something I was totally clueless about that would let me return two items at once. The only language I know that does that is Perl, but I figured it wouldn't hurt to ask.
    What's a std:: pair, then, chopped liver?
    http://www.sgi.com/tech/stl/pair.html

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    boost::tuple ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. returning multiple values from functions?
    By jamesn56 in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2005, 01:10 PM
  2. Returning Multiple Values
    By StarOrbs in forum C++ Programming
    Replies: 5
    Last Post: 03-09-2005, 01:00 PM
  3. Returning multiple values
    By gcn_zelda in forum C++ Programming
    Replies: 13
    Last Post: 08-02-2003, 05:30 PM
  4. Returning Multiple Values from a Function
    By Learner007 in forum C++ Programming
    Replies: 11
    Last Post: 07-21-2002, 11:03 AM
  5. c function returning multiple values
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-23-2001, 10:09 AM