有关Iterators and Reverse Iterators的程序

faye_2004 2004-10-05 05:04:09
#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;
void print (int elem)
{
cout << elem << ' ';
}
int main()
{
deque<int> coll;
for (int i=1; i<=9; ++i) {
coll.push_back(i);
}

deque<int>::iterator pos1;
pos1 = find (coll.begin(), coll.end(), 2);
deque<int>::iterator pos2;
pos2 = find (coll.begin(), coll.end(),7);
for_each (pos1, pos2, print);
cout << endl;
deque<int>::reverse_iterator rpos1(pos1);
deque<int>::reverse_iterator rpos2(pos2);
for.each (rpos2, rpos1, print);
cout << endl;
}
the output of the program is as follows:
2 3 4 5 6
6 5 4 3 2
为什么输出是6 5 4 3 2,如何将Iterators范围转化为Reverse Iterators

...全文
130 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
nlstone 2004-10-15
  • 打赏
  • 举报
回复
详见任何一本解释相关内容(逆迭代器)的书,譬如c++ standard library
简单地说,逆迭代器的内容是取其前一个元素的值.譬如说如果一个Reverse Iterator指向第五个元素,那么它的内容(譬如用*来访问,这是重载过的),会去取第四个元素的值.
这是为了使得find一类的算法不需要做任何改变即可应付逆迭代器(否则因为有半开区间的影响,逆迭代器find()的话就需要做更改,要么改find算法,要么改动传入find()的逆迭代器的位置)
faye_2004 2004-10-12
  • 打赏
  • 举报
回复
#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;
void print (int elem)
{
cout << elem << ' ';
}
int main()
{
deque<int> coll;
for (int i=1; i<=9; ++i) {
coll.push_back(i);
}

deque<int>::iterator pos1;
pos1 = find (coll.begin(), coll.end(), 2);
deque<int>::iterator pos2;
pos2 = find (coll.begin(), coll.end(),7);
for_each (pos1, pos2, print);
cout << endl;
deque<int>::reverse_iterator rpos1(pos1);
deque<int>::reverse_iterator rpos2(pos2);
for.each (rpos2, rpos1, print);
cout << endl;
}
the output of the program is as follows:
2 3 4 5 6
6 5 4 3 2
为什么输出是6 5 4 3 2,如何将Iterators范围转化为Reverse Iterators
另一程序
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> coll;
for (int i=1; i<=9; ++i) {
coll.push_back(i);}
vector<int>::iterator pos;
pos = find (coll.begin(), coll.end(),5);
cout << "pos: " << *pos << endl;
vector<int>::reverse_iterator rpos(pos);
cout << "rpos: " << *rpos <<endl;
}
输出为
pos: 5
rpos: 4为什么?


UPCCPU 2004-10-07
  • 打赏
  • 举报
回复
上面的问题可以看一看STL源代码:
template<class _RI,
class _Ty,
class _Rt = _Ty&,
class _Pt = _Ty *,
class _D = ptrdiff_t>
class reverse_iterator : public _Ranit<_Ty, _D> {
public:
...
_Rt operator*() const
{return (*(current - 1)); }
...
}
这是visual c++6.0中的reverse_iterator模板类的代码片断。
faye_2004 2004-10-07
  • 打赏
  • 举报
回复
另一程序
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> coll;
for (int i=1; i<=9; ++i) {
coll.push_back(i);}
vector<int>::iterator pos;
pos = find (coll.begin(), coll.end(),5);
cout << "pos: " << *pos << endl;
vector<int>::reverse_iterator rpos(pos);
cout << "rpos: " << *rpos <<endl;
}
输出为
pos: 5
rpos: 4为什么?
prozax 2004-10-05
  • 打赏
  • 举报
回复
不是很对吗 有什么问题?
zhouwander 2004-10-05
  • 打赏
  • 举报
回复
在反向型迭代器中维护着一个随机迭代器,重载了++和--运算符也就变成反向的了,deque中的迭代器就是随机的。
_Myt& operator++()
{ // preincrement
--current;
return (*this);
}

_Myt operator++(int)
{ // postincrement
_Myt _Tmp = *this;
--current;
return (_Tmp);
}

