unique_copy问题

tan625747 2010-07-21 09:40:39
《c++ 程序设计语言》

#include <iostream>
#include <fstream>
#include <string>
#include <iterator>
#include <vector>
#include <algorithm>

using namespace std;
ostream_iterator<string> oo(cout);
istream_iterator<string> ii(cin);
istream_iterator<string> eos;

int main()
{
string from,to;
cin>>from>>to;

ifstream is(from.c_str());
istream_iterator<string>ii(is);
istream_iterator<string>eos;

vector<string>b(ii,eos);
sort(b.begin(),b.end());

ofstream os(to.c_str());
ostream_iterator<string>oo(os,"\n");

unique_copy(b.begin(),b.end(),00);
return !is.eof()|| !os;
}


编译器:vs2010

错误
:\program files\microsoft visual studio 10.0\vc\include\xutility(2227): warning C4996: 'std::_Copy_impl': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
1> c:\program files\microsoft visual studio 10.0\vc\include\xutility(2212) : see declaration of 'std::_Copy_impl'
1> c:\documents and settings\administrator\my documents\visual studio 2010\projects\the c++ programming language\the c++ programming language\io.cpp(41) : see reference to function template instantiation '_OutIt std::copy<std::_Vector_iterator<_Myvec>,int>(_InIt,_InIt,_OutIt)' being compiled
1> with
1> [
1> _OutIt=int,
1> _Myvec=std::_Vector_val<std::string,std::allocator<std::string>>,
1> _InIt=std::_Vector_iterator<std::_Vector_val<std::string,std::allocator<std::string>>>
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(2216): error C2665: 'std::_Copy_impl' : none of the 2 overloads could convert all the argument types
1> c:\program files\microsoft visual studio 10.0\vc\include\xutility(2182): could be '_OutIt std::_Copy_impl<_InIt,_OutIt>(_InIt,_InIt,_OutIt,std::input_iterator_tag,std::output_iterator_tag)'
1> with
1> [
1> _OutIt=int,
1> _InIt=std::basic_string<char,std::char_traits<char>,std::allocator<char>> *
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\xutility(2191): or '_OutIt std::_Copy_impl<_InIt,_OutIt>(_InIt,_InIt,_OutIt,std::random_access_iterator_tag,std::random_access_iterator_tag)'
1> with
1> [
1> _OutIt=int,
1> _InIt=std::basic_string<char,std::char_traits<char>,std::allocator<char>> *
1> ]
1> while trying to match the argument list '(std::basic_string<_Elem,_Traits,_Ax> , std::basic_string<_Elem,_Traits,_Ax> , int, std::iterator_traits<_Iter>::iterator_category, std::iterator_traits<int>::iterator_category)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> and
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> and
1> [
1> _Iter=std::basic_string<char,std::char_traits<char>,std::allocator<char>> *
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\xutility(2227) : see reference to function template instantiation '_OutIt std::_Copy_impl<std::basic_string<_Elem,_Traits,_Ax>*,_OutIt>(_InIt,_InIt,_OutIt,std::tr1::false_type)' being compiled
1> with
1> [
1> _OutIt=int,
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>,
1> _InIt=std::basic_string<char,std::char_traits<char>,std::allocator<char>> *
1> ]
1> c:\documents and settings\administrator\my documents\visual studio 2010\projects\the c++ programming language\the c++ programming language\io.cpp(41) : see reference to function template instantiation '_OutIt std::copy<std::_Vector_iterator<_Myvec>,int>(_InIt,_InIt,_OutIt)' being compiled
1> with
1> [
1> _OutIt=int,
1> _Myvec=std::_Vector_val<std::string,std::allocator<std::string>>,
1> _InIt=std::_Vector_iterator<std::_Vector_val<std::string,std::allocator<std::string>>>
1> ]
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.92
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
...全文
136 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
aizibion 2010-07-21
  • 打赏
  • 举报
回复
将楼主
unique_copy(b.begin(),b.end(),00);
改为
unique_copy(b.begin(),b.end(),oo);
后,
vs2003:
------ 已启动全部重新生成: 项目: test, 配置: Debug Win32 ------

正在删除项目“test”(配置“Debug|Win32”)的中间文件和输出文件。
正在编译...
test.cpp
正在链接...

生成日志保存在“file://e:\Projects\Vs2003\test\test\Debug\BuildLog.htm”中
test - 0 错误,0 警告


---------------------- 完成 ---------------------

全部重新生成: 1 已成功, 0 已失败, 0 已跳过



vs2008
1>------ 已启动全部重新生成: 项目: test, 配置: Debug Win32 ------
1>正在删除项目“test”(配置“Debug|Win32”)的中间文件和输出文件
1>正在编译...
1>main.cpp
1>正在编译资源清单...
1>Microsoft (R) Windows (R) Resource Compiler Version 6.0.5724.0
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>正在链接...
1>正在嵌入清单...
1>Microsoft (R) Windows (R) Resource Compiler Version 6.0.5724.0
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>生成日志保存在“file://d:\My Documents\Visual Studio 2008\Projects\test\test\Debug\BuildLog.htm”
1>test - 0 个错误,0 个警告
========== 全部重新生成: 成功 1 个,失败 0 个,跳过 0 个 ==========
tan625747 2010-07-21
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hqin6 的回复:]

unique_copy(b.begin(),b.end(),00);-------oo吧~!
[/Quote]]

谢了,看半天没有看出来
太乙 2010-07-21
  • 打赏
  • 举报
回复
unique_copy(b.begin(),b.end(),00);-------oo吧~!
太乙 2010-07-21
  • 打赏
  • 举报
回复
// unique_copy example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool myfunction (int i, int j) {
return (i==j);
}

int main () {
int myints[] = {10,20,20,20,30,30,20,20,10};
vector<int> myvector (9); // 0 0 0 0 0 0 0 0 0
vector<int>::iterator it;

// using default comparison:
it=unique_copy (myints,myints+9,myvector.begin()); // 10 20 30 20 10 0 0 0 0
// ^

sort (myvector.begin(),it); // 10 10 20 20 30 0 0 0 0
// ^

// using predicate comparison:
it=unique_copy (myvector.begin(), it, myvector.begin(), myfunction);
// 10 20 30 20 30 0 0 0 0
// ^

myvector.resize( it - myvector.begin() ); // 10 20 30

// print out content:
cout << "myvector contains:";
for (it=myvector.begin(); it!=myvector.end(); ++it)
cout << " " << *it;

cout << endl;

return 0;
}
饭fan有引力 2010-07-21
  • 打赏
  • 举报
回复
书错了,看的书少,不好意思

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