fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. class Vec {
  5. std::vector<double> mData;
  6. int mSize;
  7. public:
  8. Vec(int size) : mData(std::vector<double>(size,0)), mSize(size) {
  9. std::cout << "constructed" << std::endl;
  10. }
  11. double& operator[](int i) {return mData[i];}
  12. double operator[](int i) const {return mData[i];}
  13. Vec choose(const Vec& ifPositive, const Vec& ifNegative) const;
  14. Vec& setNegative(Vec& target, const Vec& ifNegative) const;
  15. };
  16.  
  17. Vec Vec::choose(const Vec& ifPositive, const Vec& ifNegative) const {
  18. Vec out(mSize);
  19. for(int index = 0; index < mSize; ++index) {
  20. if(mData[index] > 0)
  21. out[index] = ifPositive[index];
  22. else
  23. out[index] = ifNegative[index];
  24. }
  25. return out;
  26. }
  27.  
  28. Vec& Vec::setNegative(Vec& target, const Vec& ifNegative) const {
  29. for(int index = 0; index < mSize; ++index) {
  30. if(mData[index] <= 0)
  31. target[index] = ifNegative[index];
  32. }
  33. return target;
  34. }
  35.  
  36. int main() {
  37. const int size = 100;
  38.  
  39. Vec trigger(size); // 1. constructor
  40. for(int i = 0; i < size; ++i) {
  41. if(i < size/2)
  42. trigger[i] = -1;
  43. else
  44. trigger[i] = 1;
  45. }
  46.  
  47. Vec valA(size), valB(size); // 2. & 3. constructor
  48. for(int i = 0; i < size; ++i) {
  49. valA[i] = 1;
  50. valB[i] = 2;
  51. }
  52.  
  53. valA = trigger.choose(valA, valB); // 4. constructor -> not necessary, see next line
  54. valA = trigger.setNegative(valA, valB); // no constructor
  55.  
  56. Vec valC = trigger.choose(valA, valB); // 5. constructor -> okay
  57.  
  58. }
Success #stdin #stdout 0s 2988KB
stdin
Standard input is empty
stdout
constructed
constructed
constructed
constructed
constructed