ways to return multiple vlaues?

  • Thread starter Oliver Bleckmann
  • Start date
O

Oliver Bleckmann

hey, which ways are available i c++ to return multiple values
i know u can use structs, any other methods?

concrete question: i want to do sth. like this:
returning two strings into stringvars data and type
<data,type>=<string,string> splittedAttribs()
strucs would lead to big changes in source...
 
R

Rud1ger Sch1erz

Oliver Bleckmann said:
hey, which ways are available i c++ to return multiple values
i know u can use structs, any other methods?

concrete question: i want to do sth. like this:
returning two strings into stringvars data and type
<data,type>=<string,string> splittedAttribs()
strucs would lead to big changes in source...

Arguments by reference spring to mind. You could use the argument list
of your function. Example:

int splittedAttribs(String& data_str, String& type_str)
{
int retval = 0; // may return error status or so...

data_str = "hello";
type_str = "world";

...

return retval;
}

Cheers,
Rudiger
 
M

mlimber

Oliver said:
hey, which ways are available i c++ to return multiple values
i know u can use structs, any other methods?

concrete question: i want to do sth. like this:
returning two strings into stringvars data and type
<data,type>=<string,string> splittedAttribs()
strucs would lead to big changes in source...

Use std::pair, or for more than two, use their extension:
std::tr1::tuple (aka boost::tuple). For an unspecified number of return
values, use a vector (or auto_ptr< vector<> >). If the types are
different, you could use std::vector<boost::any> or similar.

I'm not sure what you are trying to do in your "concrete" question, but
you can probably use ordinary assignment or std::tr1::tie().

You might also consider passing in references as was suggested
previously.

Cheers! --M
 
G

Greg

Oliver said:
hey, which ways are available i c++ to return multiple values
i know u can use structs, any other methods?

concrete question: i want to do sth. like this:
returning two strings into stringvars data and type
<data,type>=<string,string> splittedAttribs()
strucs would lead to big changes in source...

I would recommend using tuples both for their convenience and clarity -
because unlike reference parameters, returning a tuple leaves no doubt
whether a particular value is an input or output parameter.

For example:

#include <iostream>
#include <string>
#include <tr1/tuple>

using std::tr1::tuple;
using std::tr1::make_tuple;
using std::tr1::tie;

tuple<int, char, std::string> f()
{
return make_tuple(5, 'a', "some text");
}

int main()
{
int i;
char c;
std::string s;

tie( i, c, s ) = f();

std::cout << "i: " << i << "\n";
std::cout << "c: " << c << "\n";
std::cout << "s: " << s << "\n";
}

Program Output:

i: 5
c: a
s: some text

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top