怎样在C++vector容器中输出元素的位置?急求!!
我想最后输出我所输入数字的位置,该怎样写?跪求!!#include<QtCore/QCoreApplication>#include<iostream>usingstd:...
我想最后输出我所输入数字的位置,该怎样写?跪求!!
#include <QtCore/QCoreApplication>
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <vector>
using std::iterator;
using namespace std;
#include <algorithm>
vector<int> vecNumber(22);
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
int n,i;
vecNumber[0] = 0;
vecNumber[1] = 1;
for( i=2; i<22; i++) {
vecNumber[i] = vecNumber[i-1] + vecNumber[i-2];
}
for(vector<int>::iterator it=vecNumber.begin();
it !=vecNumber.end();++it)
{
cout<<*it<<" ";
}
cout<<endl;
cout<<"请输入您要验证的数字:"<<endl;
cin>>n;
vector<int>::iterator it=find(vecNumber.begin(),vecNumber.end(),n);
if(it != vecNumber.end())
{
cout<<"数字找到了"<<endl;
}
else
{
cout<<"抱歉,没有找到您输入的数字!!" <<endl;
}
return a.exec();
} 展开
#include <QtCore/QCoreApplication>
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <vector>
using std::iterator;
using namespace std;
#include <algorithm>
vector<int> vecNumber(22);
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
int n,i;
vecNumber[0] = 0;
vecNumber[1] = 1;
for( i=2; i<22; i++) {
vecNumber[i] = vecNumber[i-1] + vecNumber[i-2];
}
for(vector<int>::iterator it=vecNumber.begin();
it !=vecNumber.end();++it)
{
cout<<*it<<" ";
}
cout<<endl;
cout<<"请输入您要验证的数字:"<<endl;
cin>>n;
vector<int>::iterator it=find(vecNumber.begin(),vecNumber.end(),n);
if(it != vecNumber.end())
{
cout<<"数字找到了"<<endl;
}
else
{
cout<<"抱歉,没有找到您输入的数字!!" <<endl;
}
return a.exec();
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询