[C++] iterator 반환값을 모르겠습니다

kldpzone의 이미지

	deque<int>::iterator atIt1 ;
	cout << "전 ->" << *atIt1 << endl;
	if ( *atIt1 > 0 ) {
		cout <<"널?"<< endl;
	}
	atIt1 = find(xM.at(0).begin(), xM.at(0).end(), 8);
	cout << "후 ->" << *atIt1 << endl;

STL을 써서 짜고 있는데 atIt1의 초기값을 모르겠습니다. 그냥 NULL을 넣을려고
	deque<int>::iterator atIt1 = NULL;

라고 초기화 하고 싶어도 에러가 나고

맨 위의 코드에 find() 함수를 사용해서 못찾으면 어떤것을 반환하는지 궁금도 합니다.

익명 사용자의 이미지

반복자는 포인터형을 일반화한 것이라서 그 구현에 따라서 다양한 형태로 나타납니다. 단순한 NULL 대입으로는 불가능한 경우가 일반적이라고 생각하셔야 합니다.

굳이 반복자를 초기화하시겠다면 해당 객체의 begin() 정도로 초기화하시면 될 것 같습니다만, 위의 예처럼 find 의 리턴 값을 사용하실 것이라면 보통 end() 와 비교해서 같으면 찾는 것이 없다라고 판단하는 것이 일반적입니다.

bugiii의 이미지

헉... 왜 자동 로긴이 풀려버렸을까요? -_-;

chadr의 이미지

STL에서 find 알고리즘에서 찾지 못했을 경우에 리턴 하는 iterator는 find알고리즘의 두번째로 입력된 iterator를 리턴하도록 되어있습니다..

따라서 두번째 인자로 입력된 iterator가 end()이므로 찾지 못했을 경우에는 end() 함수가 리턴하는 iterator를 리턴할 것입니다.

-------------------------------------------------------------------------------
It's better to appear stupid and ask question than to be silent and remain stupid.

kldpzone의 이미지

네, 답변 감사합니다.
그런데 한가지

  deque<int> sp;

에서 sp에 값을 아무것도 넣지 않았을 경우.. 즉, push_back등을 전혀 하지 않았을때 find()를 쓰면 에러가 나네요.
익명 사용자의 이미지

아무 객체도 컨테이너에 포함되어 있지 않다면 .begin() 멤버 함수는 .end()와 같은 결과를 가져옵니다.

실제로 .end()가 호출하는 곳에는 아무 값도 없기 때문에, 에러가 발생하게 됩니다.

댓글 달기

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