C++ 条件运算符不允许程序终止
我刚刚了解了条件运算符,正在做一个介绍性练习,说明: 编写一个程序,使用条件运算符查找 一种C++ 条件运算符不允许程序终止,c++,algorithm,iterator,conditional-operator,termination,C++,Algorithm,Iterator,Conditional Operator,Termination,我刚刚了解了条件运算符,正在做一个介绍性练习,说明: 编写一个程序,使用条件运算符查找 一种向量,其值为奇数,且每种向量的值为奇数的两倍 元素 以下是我编写的代码: int main() { vector<int> nums = { 1,2,3,4,5,6,7,8,9 }; int i; auto beg = nums.begin(); while (*beg > 0) // This will always evaluate to true.
向量
,其值为奇数,且每种向量的值为奇数的两倍
元素
以下是我编写的代码:
int main()
{
vector<int> nums = { 1,2,3,4,5,6,7,8,9 };
int i;
auto beg = nums.begin();
while (*beg > 0) // This will always evaluate to true.
{
((*beg) % 2 == 0 && (beg < nums.end()) ? i = 0 : *beg = 2 * (*(beg++)));
/*If the number is even the program will just assign 0 to i*/
}
}
intmain()
{
向量nums={1,2,3,4,5,6,7,8,9};
int i;
auto beg=nums.begin();
while(*beg>0)//这将始终计算为true。
{
(*beg)%2==0&&(beg
如果将最后一行更改为:
((*beg)%2 == 0 && (beg < nums.end()) ? i = 0 : *beg = 2*(*(beg)));
++beg;
((*beg)%2==0&(beg
为什么会这样 它卡住了,因为如果条件
(*beg)%2==0&&(beg
为true
,迭代器将不会增加以进行进一步检查。您只有设置i=0
。您也应该增加迭代器
您可以为此使用:
while (beg != nums.end() && *beg > 0)
{
(*beg) % 2 == 0 ? (beg++, i): (*beg = 2 * (*beg) , beg++, ++i );
}
还要注意的是,计数i
应该在开始之前初始化,而不是在while
循环中初始化。
根据要求,完整的工作代码为:
#include <iostream>
#include <vector>
int main()
{
std::vector<int> nums = { 1,2,3,4,5,6,7,8,9 };
int i{0};
auto beg = nums.begin();
while (beg != nums.end() && *beg > 0)
{
(*beg) % 2 == 0 ? (beg++, i): (*beg = 2 * (*beg) , beg++, ++i );
}
for (const int ele : nums)
std::cout << ele << " ";
std::cout << "\ncount: " << i << "\n";
}
这就是说,IMO使用逗号运算符和上述条件运算符(任务)并不是一种好的编码方式,这只会让代码库的未来读者感到困惑
另请阅读:它卡住了,因为如果条件
(*beg)%2==0&&(beg
为true
,迭代器将不会为进一步检查而递增。您只有设置i=0
。您也应该递增迭代器
您可以为此使用:
while (beg != nums.end() && *beg > 0)
{
(*beg) % 2 == 0 ? (beg++, i): (*beg = 2 * (*beg) , beg++, ++i );
}
还要注意的是,计数i
应该在开始之前初始化,而不是在while
循环中初始化。
根据要求,完整的工作代码为:
#include <iostream>
#include <vector>
int main()
{
std::vector<int> nums = { 1,2,3,4,5,6,7,8,9 };
int i{0};
auto beg = nums.begin();
while (beg != nums.end() && *beg > 0)
{
(*beg) % 2 == 0 ? (beg++, i): (*beg = 2 * (*beg) , beg++, ++i );
}
for (const int ele : nums)
std::cout << ele << " ";
std::cout << "\ncount: " << i << "\n";
}
这就是说,IMO使用逗号运算符和上述条件运算符(任务)并不是一种好的编码方式,这只会让代码库的未来读者感到困惑
另请阅读:如果您想将某些值加倍,而不是将其他值加倍,只需执行以下操作:
#include <iostream>
#include <vector>
int main() {
std::vector<int> nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for (int& num : nums)
num = num % 2 ? 2 * num : num;
for (int num : nums)
std::cout << num << ' ';
std::cout << '\n';
return 0;
}
#包括
#包括
int main(){
向量nums={1,2,3,4,5,6,7,8,9};
for(int&num:nums)
num=num%2?2*num:num;
for(int-num:nums)
std::cout如果您想将某些值加倍,而不是将其他值加倍,只需执行以下操作:
#include <iostream>
#include <vector>
int main() {
std::vector<int> nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for (int& num : nums)
num = num % 2 ? 2 * num : num;
for (int num : nums)
std::cout << num << ' ';
std::cout << '\n';
return 0;
}
#包括
#包括
int main(){
向量nums={1,2,3,4,5,6,7,8,9};
for(int&num:nums)
num=num%2?2*num:num;
for(int-num:nums)
std::cout,因为在第一个版本中,仅当(*beg)%2==0&(beg
为false
,因此您的程序被卡住。在第二个版本中,增量始终为called@ButiriDan为什么它会卡住?如果条件为真,它总是可以将i分配给0!因为在第一个版本中,只有当((*beg)%2==0&(beg
为false
,因此您的程序被卡住。在第二个版本中,增量始终为called@ButiriDan为什么会卡住?如果条件为真,它总是可以将i指定为0!