首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

一个STL有关问题

2013-12-28 
一个STL问题#include list#include set#include algorithm#include iostreamusing namespace std

一个STL问题

#include <list>
#include <set>
#include <algorithm>
#include <iostream>

using namespace std;

struct compare
{
bool operator() (const int* i1, const int* i2)const
{
return *i1 < *i2;
}
};

void print(int* i)
{
cout << " " << *i;
}

int main(int, char*[])
{
list<int*> list1;
for(int i = 0; i < 5; ++i)
{
list1.push_back(new int(i * i));
}
cout << "List of int* : (";
for_each(list1.begin(), list1.end(), print);
cout << ")" << endl;

set<int*, compare> set1;
copy(list1.begin(), list1.end(), insert_iterator<set<int*, compare>> (set1, set1.begin()));
cout << "Set of int* : [";
for_each(set1.begin(), set1.end, print);
cout << "]" <<endl;

return 0;
}
我在编译这段代码的时候在copy函数那行出错了,不知道为啥,求指点,错误信息如下:
error C2665: 'set<int *,struct compare,class std::allocator<int *> >::set<int *,struct compare,class std::allocator<int *> >' : none of the 3 overloads can convert parameter 1 from type 'class std::set<in
t *,struct compare,class std::allocator<int *> >'
[解决办法]
1.第35行修改一下
for_each(set1.begin(), set1.end(), print);

2.我用vs05编译的,没有出现编译和运行错误

[解决办法]
STL中的copy和foreach都有用错,先查阅STL的基本资料。

引用:
#include <list>
#include <set>
#include <algorithm>
#include <iostream>

using namespace std;

struct compare
{
bool operator() (const int* i1, const int* i2)const
{
return *i1 < *i2;
}
};

void print(int* i)
{
cout << " " << *i;
}

int main(int, char*[])
{
list<int*> list1;
for(int i = 0; i < 5; ++i)
{
list1.push_back(new int(i * i));
}
cout << "List of int* : (";
for_each(list1.begin(), list1.end(), print);
cout << ")" << endl;

set<int*, compare> set1;
copy(list1.begin(), list1.end(), insert_iterator<set<int*, compare>> (set1, set1.begin()));
cout << "Set of int* : [";
for_each(set1.begin(), set1.end, print);
cout << "]" <<endl;

return 0;
}
我在编译这段代码的时候在copy函数那行出错了,不知道为啥,求指点,错误信息如下:
error C2665: 'set<int *,struct compare,class std::allocator<int *> >::set<int *,struct compare,class std::allocator<int *> >' : none of the 3 overloads can convert parameter 1 from type 'class std::set<in
t *,struct compare,class std::allocator<int *> >'

[解决办法]
end后少括号。。这种错仔细看看就能发现吧。

热点排行