| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 770 人关注过本帖
标题:紧急求助
只看楼主 加入收藏
surivering
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2005-3-5
收藏
 问题点数:0 回复次数:4 
紧急求助

#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编辑过]

搜索更多相关主题的帖子: display include 
2006-03-21 21:21
柳儿
Rank: 6Rank: 6
等 级:贵宾
威 望:25
帖 子:1830
专家分:30
注 册:2004-9-23
收藏
得分:0 
set<int> s(l.begin(),l.end());//这里什么东西错了
s.insert;


你这个下面那句没写完整阿??

成功会使人骄傲。如果你骄傲自大,你就会停止学习。不学习,人就停止了进步
2006-03-22 09:45
rogerer
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-3-22
收藏
得分:0 
template<class Key, class Pred = less<Key>, class A = allocator<Key> >
    class set {
public:
    typedef Key key_type;
    typedef Pred key_compare;
    typedef Key value_type;
    typedef Pred value_compare;
    typedef A allocator_type;
    typedef A::size_type size_type;
    typedef A::difference_type difference_type;
    typedef A::rebind<value_type>::other::const_reference reference;
    typedef A::rebind<value_type>::other::const_reference const_reference;
    typedef T0 iterator;
    typedef T1 const_iterator;
    typedef reverse_bidirectional_iterator<iterator,
        value_type, reference, A::pointer,
            difference_type> reverse_iterator;
    typedef reverse_bidirectional_iterator<const_iterator,
        value_type, const_reference, A::const_pointer,
            difference_type> const_reverse_iterator;
    explicit set(const Pred& comp = Pred(), const A& al = A());
    set(const set& x);
    set(const value_type *first, const value_type *last,
        const Pred& comp = Pred(), const A& al = A());
    const_iterator begin() const;
    iterator end() const;
    const_reverse_iterator rbegin() const;
    const_reverse_iterator rend() const;
    size_type size() const;
    size_type max_size() const;
    bool empty() const;
    A get_allocator() const;
    pair<iterator, bool> insert(const value_type& x);
    iterator insert(iterator it, const value_type& x);
    void insert(InIt first, InIt last);
    iterator erase(iterator it);
    iterator erase(iterator first, iterator last);
    size_type erase(const Key& key);
    void clear();
    void swap(set x);
    key_compare key_comp() const;
    value_compare value_comp() const;
    const_iterator find(const Key& key) const;
    size_type count(const Key& key) const;
    const_iterator lower_bound(const Key& key) const;
    const_iterator upper_bound(const Key& key) const;
    pair<const_iterator, const_iterator>
        equal_range(const Key& key) const;
protected:
    A allocator;
    };

The template class describes an object that controls a varying-length sequence of elements of type const Key. Each element serves as both a sort key and a value. The sequence is represented in a way that permits lookup, insertion, and removal of an arbitrary element with a number of operations proportional to the logarithm of the number of elements in the sequence (logarithmic time). Moreover, inserting an element invalidates no iterators, and removing an element invalidates only those iterators that point at the removed element.

The object orders the sequence it controls by calling a stored function object of type Pred. You access this stored object by calling the member function key_comp(). Such a function object must impose a total order on sort keys. For any element x that precedes y in the sequence, key_comp()(y, x) is false. (For the default function object less<Key>, sort keys never decrease in value.) Unlike template class multiset, an object of template class set ensures that key_comp()(x, y) is true. (Each key is unique.)

The object allocates and frees storage for the sequence it controls through a protected object named allocator, of class A. Such an allocator object must have the same external interface as an object of template class allocator. Note that allocator is not copied when the object is assigned.

不知道,希望这个对你有所帮助!!!

2006-03-22 10:57
surivering
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2005-3-5
收藏
得分:0 

好了,改过了,还是不能通过,下面的那个英文看不懂啊

2006-03-22 11:22
柳儿
Rank: 6Rank: 6
等 级:贵宾
威 望:25
帖 子:1830
专家分:30
注 册:2004-9-23
收藏
得分:0 
把代码和编译错误贴出来

成功会使人骄傲。如果你骄傲自大,你就会停止学习。不学习,人就停止了进步
2006-03-22 11:36
快速回复:紧急求助
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023148 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved