0% found this document useful (0 votes)
19 views

C++ Week 12

Uploaded by

黃楷庭
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

C++ Week 12

Uploaded by

黃楷庭
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

計算機程式與應用實習

Week 12
Vector
Note

• Vector: 向量
• Iterator: 疊代器
• Erase: 抹除 ; 消除
• Modify: 修改;修飾
• Container: 容器
Outline

• Vector
• Vector declaration and initialization
• Complier setting
• Vector function
• size()
• push_back()
• pop_back()
• front() and back()
• begin() and end()
• erase()
• insert()
• Iterator
• Find
• Practice
Vector

• A vector is a class that has both the properties of an array and


a linked list:
• Array properties: Each element is of the same data type
and continuous, and the element at that position can be
accessed using the index value.
• Linked list properties: The maximum length of the vector
can be freely changed 。
• This class also provides quite rich methods to access or
modify vector data.
Complier setting
• Before using vector, please add "-std=c++11" in "Compiler
setting" of Dev-C++.

Method 1 Method 2
Vector declaration and initialization

• declare: vector <data type> variable name ;


• initialization: variable name = {element};
• declare and initialization :
vector< data type > variable name = {element};
Array Properties of Vectors

• We can use "[ ]" to access each element in a vector, just like
an array.
Vector size

• Returns the number of elements in the vector.


• int Vector_name.size() : Threshold value often used for
loops.
Vectors push_back

• Function is used to push elements into a vector from the


back.
• void Vectors_name.push_back(element)
Vector pop back

• Remove the last element of the vector


• void pop_back(void) : The function deletes the last
element value and the vector itself is changed.
Vector front and back

• This function can be used to fetch the first and last element
of a vector container.
• Data type vector name.front(void);
• Data type vector name.back(void);
Vector begin and end

• begin() function is used to return an iterator pointing to the


first element of the vector container.
• end() function is used to return an iterator pointing next to
last element of the vector container.
Vector erase

• erase() function is used to remove elements from a container


from the specified position or range.
Vector insert

• insert() is used to insert elements or values into a vector


container.
Iterator

• Iterators, which have properties similar to those of pointers,


are used to manipulate container elements.
• vector<data type>::iterator Iterator_name;
• 「 :: 」 operator that takes out a member
Vector find

• To use find, Must #include <algorithm> this library

• Finds whether there is a specified element in the specified range,


and if so, returns the index of the first element matched.

• The iterator find (the start address of the vector to find, the end
address of the vector to find, the target element), it will return the
address of the paired element in the range to the iterator.

• If the target element is found, use "*iterator" to output the


element. To find the index value, use distance (vector start
address, iterator) to return the index value of the matched result.
Vector find
Practice

• The first input integer n, which means that there are several integers
to be input, then input the string of integers in sequence, Input the
integer m, which means that there are several integers in another
group to be input, and then input the string of integers in sequence.
Compare these two sets of integers, and output the non-repeated
integers in the two sets of arrays.
input:
3
345
4
1357
output:
147
Practice

• Input an integer n, indicating the number of elements, and


then input each element in sequence,
1. After output all the elements in sequence
2. Drop the first element
3. Reverse the order of the elements
repeat the actions 1~3 until all the elements are drop, then
end the program.
input:
5
13579
output:
13579
9753
357
75
5
Practice

• Input an integer n, then input n x n integers, and then input


integers a, b, indicating that the column a and the b-th item
are interchanged in the entire column.

input:
3
123
456
789
01
output:
213
546
879

You might also like