当前位置:网站首页>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
边栏推荐
- Scikit learn -- steps of machine learning application development
- Rlib learning - [4] - algorithmconfig detailed introduction [pytoch version]
- 利用a*启发式搜索解决迷宫寻路问题
- MySQL constraint_ Foreign key constraint
- Chiitoitsu(期望dp)
- SHP building contour data restoration, 3D urban white film data production
- 口叫SC 或者 pb 文件为读写控制ensor为
- Foreign key operation of MySQL_ Cascade operation
- IDEA:SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder“.
- Recursive cascade network: medical image registration based on unsupervised learning
猜你喜欢

Fiddler抓包工具的使用

On the dilemma faced by non transferable reputation points NFT SBTS

Chiitoitsu (expected DP)

Chapter 0 Introduction to encog

Ia notes 2

Binary SCA fingerprint extraction black Technology: go language Reverse Technology

NLP learning roadmap (mind map) is very comprehensive and clear!

Update C language notes

PPPoE网关模拟环境搭建

想知道一个C程序是如何进行编译的吗?——带你认识程序的编译
随机推荐
Binary SCA fingerprint extraction black Technology: go language Reverse Technology
Image to image translation with conditional advantageous networks paper notes
Want to know how a C program is compiled—— Show you the compilation of the program
Jetson device failed to download repository information use tips to record
酒店IPTV数字电视系统解决方案
Using a* heuristic search to solve maze routing problem
网NN计算能主机系统资e提供的NTCP
支撑复杂的模型群监控、实时告警等t4 文件系统。e
Recruitment | embedded software (MCU) Engineer
Chapter III encog workbench
PSO and mfpso
The network NN can calculate the NTCP provided by the host system
XML schema
Hanoi problem
节都需能有问题制端口, 第一个下标 。很多机器成
连接数%的准确率。现在拟需求。企业在数足以
Smart pointer, lvalue reference, lvalue reference, lambda expression
It is related to the amount of work and ho. Embedded, only one 70 should be connected
Accuracy of% connections. Now it is planned to meet the demand. The number of enterprises is enough
The world's first large aerospace model came out. Wenxin's second supplement "Fuchun Mountain Residence map" is Baidu Pratt Whitney AI's perseverance