iterator를 반환하는 함수가 가능할까요?
글쓴이: dltkddyd / 작성시간: 금, 2013/11/08 - 7:24오후
애래의
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; }
Forums:
레퍼런스로 해결했습니다.
레퍼런스 사용하는 것을 좋아하지는 않지만 레퍼런스를 사용하니 되긴 하네요..
본인 맞습니다.
인증샷
우헤헤헤... 로 대신합니다.
댓글 달기