unique_copy 先排序后copy的困惑

silferlee 2011-11-08 08:34:16
在C++PRIMER中,unique_copy习题11.15中,中说:
算法标准库定义了一个名为 unique_copy 的函数,其
操作与 unique 类似,唯一的区别在于:前者接受第
三个迭代器实参,用于指定复制不重复元素的目标序
列。编写程序使用 unique_copy 将一个 list 对象中
不重复的元素复制到一个空的 vector 对象中。

我看了unique_copy的代码,知道这个copy要建立在排序的基础上,所以在源代码的基础上,添加了sort函数,请注意下面的注释行,但是运行错误,去掉注释行,就能正常运行,还请大侠指教!


#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<algorithm>
#include<iterator>
using namespace std;

int main(){

list<string> lst;
vector<string> svec;
string word;

cout<<"please enter the add words:"<<endl;
while(cin>>word){
lst.push_back(word);
}
cin.clear();

//sort(lst.begin(),lst.end());

unique_copy(lst.begin(),lst.end(),back_inserter(svec));

cout<<"the list words:"<<endl;
for(list<string>::iterator lit=lst.begin();lit!=lst.end();++lit){
cout<<*lit<<" ";
}
cout<<endl;

cout<<"the vector words:"<<endl;
for(vector<string>::iterator vit=svec.begin();vit!=svec.end();++vit){
cout<<*vit<<" ";
}
cout<<endl;
return 0;
}
...全文
41 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
iamnobody 2011-11-08
  • 打赏
  • 举报
回复
list自带sort()算法的意思就是说他不支持std::sort();
無_1024 2011-11-08
  • 打赏
  • 举报
回复
注意有这么一句话 排序算法 这些算法要求随机访问地带器 但是List不能随机访问 必须顺序访问
無_1024 2011-11-08
  • 打赏
  • 举报
回复
换成list的成员函数 lst.sort();

#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<algorithm>
#include<iterator>
using namespace std;

int main(){

list<string> lst;
vector<string> svec;
string word;

cout<<"please enter the add words:"<<endl;
while(cin>>word){
lst.push_back(word);
}
cin.clear();
cin.sync();

//sort(lst.begin(),lst.end());
lst.sort();

unique_copy(lst.begin(),lst.end(),back_inserter(svec));

cout<<"the list words:"<<endl;
for(list<string>::iterator lit=lst.begin();lit!=lst.end();++lit){
cout<<*lit<<" ";
}
cout<<endl;

cout<<"the vector words:"<<endl;
for(vector<string>::iterator vit=svec.begin();vit!=svec.end();++vit){
cout<<*vit<<" ";
}
cout<<endl;
return 0;
}

内容简介回到顶部↑这本书不适合C++ 初学者,不适合 Genericity(泛型技术)初学者,或 STL 初学者。这本书也不适合带领你学习面向对象(Object Oriented)技术 — 是的,STL 与面向对象没有太多关连。本书前言清楚说明了书籍的定位和合适的读者,以及各类基础读物。如果你的Generic Programming/STL实力足以阅读本书所呈现的源码,那么,恭喜,你踏上了基度山岛,这儿有一座大宝库等着你。源码之前了无秘密,你将看到vector的实现、list的实现、heap的实现、deque的实现、RB-tree的实现、hash-table的实现、set/map 的实现;你将看到各种算法(排序、搜寻、排列组合、数据移动与复制…)的实现;你甚至将看到底层的memory pool 和高阶抽象的traits 机制的实现。那些数据结构、那些算法、那些重要观念、那些编程实务中最重要最根本的珍宝,那些蜇伏已久彷佛已经还给老师的记忆,将重新在你的脑中闪闪发光。 目录回到顶部↑庖丁解牛(侯捷自序) i 目录 v 前言 xvii 本书定位 xvii 合适的读者 xviii 最佳阅读方式 xviii 我所选择的剖析对象 xix 各章主题 xx 编译工具 xx 中英术语的运用风格 xxi 英文术语采用原则 xxii 版面字形风格 xxiii 源码形式与下载 xxiv 在线服务 xxvi 推荐读物 xxvi 第1章 STL 概论与版本简介001 1.1 STL 概论 001 1.1.1 STL的历史 003 1.1.2 STL与C++ 标准程序库 003 . 1.2 STL 六大组件 - 功能与运用 004 1.3 GNU源码开放精神 007 1.4 HP STL实现版本 009 1.5 P.J. Plauger STL实现版本 010 1.6 Rouge Wave STL实现版本 011 1.7 STLport 实现版本 012 1.8 SGI STL实现版本 总览 013 1.8.1 GNU C++ header 文件分布 014 1.8.2 SGI STL 文件分布与简介 016 STL 标准头文件(无扩展名) 017 C++ 标准规格定案前,HP规范的STL头文件(扩展名 .h) 017 SGI STL 内部文件(SGI STL真正实现于此) 018 1.8.3 SGI STL 的组态设定(configuration) 019 1.9可能令你困惑的C++ 语法 026 1.9.1 stl_config.h 中的各种组态 027 组态3:static template member 027 组态5:class template partial specialization 028 组态6:function template partial order 028 组态7:explicit function template arguments 029 组态8:member templates 029 组态10:default template argument depend on previous template parameters 030 组态11:non-type template parameters 031 组态:bound friend template function 032 组态:class template explicit specialization 034 1.9.2 临时对象的产生与运用 036 1.9.3 静态常数整数成员在class 内部直接初始化 037 in-class static const integral data member initialization 1.9.4 increment/decrement/dereference 运算子 037 1.9.5 "前闭后开"区间表示法 [ ) 039 1.9.6 function call运算子(operator()) 040 第2章 空间配置器(allocator) 043 2.1 空间配置器的标准接口 043 2.1.1 设计一个简单的空间配置器,JJ::allocator 044 2.2 具备次配置力(sub-allocation)的SGI 空间配置器 047 2.2.1 SGI 标准的空间配置器,std::allocator 047 2.2.2 SGI 特殊的空间配置器,std::alloc 049 2.2.3 构造和析构基本工具:construct() 和 destroy() 051 2.2.4 空间的配置与释

64,654

社区成员

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

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