리스트에 대해서 질문합니다.

BrownBear의 이미지

class ABCDE
{
private:
list ListStorage;

public:
ABCDE();
~ABCDE();
};

리스트를 private에서 변수로 선언했을 때 ~ABCDE에서 ListStorage.clear(); 즉 리스트를 굳이 클리어 해줄필요가 없나요?

두번째로 리스트에 대한 iterator의 유효성을 검사하려고합니다.

그런데 이게

list::iterator ListIterator;

if(ListIterator == NULL); //컴파일 오류

리스트 Iterator에 대한 NULL을 검사하는 방법이 없나요?

임시적으로 *(unsigned char*)(&Iterator) == NULL 이런식으로 하고 있기는 한데 이게 검증이 안된 방법이라 안전한지 모르겠네요.

kukyakya의 이미지

해줄 필요 없습니다. ABCDE가 destruct될 때 list 또한 같이 destruct됩니다.

iterator NULL 검사가 뭘 원하시는 건지 잘 모르겠지만 iterator의 validity를 검증을 하고 싶으신거라면 딱히 방법이 없습니다. 다만 iterator가 가리키는 원소 자체가 삭제되지 않는다면 list의 iterator는 항상 유효합니다.

*(unsigned char*)(&Iterator) == NULL은 iterator의 validity와는 상관이 없습니다.

BrownBear의 이미지

그러니깐 Iterator의 NULL은 만약

typedef struct STRUCT_NAME
{
DWORD Id = 0;
} _STRUCT_NAME, *PSTRUCT_NAME;

list "STRUCT_NAME" StructNameStorage; //다음과 같은 변수가 있고 //태그로 인식되서 씹혀서 "로 대신합니다.

아래와 같은 함수가 있을 때

list<STRUCT_NAME>::iterator GetIterator(DWORD Id)
{
 list<STRUCT_NAME>::iterator Iterator_End = StructNameStorage.end();
 list<STRUCT_NAME>::iterator CurrentIterator; 
 
 for(list<STRUCT_NAME>::iterator Iterator_Pos = StructNameStorage.begin(); Iterator_Pos != Iterator_End; ++Iterator_Pos)
 {
  if(Iterator_Pos->Id == Id)
  {
   return Iterator_Pos;
  }
 }
 
 return CurrentIterator;
}

문제는 iterator를 찾지 못하여 반환을 할때 NULL로 반환을 하려고 하면 그런 형식으로 반환할 수 읍다고 궁시렁 대더군요 컴파일러가.

그래서 어쩔 수 없이 아무것도 없는 iterator를 반환하는데 어떻게 해야 하나요 (찾지 못했을 경우 선언만 달랑되있는 iterator 반환, 이것을 사용하려고 할 시 오류 발생)

아니면 이렇게 함수를 이용해서 iterator를 반환하는 예시가 없는건지요?

kukyakya의 이미지

그럴땐 list::end()를 반환하시면 됩니다.

begin()이 첫 원소를 가리키는 iterator를 반환하는 것과는 다르게,
end()는 마지막 원소의 "다음" 원소를 가리키는 iterator를 반환합니다.

BrownBear의 이미지

아 그렇다면 list.end로 반환된 iterator이 "유효하지 않다면"

if(iterator == list.end())
{
//유효하지 않다! 가 되겠군요?
}

kukyakya의 이미지

iterator가 list가 반환한 iterator라면 그렇습니다.

만약 다른 list2가 있고 iterator2 = list2.end()라면, iterator2 != list.end()지만 iterator2가 list의 원소를 가리키지도, dereferenceable하지도 않겠죠.

BrownBear의 이미지

list A에 대해

A에 5개의 원소가 들어가있을때 end를 반환한것과

6개 원소가 들어있을 때 end를 반환한것과 다른가요?

익명 사용자의 이미지

같다고 나오네요

5개가 들어있을때의 end와 6개가 들어있을때의 end가 같은데 어째서 이런건지

아니면 특정한 값을 똑같이 반환하는지 궁금하네영

jick의 이미지

"음 그럴리가?" 하고 찾아보니... 오오 놀랍게도 list의 iterator는 아이템을 넣건 빼건 유효성이 유지되는군요! (훌륭한데?)

http://en.cppreference.com/w/cpp/container/list

Addition, removal and moving the elements within the list or across several lists does not invalidate the iterators or references. An iterator is invalidated only when the corresponding element is deleted.

http://stackoverflow.com/questions/14647007/does-stdlistclear-invalidate-stdlistend-iterator

Other answers point out that, in general, you can't rely on a container's past-the-end iterator remaining valid when the container is cleared. However, the past-the-end iterator of a list should indeed remain valid:

C++11 23.3.5.4/3 Effects: Invalidates only the iterators and references to the erased elements.

The past-the-end iterator does not refer to any element, so should not be invalidated.

twinwings의 이미지

메모리가 불연속적으로 할당받는 컨테이너는 모두 유지 될 겁니다.

std::set, std::map, std::list 등이요.

하지만 연속적으로 할당받는 컨테이너인 std::vector같은 경우 아이템이 추가 될 경우, 메모리가 모자라서 통째로 새로 받는 경우가 생길 수 있기 때문에 유효성을 보장할 수 없죠

hsnks100의 이미지

흠................ class... list... std::list... atoi... strcpy...

----------------------------------------------------
개인 블로그: https://kangssu.com

댓글 달기

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