fork download
  1. #include <experimental/optional>
  2.  
  3. #include <vector>
  4.  
  5. #include <algorithm>
  6.  
  7. #include <iostream>
  8.  
  9. template<typename t>
  10. using optional = std::experimental::fundamentals_v1::optional<t>;
  11.  
  12. int main()
  13. {
  14. auto nullopt = std::experimental::fundamentals_v1::nullopt;
  15.  
  16. std::size_t nResults = 10;
  17. std::vector<optional<std::size_t> > sizes(nResults, nullopt);
  18.  
  19. sizes[4] = 400;
  20. sizes[7] = 500;
  21.  
  22. for(std::size_t i = 0; i < sizes.size(); ++i)
  23. {
  24. if(sizes[i])
  25. std::cout<< "size at index " << i << ": " << *sizes[i];
  26. else
  27. std::cout<< "no size computed at index " << i;
  28. std::cout<< "\n";
  29. }
  30. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
no size computed at index 0
no size computed at index 1
no size computed at index 2
no size computed at index 3
size at index 4: 400
no size computed at index 5
no size computed at index 6
size at index 7: 500
no size computed at index 8
no size computed at index 9