为什么 if 语句在 C++ 函数中不起作用?
Why is if statement not working in c++ function?
在这段代码中,我可以在向量中找到一个元素并可以打印它的索引 too.But 如果我给一个输入让我们说 1 不是向量元素,它不会打印输出为“Element Not找到了。”。while 循环后的 if 语句不起作用。
#include<bits/stdc++.h>
using namespace std;
void search(vector<vector<int>> v,int e){
int i = 0;
int j = v[0].size() - 1;
int found = 0;
while(v[i][j]){
if(v[i][j] == e){
found = 1;
cout<< "Element found at: (" << i <<" , "<< j <<" )"<<endl;
break;
}
else{
if(v[i][j] > e){
j--;
}
else
i++;
}
}
if(found == 0){
cout<< "Element Not Found."<< endl;
}
}
int main(){
vector<vector<int>> v{
{10,20,30,40},
{15,25,35,45},
{27,29,37,48},
{32,33,39,50}
};
int e;
cout<< "Enter the element to find:";
cin>> e;
search(v,e);
return 0;
}
这是我的全部代码。
感谢您的提示。
v[i][j]
会不断迭代,直到下标return为任意非零值,(即使超出有效边界).
你应该在循环中引入这个条件:
while(i < v[0].size() && j >= 0) ...;
至少引入检查 v
是否为空也是明智的。我也想提请注意这部分:
// ...
if(v[i][j] == e) {
found = 1; // Useless statement
cout << "Element found at: (" << i <<" , "<< j <<" )" << endl;
break; // We can just place a return here
}
// ...
found
更新没有任何意义,因为如果条件为真,是的,我们找到了寻找的值,我们打印并可能 return (无效)。因此在下面的 if
中检查不需要 found
标志的值。
所以,最后,我建议你看看这个解决方案:
void search(vector<vector<int>> v,int e){
if (!v.size()) {
std::cout << "Empty." << std::endl;
return;
}
int i = 0;
int j = v[0].size() - 1;
int found = 0;
while(i < v[0].size() && j >= 0){
if(v[i][j] == e){
cout<< "Element found at: (" << i <<" , "<< j <<" )"<<endl;
return;
}
else (v[i][j] > e) ? --j : ++i; // Ternary operator
}
cout << "Element Not Found." << endl;
}
示例:
Enter the element to find: 48
Element found at: (2 , 3 )
Enter the element to find: 42
Element Not Found.
在这段代码中,我可以在向量中找到一个元素并可以打印它的索引 too.But 如果我给一个输入让我们说 1 不是向量元素,它不会打印输出为“Element Not找到了。”。while 循环后的 if 语句不起作用。
#include<bits/stdc++.h>
using namespace std;
void search(vector<vector<int>> v,int e){
int i = 0;
int j = v[0].size() - 1;
int found = 0;
while(v[i][j]){
if(v[i][j] == e){
found = 1;
cout<< "Element found at: (" << i <<" , "<< j <<" )"<<endl;
break;
}
else{
if(v[i][j] > e){
j--;
}
else
i++;
}
}
if(found == 0){
cout<< "Element Not Found."<< endl;
}
}
int main(){
vector<vector<int>> v{
{10,20,30,40},
{15,25,35,45},
{27,29,37,48},
{32,33,39,50}
};
int e;
cout<< "Enter the element to find:";
cin>> e;
search(v,e);
return 0;
}
这是我的全部代码。 感谢您的提示。
v[i][j]
会不断迭代,直到下标return为任意非零值,(即使超出有效边界).
你应该在循环中引入这个条件:
while(i < v[0].size() && j >= 0) ...;
至少引入检查 v
是否为空也是明智的。我也想提请注意这部分:
// ...
if(v[i][j] == e) {
found = 1; // Useless statement
cout << "Element found at: (" << i <<" , "<< j <<" )" << endl;
break; // We can just place a return here
}
// ...
found
更新没有任何意义,因为如果条件为真,是的,我们找到了寻找的值,我们打印并可能 return (无效)。因此在下面的 if
中检查不需要 found
标志的值。
所以,最后,我建议你看看这个解决方案:
void search(vector<vector<int>> v,int e){
if (!v.size()) {
std::cout << "Empty." << std::endl;
return;
}
int i = 0;
int j = v[0].size() - 1;
int found = 0;
while(i < v[0].size() && j >= 0){
if(v[i][j] == e){
cout<< "Element found at: (" << i <<" , "<< j <<" )"<<endl;
return;
}
else (v[i][j] > e) ? --j : ++i; // Ternary operator
}
cout << "Element Not Found." << endl;
}
示例:
Enter the element to find: 48
Element found at: (2 , 3 )
Enter the element to find: 42
Element Not Found.