2

if statements are usually implemented as if(variable > value) what if I have a number of variables and they all have an operation for example a decrement operation and I want that if the value of any of these variables reaches to 0 the program should replace it to 100. Is there a simple way to do that?

  • 1
    Your title mismatches with your description. One ask for [`std::all_of`/`std::any_of`/`std::none_of`](https://en.cppreference.com/w/cpp/algorithm/all_any_none_of), the other asks for a [transform_if](https://stackoverflow.com/questions/23579832/why-is-there-no-transform-if-in-the-c-standard-library) – Jarod42 Mar 27 '20 at 07:00
  • One way is to store all th variables as an array, and then loop over the array – M.M Mar 27 '20 at 07:26

4 Answers4

2

You can't do that by ordinary if but if you have a container containing your values, you can use std::transform and the condition you want, to do the following

#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
    std::vector<int> vec = {1,4 ,-7, 0 ,-9, 9};//The container of the values
    //the following line does every thing
    std::transform(vec.begin(), vec.end(), vec.begin(), [](const int& i){return (i < 0)? 100:i; });
    //To display
    for(auto& el : vec)
        std::cout<< el << " ";

}

And this is another answer inspired by @user2962393 's answer

#include <iostream>

void cond(int & h){
    if( h < 0 )
        h=100;
}
template<typename T, typename... Ts>
void set_100_if_cond(T& x, Ts&... xs) {
    (cond(x), ... , cond( xs ));
}

int main() {
    int var1 {50};
    int var2 {0};
    int var3 {-9};
    set_100_if_cond(var1, var2, var3);
    std::cout << var1 << " " << var2 << " " << var3;
}
asmmo
  • 6,922
  • 1
  • 11
  • 25
  • You can do it in a single `if` if you use an implicit construction from an initializer list: see my answer. – Bathsheba Mar 27 '20 at 06:49
  • 1
    @Bathsheba I think the OP wants to iterate over each value and the change the values satisfying certain condition. But your code doesn't do that – asmmo Mar 27 '20 at 07:00
  • 1
    That's a good point. And makes your answer elegant and appropriate. Have an upvote. – Bathsheba Mar 27 '20 at 07:21
  • Nice solution. Vector could be replaced with an array: std::array. – user2962393 Mar 27 '20 at 07:22
  • @user2962393: Indeed it could, but the rule of thumb "use a `std::vector` unless you have a good reason not to" is a good rule of thumb, and having to type "6" is annoying. (And don't forget to vote accordingly; helps the community.) – Bathsheba Mar 27 '20 at 07:23
  • @Bathsheba Array avoids dynamic memory allocation, and in my book that's often worth more than typing the annoying extra "6". Ideally the extra "6" would be be automatically synthesized. Maybe someday in the future. – user2962393 Mar 27 '20 at 07:38
  • @Bathsheba: Thanks to C++17 CTAD, no size to provide: `std::array> a{1,4 ,-7, 0 ,-9, 9}; – Jarod42 Mar 27 '20 at 12:49
0

Since C++11 you can write

#include <algorithm>
#include <initializer_list>
if (std::max({a, b, c, d, ..., z}) > value){
}

assuming of course all the variables a to z have the same type; you can cast them if not. The ... is used here typographically; insert your actual variable names.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

If all items are in a container, you can use std::any_of. Otherwise, you can wrap up pointers to them into a container and use the same algorithm. If you don't want to do that, you might just have to type it all out or get creative.

Zuodian Hu
  • 979
  • 4
  • 9
0

if any of these values reaches to 0 the program should replace it to 100.

One way to solve this kind of problem is by using variadic templates and recursion (C++11):

#include <iostream>

template<typename T>
void set_100_if_0(T& x) {
   if (x == 0) x = 100;
}

template<typename T, typename... TT>
void set_100_if_0(T& x, TT&... xx) {
    set_100_if_0(x);
    set_100_if_0(xx...);
}

int main() {
    int foo = 42;
    int bar = 0;
    int baz = 99;
    set_100_if_0(foo, bar, baz);
    std::cout << bar << std::endl;
}

The program will print 100.

Is there a simple way to do that?

Nope. Nothing in C++ is simple.

user2962393
  • 1,083
  • 9
  • 12