iterator를 반환하는 함수가 가능할까요?

dltkddyd의 이미지

애래의

list::iterator output_list_data(list::iterator first, list::iterator nexttolast)

함수좀 점검해주세요. 이 함수는 list로 생성한 자료구조를 특정 범위의 반복자로 출력을 하고 마지막 반복자를 반환하는 함수입니다. 그런데 제가 알기로 지역객체는 반환시 소멸자가 호출되기 때문에 반환을 받지 못하는 것입니다. 그런데 output_list_data의 지역객체 pos가 반환이 되긴 합니다. 그런데 반환해서 출력해보면 그 index 값은 3이 됩니다. 반환되는 반복자는 자료구조의 마지막을 가리키기 때문에 값이 출력되면 안되는 것이죠? 여하튼 마지막 반복자를 제대로 반환받고 싶은데, 어떻게 해야 하나요? 지역변수인 pos를 반환받는 방법이 있을까요? 아니면 그 마지막 반복자를 다른 방식으로 반환받는 방법이 있을까요?

#include <iostream>
using namespace std;
#include <list>
#include <string>
struct Books {
	string title;
	string author;
	string publisher;
	string publishingDate;
	int index;//책 등록번호
	Books(string title, string author, string publisher, string publishingDate, int index) {
		this->title=title;
		this->author=author;
		this->publisher=publisher;
		this->publishingDate=publishingDate;
		this->index=index;
	}
};
 
list<Books>::iterator output_list_data(list<Books>::iterator first, list<Books>::iterator nexttolast) {
	list<Books>::iterator pos;
	for(	pos=first;pos!=nexttolast;++pos) {
		cout<<"==============================================="<<endl;
		cout<<"title is "<<pos->title<<endl;
		cout<<"author is "<<pos->author<<endl;
		cout<<"publisher is "<<pos->publisher<<endl;
		cout<<"publishingDate is "<<pos->publishingDate<<endl;
		cout<<"index is "<<pos->index<<endl;
		cout<<"==============================================="<<endl;
	}
	list<Books>::iterator* replacing_pos=new list<Books>::iterator;
	(*replacing_pos)=nexttolast;
	//cout<<"in output_list_data, "<<(--pos)->index;
        //cout<<(--(*replacing_pos))->index;
	//cout<<(--nexttolast)->index<<endl;//test
	//return replacing_pos;
	return pos;
}
 
int main() {
	list<Books> ProgLibrary;
 
	//push_back()
	ProgLibrary.push_back(Books(string("The forgotten time"),string("Dikkins Chars"),string("Dream Co"),string("2011.11.5"),3));
	ProgLibrary.push_back(Books("The wind blows suddenly","Verboss Jack","Doctor Duck","2012.5.8",4));
	ProgLibrary.push_back(Books("The computers has existed","Computer Man","About Computer","2009.5.9",5));
	ProgLibrary.push_back(Books("The river has flowed forever","Tobbin Who","History River","20013.8.23",6));
 
	//push_front()
	ProgLibrary.push_front(Books("The head refuses the body","Why Huston","Really Doctor","2009.4.7",2));
	ProgLibrary.push_front(Books("The wings can't be used for step","Eagle Man","Animal Nest","2005.8.19",1));
 
	//iterator
	for(list<Books>::iterator pos1=ProgLibrary.begin();pos1!=ProgLibrary.end();++pos1) {
		cout<<"==============================================="<<endl;
		cout<<"title is "<<pos1->title<<endl;
		cout<<"author is "<<pos1->author<<endl;
		cout<<"publisher is "<<pos1->publisher<<endl;
		cout<<"publishingDate is "<<pos1->publishingDate<<endl;
		cout<<"index is "<<pos1->index<<endl;
		cout<<"==============================================="<<endl;
	}
 
	//pop_back()
	cout<<"pop_back testing"<<endl;
	ProgLibrary.pop_back();
	list<Books>::iterator confirm=output_list_data(ProgLibrary.begin(),ProgLibrary.end());
	//--confirm;
	cout<<(confirm)->index<<(confirm)->title<<endl;
	//delete confirm;
	/*if(confirm->index!=6) {
		cout<<"The pop_back() is success."<<endl;
	}
	else {
		cout<<"The pop_back() has failed."<<endl;
	}*/
 
	return 0;
}
dltkddyd의 이미지

레퍼런스 사용하는 것을 좋아하지는 않지만 레퍼런스를 사용하니 되긴 하네요..

본인 맞습니다.
인증샷
우헤헤헤... 로 대신합니다.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.