C++ reverse_iterator에 대해 여쭙고자 합니다.

greathero의 이미지

ESTL을 보다가 궁금증이 풀리지 않아 글을 올립니다.
아래와 같은 코드가 있습니다.

vector<int> v;
v.reserve(5);
 
// v에 숫자 1 ~ 5까지 삽입
 
vector<int>::reverse_iterator rit = find(v.rbegin(), v.rend(), 3); 
 
vector<int>::iterator it(rit.base());

reverse_iterator를 iterator로 바꾸는 과정인데요.
이 코드를 실행시키고 난 후엔 아래 그림처럼 된다고 합니다.

re rit rb
| 1 | 2 | 3 | 4 | 5
b it e

위 그림에서 re는 rend(), rb는 rbegin(), b는 begin(), e는 end()를 뜻합니다.
re, rb, b, e의 포지션은 당연히 이해가 갑니다.

제가 이해 되지 않는 것은 it의 포지션입니다.
it를 rit.base()로 초기화 했는데 왜 rit와 같은 3을 가리키는 게 아니라 4를 가리킬까요?

greathero의 이미지

http://kldp.org/files/box.png <- 이거 클릭하시면 잘 보여요~~~

댓글 첨부 파일: 
첨부파일 크기
Image icon box.png4.11 KB
익명 사용자의 이미지

&*(reverse_iterator(i) == &*(i-1)
이라고 알고있습니다.
다시 얘기하면 &*ri == &*(ri.base() -1) 로 표현하구요
구간 [f,l) 범위는 f 에서 l-1까지죠?
근데 reverse_iterator(l),reverse_iterator(f) 와 동일하면서도 요소가 반대인 요소를
담아야 하자나요.
그럼 생각을 해보면 [0,5) 에서 first가 0 , last 가 5면
실제로 참조하는값은 0,1,2,3,4 가 됩니다.
근데 reverse_iterator(5) 가 마지막 방을 가르켜야 요소를 반대로 참조할수 있겟죠?
그럼 reverse_iterator(5) == iterator(4) 와 같다는 말이 되어야 하고
여기에서 보면 인덱스가 하나가 차이가 날 수밖에 없습니다.
그래서 베이스 보다 인덱스 하나 전인 3을 가르킬때 base는 4를 가르키고 있는겁니다..
좀 이상한것도 같고... 제가 이해한 reverse_iterator가 이게 맞는지는 잘 모르겠습니다만....
저도 이정도로밖에 설명을... ㅜㅜ 죄송 ㅜ
화이팅이요!!!

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.