code_junkie

Best way to return list of objects in C++

Nov 14th, 2011
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. void DoSomething(const std::vector<SomeType>& in, std::vector<SomeType>& out)
  2. {
  3. ...
  4. // no need to return anything, just modify out
  5. }
  6.  
  7. void DoSomething(iterator const & from, iterator const & to);
  8.  
  9. std::vector theOutput(inputVector);
  10.  
  11. template <typename InIt, typename OutIt>
  12. void DoMagic(InIt first, InIt last, OutIt out)
  13. {
  14. for(; first != last; ++first) {
  15. if(IsCorrectIngredient(*first)) {
  16. *out = DoMoreMagic(*first);
  17. ++out;
  18. }
  19. }
  20. }
  21.  
  22. std::vector<MagicIngredients> ingredients;
  23. std::vector<MagicResults> result;
  24.  
  25. DoMagic(ingredients.begin(), ingredients.end(), std::back_inserter(results));
  26.  
  27. tuple<int, int, double> add_multiply_divide(int a, int b) {
  28. return make_tuple(a+b, a*b, double(a)/double(b));
  29. }
  30.  
  31. list<int> foo(void)
  32. {
  33. list<int> l;
  34. // do something
  35. return l;
  36. }
  37.  
  38. list<int> lst=foo();
  39.  
  40. list<int> lst;
  41. // do anything you want with list
  42. lst.swap(foo());
Add Comment
Please, Sign In to add comment