为什么我的功能不能正常工作
Why is my function not working properly
我检查向量中的数字是偶数还是奇数的函数无法正常工作。
此函数打印我在向量中键入的数字并将它们放入 2 个类别 EVENS/ODDS。但是,如果我有一个负数,那么不打印它但均匀工作的赔率有问题。
有问题的代码在这里:
void printEvensAndOddsVector(const vector <int>& new_v1)
{
cout << "Vector Evens: ";
for (unsigned int i = 0; i < new_v1.size(); ++i)
{
if (new_v1.at(i) % 2 == 0)
{
cout << new_v1.at(i) << " ";
}
}
cout << " \n";
cout << "Vector Odds: ";
for (unsigned int i = 0; i < new_v1.size(); ++i)
{
if (new_v1.at(i) % 2 == 1) // Here is the problem.
{
cout << new_v1.at(i) << " ";
}
}
cout << " \n";
}
或者更准确地说是这一行:if (new_v1.at(i) % 2 == 1)
它打印奇数,但只打印正数而不打印负数。
但是,如果我将其更改为 if (new_v1.at(i) % 2 != 0)
,则它可以正常工作。
为什么会发生这种情况,等于运算符是否存在问题?
如果是,那么为什么在仍然使用等于运算符的情况下打印均匀值,即使它们是负数?
此处代码供参考
clude <iostream>
#include <vector>
using namespace std;
void fillVector(vector <int>&);
void printVector(const vector <int>&);
void printEvensAndOddsVector(const vector <int>&); // Prints Evens and Odds.
int main()
{
vector <int> v1;
fillVector(v1);
printVector(v1);
printEvensAndOddsVector(v1);
cout << "\n\n\n";
system( "pause");
return 0;
}
// Function Definitions
void fillVector(vector <int>& new_v1)
{
int number;
cout << "Type in numbers and type -100 to stop: ";
cin >> number;
while (number != -100)
{
new_v1.push_back(number);
cin >> number;
}
}
void printVector(const vector <int>& new_v1)
{
cout << "\nVector: ";
for (unsigned int i = 0; i < new_v1.size(); ++i)
{
cout << new_v1.at(i) << " ";
}
cout << " \n";
}
void printEvensAndOddsVector(const vector <int>& new_v1)
{
cout << "Vector Evens: ";
for (unsigned int i = 0; i < new_v1.size(); ++i)
{
if (new_v1.at(i) % 2 == 0)
{
cout << new_v1.at(i) << " ";
}
}
cout << " \n";
cout << "Vector Odds: ";
for (unsigned int i = 0; i < new_v1.size(); ++i)
{
if (new_v1.at(i) % 2 == 1)
{
cout << new_v1.at(i) << " ";
}
}
cout << " \n";
}
This line: if (new_v1.at(i) % 2 == 1)
. It prints the numbers that are odd but only the positive ones not the negative ones.
对于 C++ 中的负数 n
、n % 2
returns 0
或 -1
。它永远不会 returns 1
,因此对于负输入,条件不可能成立。
如您所见,与零进行比较是可行的。
见Modulo operator with negative values
我检查向量中的数字是偶数还是奇数的函数无法正常工作。 此函数打印我在向量中键入的数字并将它们放入 2 个类别 EVENS/ODDS。但是,如果我有一个负数,那么不打印它但均匀工作的赔率有问题。 有问题的代码在这里:
void printEvensAndOddsVector(const vector <int>& new_v1)
{
cout << "Vector Evens: ";
for (unsigned int i = 0; i < new_v1.size(); ++i)
{
if (new_v1.at(i) % 2 == 0)
{
cout << new_v1.at(i) << " ";
}
}
cout << " \n";
cout << "Vector Odds: ";
for (unsigned int i = 0; i < new_v1.size(); ++i)
{
if (new_v1.at(i) % 2 == 1) // Here is the problem.
{
cout << new_v1.at(i) << " ";
}
}
cout << " \n";
}
或者更准确地说是这一行:if (new_v1.at(i) % 2 == 1)
它打印奇数,但只打印正数而不打印负数。
但是,如果我将其更改为 if (new_v1.at(i) % 2 != 0)
,则它可以正常工作。
为什么会发生这种情况,等于运算符是否存在问题?
如果是,那么为什么在仍然使用等于运算符的情况下打印均匀值,即使它们是负数?
此处代码供参考
clude <iostream>
#include <vector>
using namespace std;
void fillVector(vector <int>&);
void printVector(const vector <int>&);
void printEvensAndOddsVector(const vector <int>&); // Prints Evens and Odds.
int main()
{
vector <int> v1;
fillVector(v1);
printVector(v1);
printEvensAndOddsVector(v1);
cout << "\n\n\n";
system( "pause");
return 0;
}
// Function Definitions
void fillVector(vector <int>& new_v1)
{
int number;
cout << "Type in numbers and type -100 to stop: ";
cin >> number;
while (number != -100)
{
new_v1.push_back(number);
cin >> number;
}
}
void printVector(const vector <int>& new_v1)
{
cout << "\nVector: ";
for (unsigned int i = 0; i < new_v1.size(); ++i)
{
cout << new_v1.at(i) << " ";
}
cout << " \n";
}
void printEvensAndOddsVector(const vector <int>& new_v1)
{
cout << "Vector Evens: ";
for (unsigned int i = 0; i < new_v1.size(); ++i)
{
if (new_v1.at(i) % 2 == 0)
{
cout << new_v1.at(i) << " ";
}
}
cout << " \n";
cout << "Vector Odds: ";
for (unsigned int i = 0; i < new_v1.size(); ++i)
{
if (new_v1.at(i) % 2 == 1)
{
cout << new_v1.at(i) << " ";
}
}
cout << " \n";
}
This line:
if (new_v1.at(i) % 2 == 1)
. It prints the numbers that are odd but only the positive ones not the negative ones.
对于 C++ 中的负数 n
、n % 2
returns 0
或 -1
。它永远不会 returns 1
,因此对于负输入,条件不可能成立。
如您所见,与零进行比较是可行的。
见Modulo operator with negative values