Teacher Teacher - STL Source Code Analysis - Question in the book

zhaozj2021-02-11  209

Teacher Teacher - STL Source Code Analysis - Question in the book

Foreword

I am watching the STL source code in Houjie. Use in the case. The code is as follows: Status 8: __ STL_MEMBER_TEMPLATES

#include

Using namespace std;

Class alloc {

}

Template

Class vector {

PUBLIC:

Typedef t value_type;

TypeDef value_type * iterator;

Template

Void Insert (Iterator Position, I First, I Last)

{

Cout << "INSERT () << endl;

}

}

int main ()

{

INT IA [5] = {0, 1, 2, 3, 4};

Vector x;

Vector :: Iterator ITE;

X.insert (ITE, IA, IA 5); // INSERT ()

}

There is an error in the code in the code, and when compiling running, it must be replaced with the iTerator and Vector in the Iterator and Vector declarations in the STL library. There is also a mainframe without a return value. The replacement code is as follows:

Replace the code

#include

#include

Using namespace std;

int main ()

{

INT IA [5] = {0, 1, 2, 3, 4};

Vector x;

Vector :: Iterator ITE;

X.insert (ITE, IA, IA 5); // INSERT ()

Retrun 0;

}

The reason is that ITE is not initialized when defining ITE. When calling this INSERT function (Of course, call INSERT (int value) will not be wrong, it does not use ITE), it thinks it is correctly initialized ITE (ITE is invalid at this time, because there is no initialization), so when it comes to ITE Illegal access occurs when the memory is stored. When you have correct initialization when you declare the Vector ITE, it will not be wrong.

Vector :: item it = x.begin ();

Writing this article may have a small subject masterpiece, and the error here is not an initialization pointer, but it does not have illegal access because it does not use ITE. But when I actually applied, I must pay attention to initialization. Don't think that Teacher Hou teacher is written, they will copy it, then you will regret it. The author is just a semantic, or it may be that I understand the error. In short, this article is for reference only. If there is a mistake, he is looking forward to the letter, and it is grateful.

Acknowledgments!

Yuan Kai

2001-11-09

转载请注明原文地址:https://www.9cbs.com/read-5584.html

New Post(0)