当前位置:网站首页>Learning some contents of vector and iterator
Learning some contents of vector and iterator
2022-07-24 05:14:00 【Rain and cold at night in Xiaoxiang】
vector because “ Holding ” Other objects , So it is often called “ Containers ”
One 、vector The basic content
vector<int>ivec; //ivec preservation int Object of type
vector<Students>Student; //Student preservation Students Object of type
One side ,vector The advantage is that , The program can run efficiently vector Object .
On the other hand ,vector You can also save “ class ” The object of , That's great
Personally think that ,vector One of the benefits of can save memory . It's like we just started learning C and C++ When , If you want to open an array , All open, similar to
int a[100000]={0};// Open a big value
// you 're right , But it's a waste of memory , And will let others see that he is a novice Xiaobai
Two 、vector The initialization
vector<int>v1(10)//v1 Yes 10 Elements , Each value is 0
vector<int>v2{
10}//v2 Yes 1 Elements , The value of this element is 0
vector<int>v3(10,1);//v3 Yes 10 Elements , Each value is 1
vector<int>v4{
10,1}//v4 Yes 2 Elements , Respectively 10 and 1
You know ,() It is used to initialize the number of elements ,{ } Is used to initialize vector Object's
3、 ... and 、vector operation
```cpp
v.empty();// If v It doesn't contain any elements , Return to true ; Otherwise return false
v.size();// return v The number of elements in
v.push_back(t);// towards v Add a value of t The elements of
v[n];// return v pass the civil examinations n A reference to an element at a location
v1 = v2;// use v2 Copy replacement of elements in v1 The elements in
v1 = {
a,b,c...};// Replace... With a copy of the elements in the list v1 The elements in
Be careful : You can't add elements in subscript form
But we can know the existence of elements to perform subscript operations , Such as :
vector<int> ivec;
cout << ivec[0];// error :ivec It doesn't contain any elements
vector<int>ivec2(10);
cout << ivec2[10];// error :ivec2 The legal retrieval of elements is 0-9
vector<int>c(10);
c[5] = 6;
cout << c[5] << endl;// The output is 6, This can explain how to perform the following table operations when determining the existing elements
iterator :
We can use the subscript operator to access string The character or... Of the object vector The elements of the object , In fact, there is another more general mechanism that can achieve the same purpose —— iterator .
Iterators are valid and invalid , This is similar to the pointer
Similar to pointer type , Iterators also provide indirect access to objects . But unlike pointers , Getting iterators is not picking address characters , Types with iterators have members that return iterators . For example, these types are called begin and end Members of , among begin Member is responsible for returning the iterator that points to the first element .
Operators of standard container iterators
*itera// Return iterator iter The reference to the indicated element
iter->mem// Quoting iter And get the name of the element mem Members of , Equivalent to (*iter).mem
++iter// Make iter Indicates the next element of the container
--iter// Make iter Indicates the next element of the container
!! because end The returned iterator does not actually indicate an element , Therefore, it cannot be incremented or dereferenced !
Iterator type :
In fact, those standard library types that have iterators use iterator and const_iterator To represent the type of iterator :
vector::iterator it; //it Be able to read and write vector The elements of
string::iterator it2; //it2 Be able to read and write string Characters in objects
vector::const_iterator it3; //it3 Only read vector The elements of , Can't write elements
string::const_iterator it4; //it4 Only read string Characters in objects , Can't write characters
vector<int>c;
for (int i = 0;i < 10;i++)
{
c.push_back(i);
}
vector<int>::iterator it;
for (it = c.begin();it != c.end();++it)
{
cout << *it << endl;
}
The output is
0
1
2
3
……
9
This is the use of iterators
Combining dereference and member access operations
Check vector Whether the object is empty , You can use the following code
(it).empty();
Parentheses are indispensable , The meaning of this expression is to it Quoting , Then the result of dereference is executed with dot operator .
But in order to simplify the above expression ,c++ The language defines the arrow operator (->) Arrow operators combine dereference and member access , in other words :
it->mem and (it).mem Express the same meaning . Therefore, it can also be used it->empty();
for example
typedef struct node
{
int data;
struct node* next;
}stu;
stu* list1;
stu* list2;
void creat()
{
list1 = (stu*)malloc(sizeof(struct node));
list2 = (stu*)malloc(sizeof(struct node));
list1->next = NULL;// Equivalent to (*list1).next = nullptr;
list2->next = NULL;// Equivalent to (*list2).next = nullptr;
}
Dictation right vector The operation of the object invalidates the iterator
vector It can grow dynamically , But there will be some side effects . A known limitation is that it cannot be within the scope for Circular thinking vector Object to add elements , There are also some limitations, which will be discussed later .
But for loop bodies that use iterators , Do not add elements to the container to which the iterator belongs
( for example push_back()) operation .
About c++primer practice 3.26 Some insights
practice 3.26:
stay 100 In the binary search program of page , Why do you use mid = beg + ( end - beg ) / 2, Instead of mid = ( beg + end ) / 2;?
The answer is simple , Because in vector Iterators support operations , Adding two iterators is not supported , But it supports an iterator +(or)- A number .
Subtracting two iterators is the distance between them , It's a number !
Learning materials :《C++Primer》, Classics are classics
边栏推荐
- Use of fiddler packet capturing tool
- Summary of the development process and key and difficult points of the Listening Project
- Hcip day 3 - mGRE experiment
- Introduction to MapReduce
- Summary of common errors in wechat applet cloud development
- Teach you how to weld CAD design board bottom (for beginners) graphic tutorial
- The difference between compiled language and interpreted language
- MapReduce介绍
- Learning pyramid context encoder network for high quality image painting paper notes
- Token of space renewable energy
猜你喜欢

Chapter 0 Introduction to encog

Kingbase V8R6集群安装部署案例---脚本在线一键缩容

微信朋友圈的高性能架构设计

Kingbase V8R6集群安装部署案例---脚本在线一键扩容

MGRE and OSPF comprehensive experiment

Chiitoitsu (expected DP)

How can e-commerce projects solve the over issuance of online coupons (troubleshooting + Solutions) (glory Collection)

Chapter V communication training

The difference between run and start in thread class

Theoretical basis of machine learning
随机推荐
Bear market bottoming Guide
MapReduce介绍
[Huang ah code] Introduction to MySQL - 3. I use select *, and the boss directly rushed me home by train, but I still bought a station ticket
Image painting for irregular holes using partial revolutions paper notes
Problems and solutions of QT (online installation package) crash in win10 installation
C. Recover an RBS (parenthesis sequence, thinking)
Introduction to MapReduce
Chapter 1 regression, classification & clustering
XML schema
京东方高级副总裁姜幸群:AIoT技术赋能企业物联网转型
Chapter5 foundation of deep learning
Quick reference manual for the strongest collation of common regular expressions (glory Collection Edition)
On the dilemma faced by non transferable reputation points NFT SBTS
Learning pyramid context encoder network for high quality image painting paper notes
13. Write a program, in which a user-defined function is used to judge whether an integer is a prime number. The main function inputs a number and outputs whether it is a prime number.
Jetson device failed to download repository information use tips to record
MapReduce concept
[basic 8] - processes and threads
微信朋友圈的高性能架构设计
Hcde city closed door meeting successfully held in Nanjing station