C++ 使用For循环显示1个结果
这是我的代码:C++ 使用For循环显示1个结果,c++,C++,这是我的代码: void IDsearch(vector<Weatherdata>temp) { int userinput; cout << "Enter the ID of the Event and i will show you all other information: " << endl; cin >> userinput; for(unsigned int i = 0; i < temp.siz
void IDsearch(vector<Weatherdata>temp)
{
int userinput;
cout << "Enter the ID of the Event and i will show you all other information: " << endl;
cin >> userinput;
for(unsigned int i = 0; i < temp.size();i++)
{
if(userinput == temp[i].eventID)
{
cout << "Location: " << temp[i].location << endl;
cout << "Begin Date: " << temp[i].begindate << endl;
cout << "Begin Time: " << temp[i].begintime << endl;
cout << "Event Type: " << temp[i].type << endl;
cout << "Death: " << temp[i].death << endl;
cout << "Injury: " << temp[i].injury << endl;
cout << "Property Damage: " << temp[i].damage << endl;
cout << "Latitude: " << temp[i].beginlat << endl;
cout << "Longitude: " << temp[i].beginlon << endl;
}
}
}
void IDsearch(vectortemp)
{
int用户输入;
cout用户输入;
对于(无符号整数i=0;i cout您可以使用标志来记住是否找到了某些元素
void IDsearch(const vector<Weatherdata>&temp) // use reference for better performance
{
int userinput;
bool found = false;
cout << "Enter the ID of the Event and i will show you all other information: " << endl;
cin >> userinput;
for(unsigned int i = 0; i < temp.size();i++)
{
if(userinput == temp[i].eventID)
{
cout << "Location: " << temp[i].location << endl;
cout << "Begin Date: " << temp[i].begindate << endl;
cout << "Begin Time: " << temp[i].begintime << endl;
cout << "Event Type: " << temp[i].type << endl;
cout << "Death: " << temp[i].death << endl;
cout << "Injury: " << temp[i].injury << endl;
cout << "Property Damage: " << temp[i].damage << endl;
cout << "Latitude: " << temp[i].beginlat << endl;
cout << "Longitude: " << temp[i].beginlon << endl;
found = true;
}
}
if(!found)
{
cout << "it doesnt match" << endl;
}
}
void IDsearch(const vector&temp)//使用引用可获得更好的性能
{
int用户输入;
bool-found=false;
cout用户输入;
对于(无符号整数i=0;i cout您可以使用标志来记住是否找到了某些元素
void IDsearch(const vector<Weatherdata>&temp) // use reference for better performance
{
int userinput;
bool found = false;
cout << "Enter the ID of the Event and i will show you all other information: " << endl;
cin >> userinput;
for(unsigned int i = 0; i < temp.size();i++)
{
if(userinput == temp[i].eventID)
{
cout << "Location: " << temp[i].location << endl;
cout << "Begin Date: " << temp[i].begindate << endl;
cout << "Begin Time: " << temp[i].begintime << endl;
cout << "Event Type: " << temp[i].type << endl;
cout << "Death: " << temp[i].death << endl;
cout << "Injury: " << temp[i].injury << endl;
cout << "Property Damage: " << temp[i].damage << endl;
cout << "Latitude: " << temp[i].beginlat << endl;
cout << "Longitude: " << temp[i].beginlon << endl;
found = true;
}
}
if(!found)
{
cout << "it doesnt match" << endl;
}
}
void IDsearch(const vector&temp)//使用引用可获得更好的性能
{
int用户输入;
bool-found=false;
cout用户输入;
对于(无符号整数i=0;i
int i;
for (i=0; i<N; i++)
if (...) {
...
break; // i does not reach N
}
if (i == N) { // never entered ifs in the for loop
inti;
对于(i=0;i一个很好的模式,“过去的方式”这样做:
int i;
for (i=0; i<N; i++)
if (...) {
...
break; // i does not reach N
}
if (i == N) { // never entered ifs in the for loop
inti;
for(i=0;i另一种方法,几乎相当于在for循环中使用break语句
只需在向量中循环,然后在向量外打印结果
unsigned int i = 0;
for(; i < temp.size() && userinput != temp[i].eventID; ++i);
if(i < temp.size() && userinput == temp[i].eventID)
{
cout << "Location: " << temp[i].location << endl;
cout << "Begin Date: " << temp[i].begindate << endl;
....
}
else
{
cout << "it doesnt match" << endl;
}
无符号整数i=0;
对于(;i cout还有另一种方法,几乎相当于在for循环中使用break语句
只需在向量中循环,然后在向量外打印结果
unsigned int i = 0;
for(; i < temp.size() && userinput != temp[i].eventID; ++i);
if(i < temp.size() && userinput == temp[i].eventID)
{
cout << "Location: " << temp[i].location << endl;
cout << "Begin Date: " << temp[i].begindate << endl;
....
}
else
{
cout << "it doesnt match" << endl;
}
无符号整数i=0;
对于(;i 您也可以从if
返回,而不是使用标志。@Bob\uuuuu…如果保证temp
中没有两个元素具有相同的eventID
。您也可以从if
返回,而不是使用标志。@Bob\uuu…如果保证temp
中没有两个元素具有相同的事件ID
。