#include<iostream>
#include<list>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
template <class T>
void display(const T&c,char *label)
{
typename T::const_iterator iter=c.begin();
cout<<label;
for(;iter!=c.end();++iter)
cout<<*iter<<" ";
cout<<endl;
}
int main()
{
vector<int>v1,v2(3),v3(2,8);
display(v1,"v1=");
display(v2,"v2=");
display(v3,"v3=");
list<int> l;
l.push_back(9);
l.push_back(3);
l.push_front(5);
list<int>::iterator itr=l.begin();
l.insert(itr,1);
cout<<"itr points to"<<*itr<<endl;
l.insert(itr,7);
l.insert(itr,3);
set<int> s(l.begin(),l.end());//这里什么东西错了
s.insert(8);
display(1,"1= ");
display(s,"s= ");
cout<<count(1.begin(),l.end(),3)<<"3's exist in l\n";
itr=l.begin();
itr=find(itr,l.end(),7);
if(itr!=l.end())
cout<<"Found"<<*itr<<endl;
else
cout<<"Not found\n";
++itr;
itr=find(itr,l.end(),7);
if(itr!=l.end())
cout<<"Found"<<*itr<<endl;
else
cout<<"Not found\n";
if(binary_search(s.begin(),s.end(),7)==true)
cout<<"7 Found in set with B'Search.\n";
else
cout<<"7 Not found in set with B'Search.\n";
if(binary_search(s.begin(),s.end(),4)==true)
cout<<"4 Not found in set with B'Search.\n";
else
cout<<"4 Not found in set with B'Search.\n";
if(binary_search(s.begin(),l.end(),7)==true)
cout<<"7 Found in list with B'Search.\n";
else
cout<<"7 Not found in list with B'Search.\n";
}
这个帖子编译不能通过,说是定义set时出错,不知道是什么原因
C:\Program Files\Microsoft Visual Studio\MyProjects\s13\s13.cpp(30) : error C2664: '__thiscall std::set<int,struct std::less<int>,class std::allocator<int> >::std::set<int,struct std::less<int>,class std::allocator<int> >(const struct std::less<int>
&,const class std::allocator<int> &)' : cannot convert parameter 1 from 'class std::list<int,class std::allocator<int> >::iterator' to 'const struct std::less<int> &'
Reason: cannot convert from 'class std::list<int,class std::allocator<int> >::iterator' to 'const struct std::less<int>'
No constructor could take the source type, or constructor overload resolution was ambiguous
[此贴子已经被作者于2006-3-23 8:55:53编辑过]