在这里,使用几个IF条件或SWITCH-CASE进行编码可能会很好
coding with several IF conditions or is SWITCH-CASE might be good here?
我有一个vector<ClassA>
(比如my_vector
,这个ClassA本身是ClassB的向量),我想写if condition
来测试这样的条件:
(1)。如果只有一个元素不为空,而其他元素都为空,(如my_vector的大小是5,我应该测试这个非空的例如my_vector[0], my_vector[1],…)
(2)也适用于两个元素不为空而其他元素为空的情况(其他配对类似)
(3)同样,三个或三个以上的元素不为空
我在想如何编码这个
这是我的尝试
if (!my_vector[0].empty() && my_vector[1].empty() && my_vector[2].empty() && .. &&
my_vector[4].empty()){ //process 1}
else if (!my_vector[1].empty() && my_vector[0].empty() && my_vector[2].empty() && ..){
//process 2}
else if(!my_vector[2].empty() && my_vector[0].empty() && my_vector[1].empty() && ..){
//process 3}
...
...
else if (!my_vector[0].empty() && !my_vector[1].empty() && my_vector[2].empty() && ..
my_vector[4].empty()){ //process n}
else if (!my_vector[0].empty() && !my_vector[2].empty() && my_vector[1].empty() && ..
my_vector[4].empty()){ //process n+1}
....
....
else if (!my_vector[0].empty() && !my_vector[1].empty() && !my_vector[2].empty() &&
my_vector[3].empty() && my_vector[4].empty()){ //process p}
....
like wise
这真的很难测试,任何有条理的方法都无法做到。
使用<algorithm>
中的count_if模板函数和lambda,您将获得一个紧凑而清晰的解决方案:
unsigned int non_empties = std::count_if(myvector.begin(), myvector.end(), [](const ClassA & c) {
return !c.empty();
});
if (non_empties == 1) {
// process 1
} else if (non_empties == 2) {
// process 2
} else if (non_empties >= 3) {
// process 3
}
<algorithm>
库令人惊讶地经常被忽视,尽管它提供了像这样的真正实用的解决方案。
如果空/非空元素的配对无关紧要,则可以遍历集合以获得空元素的数量:
int size = my_vector.size();
int emptyCount = 0;
for (int i = 0; i < size; i++) {
emptyCount += (my_vector[i].empty() ? 1 : 0);
}
if (emptyCount == 0) {
// no elements are empty
} else if (emptyCount == 1) {
// only one element is empty
} else if { emptyCount == 2) {
// two elements are empty
} ...
最后,使用此方法,您仍然需要为每个条件使用if/else-if;但是,这可以扩展为使用百分比(如果您的集合增长到随机大小)。
定义一个模式数组(概念上是bool
的2D数组)。然后依次遍历每一行,找到匹配的行。然后调用与该行对应的相关函数(您可能需要一个函数指针数组)。
也许c++11的新函数all_of, none_of可以帮到你。
你可以这样写
auto iter_begin = my_vector.begin();
auto iter_end = my_vector.end();
auto is_empty = [](decltype(*iter_begin) const & param){ return param.empty();};
auto is_not_empty = [](decltype(*iter_begin) const & param){ return !param.empty();};
if(iter_begin->empty() && all_of(iter_begin + 1, iter_end, is_empty ))
else if(all_of(iter_begin, iter_begin + 1, is_not_empty) && all_of(iter_begin + 2, iter_end, is_empty))
else if(all_of(iter_begin, iter_begin + 2, is_not_empty) && all_of(iter_begin + 3, iter_end, is_empty))
等等
相关文章:
- 在 C++ 的 Switch Case 中创建对象后对对象调用方法
- switch-case语句如何在Arduino/C++中工作
- 一个'case'在简单的'switch'语句中不起作用
- 为什么您可以在 switch 语句中的"默认"下定义变量,而不能在"case"下定义变量
- 当"if else"已经存在时,我为什么要学习"switch case"
- 使用枚举关闭switch/case语句的编译器警告
- 在我的switch语句完全执行之前,我的while循环进入第二次迭代,我用c ++编码
- 在C++中的Switch Case中执行OR
- 在这里,使用几个IF条件或SWITCH-CASE进行编码可能会很好
- c++访问器mutator未读取cin-switch case语句
- switch case是循环还是条件结构?
- 我如何告诉gcc在switch/case语句上警告(或失败)而不中断?
- 不能从switch case语句传递构造函数的参数
- 如何在c++中循环一次要求输入的switch-case语句?
- 在c++ 11中,哪个更正确、更快:switch-case还是if().| |.[|].
- switch case语句中的循环
- 在SW版本中添加新参数时如何处理"switch/case"
- 为什么在switch case语句中只允许有限的类型?
- Switch case语句如何在内部实现或工作
- switch case vs if else