Friends can i return a list or a map from a function???

Recommended Answers

All 3 Replies

Friends can i return a list or a map from a function???

Why don't you just try it?

map<int, string> Foo(void)
{
    map<int, string> Bar;
    return Bar;
}

int main()
{
   map<int, string> mp = Foo();
   return 0;
}

Works? Question answered :)

Thanks a lot...it worked

The problem with doing that is the program must duplicate the map when returning it, which can be very costly in both time and resources. A better solution is for main() to create the map object, pass it to Foo() by reference, and then Foo() can fill it in

void Foo(map<int, string>& Bar)
{
    // do something with Bar not shown
}

int main()
{
   map<int, string> mp;
   Foo(mp);
   return 0;
}
commented: agreed +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.