fork(1) download
  1.  
  2. #include <iostream>
  3. #include <vector>
  4. #include <memory>
  5.  
  6. using XS = std::vector<std::shared_ptr<int>>;
  7.  
  8. void foo(XS const& xs) {
  9. for (auto& x : xs) {
  10. *x = 42;
  11. }
  12. }
  13.  
  14. void print(XS const& xs) {
  15. std::cout << "<XS>\n";
  16. for (auto const& x : xs) {
  17. std::cout << "\t" << *x << "\n";
  18. }
  19. std::cout << "</XS>\n";
  20. }
  21.  
  22. int main(int, char**) {
  23. XS xs;
  24. for (auto i : { 1, 2, 3}) {
  25. xs.push_back(std::make_shared<int>(i));
  26. }
  27.  
  28. std::cout << "before:\n";
  29. print(xs);
  30. foo(xs);
  31. std::cout << "after:\n";
  32. print(xs);
  33.  
  34. return 0;
  35. }
  36.  
  37.  
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
before:
<XS>
	1
	2
	3
</XS>
after:
<XS>
	42
	42
	42
</XS>