vector::front() and vector::back() in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container.
vector::front()
This function can be used to fetch the first element of a vector container.获得vector的第一个元素
Syntax :
vectorname.front() Parameters : No value is needed to pass as the parameter. Returns : Direct reference to the first element of the vector container.
Examples:
Input : myvector = 1, 2, 3 myvector.front(); Output : 1 Input : myvector = 3, 4, 1, 7, 3 myvector.front(); Output : 3
Errors and Exceptions
1. If the vector container is empty, it causes undefined behaviour. 如果vector为空,导致未定义行为
2. It has a no exception throw guarantee if the vector is not empty.
// CPP program to illustrate
// Implementation of front() function
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> myvector;
myvector.push_back(3);
myvector.push_back(4);
myvector.push_back(1);
myvector.push_back(7);
myvector.push_back(3);
// Vector becomes 3, 4, 1, 7, 3
cout << myvector.front();
return 0;
}
Output:
3
vector::back()
This function can be used to fetch the last element of a vector container.获得vector的最后一个元素
Syntax :
vectorname.back() Parameters : No value is needed to pass as the parameter. Returns : Direct reference to the last element of the vector container.
Examples:
Input : myvector = 1, 2, 3 myvector.back(); Output : 3 Input : myvector = 3, 4, 1, 7, 2 myvector.back(); Output : 2
Errors and Exceptions
1. If the vector container is empty, it causes undefined behaviour.
2. It has a no exception throw guarantee if the vector is not empty.
// CPP program to illustrate
// Implementation of back() function
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> myvector;
myvector.push_back(3);
myvector.push_back(4);
myvector.push_back(1);
myvector.push_back(7);
myvector.push_back(2);
// Vector becomes 3, 4, 1, 7, 2
cout << myvector.back();
return 0;
}
Output:
2
Difference between front(), back() and begin, end() function函数front(), back() and begin, end()之间的区别
begin() and end() function return an iterator(like a pointer) initialized to the first or the last element of the container that can be used to iterate through the collection, while front() and back() function just return a reference to the first or the last element of the container.函数begin() and end()获得最前或最后元素的迭代器(指针),可以遍历整个容器, front() and back()仅仅只是返回最前或最后的元素的引用(值)。
Application : Given an empty vector of integers, add numbers to the vector, then print the difference between the first and the last element.
Input : 1, 2, 3, 4, 5, 6, 7, 8 Output : 7 Explanation - Last element = 8, First element = 1, Difference = 7
Algorithm
1. Add numbers to the vector using push_back() function
2. Compare the first and the last element.
3. If first element is larger, subtract last element from it and print it.
4. Else subtract first element from the last element and print it.
// CPP program to illustrate
// application Of front() and back() function
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> myvector;
myvector.push_back(8);
myvector.push_back(7);
myvector.push_back(6);
myvector.push_back(5);
myvector.push_back(4);
myvector.push_back(3);
myvector.push_back(2);
myvector.push_back(1);
// Vectro becomes 1, 2, 3, 4, 5, 6, 7, 8
if (myvector.front() > myvector.back()) {
cout << myvector.front() - myvector.back();
}
else if (myvector.front() < myvector.back()) {
cout << myvector.back() - myvector.front();
}
else
cout << "0";
}
Output:
7
Recommended Posts:
- Is body of a Default Constructor blank in C++?
- clocale header file in C++
- Commonly used String functions in C/C++ with Examples
- Difference between C and C++
- unordered_map emplace() in C++ STL
- Find Maximum and Minimum element in a Set in C++ STL
- deque emplace in C++ STL
- valarray atan2() function in C++
- How to delete an element from the Set by passing its value in C++
- unordered_multimap insert() in C++ STL
- How to delete a range of values from the Set using Iterator
- unordered_map cbegin in C++ STL
- unordered_map size() in C++ STL
- unordered_map reserve() in C++ STL
- Split a string in equal parts such that all parts are palindromes
上一篇: 【C++】模拟实现vector
下一篇: 简化版vector