_Myt& operator--()
{ // predecrement
++current;
return (*this);
}

_Myt operator--(int)
{ // postdecrement
_Myt _Tmp = *this;
++current;
return (_Tmp);
}
fangrk 2004-10-05
  • 打赏
  • 举报
回复
你不是已经转换了吗?
Table of Contents Preface 1 Chapter 1: Shell Something Out 7 Introduction 7 Printing in the terminal 9 Playing with variables and environment variables 12 Doing math calculations with the shell 17 Playing with file descriptors and redirection 19 Arrays and associative arrays 25 Visiting aliases 27 Grabbing information about terminal 29 Getting, setting dates, and delays 30 Debugging the script 33 Functions and arguments 35 Reading the output of a sequence of commands 38 Reading "n" characters without pressing Return 40 Field separators and iterators 41 Comparisons and tests 44 Chapter 2: Have a Good Command 49 Introduction 50 Concatenating with cat 50 Recording and playback of terminal sessions 53 Finding files and file listing 55 Playing with xargs 63 Translating with tr 69 Checksum and verification 72 Sorting, unique and duplicates 75 Temporary file naming and random numbers 80 Splitting files and data 81 ii Table of Contents Slicing file names based on extension 84 Renaming and moving files in bulk 86 Spell checking and dictionary manipulation 89 Automating interactive input 90 Chapter 3: File In, File Out 95 Introduction 96 Generating files of any size 96 Intersection and set difference (A-B) on text files 97 Finding and deleting duplicate files 100 Making directories for a long path 103 File permissions, ownership, and sticky bit 104 Making files immutable 109 Generating blank files in bulk 110 Finding a symbolic link and its target 111 Enumerating file type statistics 113 Loopback files and mounting 115 Creating ISO files, Hybrid ISO 117 Finding difference between files, patching 120 head and tail – printing the last or first 10 lines 122 Listing only directories – alternative methods 125 Fast command-line navigation using pushd and popd 126 Counting number of lines, words, and characters in a file 128 Printing directory tree 129 Chapter 4: Texting and Driving 131 Introduction 132 Basic regular expression primer 132 Searching and mining "text" inside a file with grep 136 Column-wise cutting of a file with cut 142 Frequency of words used in a given file 146 Basic sed primer 147 Basic awk primer 150 Replacing strings from a text or file 156 Compressing or decompressing JavaScript 158 Iterating through lines, words, and characters in a file 161 Merging multiple files as columns 162 Printing the nth word or column in a file or line 163 Printing text between line numbers or patterns 164 Checking palindrome strings with a script 165 Printing lines in the reverse order 169 Parsing e-mail addresses and URLs from text 171 iii Table of Contents Printing n lines before or after a pattern in a file 172 Removing a sentence in a file containing a word 174 Implementing head, tail, and tac with awk 175 Text slicing and parameter operations 177 Chapter 5: Tangled Web? Not At All! 179 Introduction 180 Downloading from a web page 180 Downloading a web page as formatted plain text 183 A primer on cURL 183 Accessing Gmail from the command line 188 Parsing data from a website 189 Image crawler and downloader 191 Web photo album generator 193 Twitter command-line client 195 define utility with Web backend 197 Finding broken links in a website 199 Tracking changes to a website 200 Posting to a web page and reading response 203 Chapter 6: The Backup Plan 205 Introduction 205 Archiving with tar 206 Archiving with cpio 211 Compressing with gunzip (gzip) 212 Compressing with bunzip (bzip) 215 Compressing with lzma 217 Archiving and compressing with zip 219 squashfs – the heavy compression filesystem 220 Cryptographic tools and hashes 222 Backup snapshots with rsync 224 Version control based backup with Git 227 Cloning hard drive and disks with dd 230 Chapter 7: The Old-boy Network 233 Introduction 233 Basic networking primer 234 Let's ping! 241 Listing all the machines alive on a network 243 Transferring files 247 Setting up an Ethernet and wireless LAN with script 250 Password-less auto-login with SSH 253 Running commands on remote host with SSH 255

24,856

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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