声明一个整型数组,使用c++标准模板中的查找算法find()进行数据的查找,然后应用排
2个回答
展开全部
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
template <class Type>
void print_elements( Type elem ) { cout << elem << " "; }
void (*pfi)( int ) = print_elements;
int main ()
{
int a[]={0,1,2,3,4,5,6,7,8,9};
const int size = sizeof(a)/sizeof(*a);
vector<int> v(a,a+size);
for_each(v.begin(),v.end(),pfi);
cout<<endl;
int fv = a[3];
vector<int>::iterator it = find(v.begin(),v.end(),fv);
cout << "find the first occurrence of " << fv << "\t"
<< ( it!=v.end() ? "found!\n" : "not found!\n" );
sort(v.begin(),v.end());
for_each(v.begin(),v.end(),pfi);cout<<endl;
sort(v.begin(),v.end(),greater<int>());
for_each(v.begin(),v.end(),pfi);cout<<endl;
sort(v.begin(),v.end(),less<int>());
for_each(v.begin(),v.end(),pfi);cout<<endl;
return 0;
}
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
template <class Type>
void print_elements( Type elem ) { cout << elem << " "; }
void (*pfi)( int ) = print_elements;
int main ()
{
int a[]={0,1,2,3,4,5,6,7,8,9};
const int size = sizeof(a)/sizeof(*a);
vector<int> v(a,a+size);
for_each(v.begin(),v.end(),pfi);
cout<<endl;
int fv = a[3];
vector<int>::iterator it = find(v.begin(),v.end(),fv);
cout << "find the first occurrence of " << fv << "\t"
<< ( it!=v.end() ? "found!\n" : "not found!\n" );
sort(v.begin(),v.end());
for_each(v.begin(),v.end(),pfi);cout<<endl;
sort(v.begin(),v.end(),greater<int>());
for_each(v.begin(),v.end(),pfi);cout<<endl;
sort(v.begin(),v.end(),less<int>());
for_each(v.begin(),v.end(),pfi);cout<<endl;
return 0;
}
展开全部
花3分钟写的,跟你的要求一样
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool largeThan(int x,int y)
{
return x>y;
}
int main()
{
int a[7]={1,2,3,4,5,6,7};
size_t arrSize=7;
int searchValue=5;
vector<int> vec(a,a+arrSize);
vector<int>::iterator it=find(vec.begin(),vec.end(),searchValue);
if(it==vec.end())
cout<<"not found"<<endl;
else
cout<<searchValue<<"'s index is "<<(it-vec.begin())<<endl;
//升序
sort(vec.begin(),vec.end());
for(vector<int>::iterator it=vec.begin();it!=vec.end();it++)
cout<<*it<<ends;
cout<<endl;
//降序;
sort(vec.begin(),vec.end(),largeThan);
for(vector<int>::iterator it=vec.begin();it!=vec.end();it++)
cout<<*it<<ends;
cout<<endl;
}
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool largeThan(int x,int y)
{
return x>y;
}
int main()
{
int a[7]={1,2,3,4,5,6,7};
size_t arrSize=7;
int searchValue=5;
vector<int> vec(a,a+arrSize);
vector<int>::iterator it=find(vec.begin(),vec.end(),searchValue);
if(it==vec.end())
cout<<"not found"<<endl;
else
cout<<searchValue<<"'s index is "<<(it-vec.begin())<<endl;
//升序
sort(vec.begin(),vec.end());
for(vector<int>::iterator it=vec.begin();it!=vec.end();it++)
cout<<*it<<ends;
cout<<endl;
//降序;
sort(vec.begin(),vec.end(),largeThan);
for(vector<int>::iterator it=vec.begin();it!=vec.end();it++)
cout<<*it<<ends;
cout<<endl;
}
更多追问追答
追问
额?有一个错误》Files\Microsoft Visual Studio\MyProjects\e\1.cpp(23) : error C2374: 'it' : redefinition; multiple initialization
追答
你把 vector::iterator it=find(vec.begin(),vec.end(),searchValue); 换一个名字即可(和后面的 for(vector::iterator it=vec.begin();it!=vec.end();it++)重复了,或者直接把后面的定义去掉。
还有,局部变量覆盖全局变量没什么问题,我的正常运行。你的肯定不是vs2010
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |