-
Notifications
You must be signed in to change notification settings - Fork 0
Iterator
DryPerspective edited this page Sep 21, 2023
·
1 revision
Most of the changes to <iterator> since C++98 were explicitly to support new language features, such as move semantics, concepts, and C++20 ranges. As such, this is a relatively minor header only containing what is meaningful in C++98 and what can be reasonably implemented within the limits of the C++98 template rules.
This header also includes some helper typedefs to get a good handle on the actual iterator types in a generic way - work which would ordinarily be done by auto on C++11 and up. These allow handles on iterator types for both iterable containers as well as C-arrays to be created in generic code.
Note that these functions to extend support for const, reverse, and const-reverse iterators even if many C++98 tools did not.
| counted_iterator | An iterator which maintains an internal count to the end of its specified range |
| iter_swap | Swaps the values pointed to by two dereferenceable iterators |
| make_reverse_iterator | Makes a reverse iterator from its parameter |
| next | Increment an iterator |
| prev | Decrement an iterator |
| begin cbegin |
Returns an iterator to the beginning of the provided range |
| end cend |
Returns an iterator to the end of the provided range |
| rbegin crbegin |
Returns a reverse iterator to the beginning of the provided range |
| rend crend |
Returns a reverse iterator to the end of the provided range |
| size | Returns the size of the provided range, using unsigned type std::size_t
|
| ssize | Returns the size of the provided range, using signed type std::ptrdiff_t
|
| empty | Returns whether the provided range is empty |
| data | Returns the underlying array for the provided range |
| iterator_type | Metafunction to get a iterator for a given type |
| const_iterator_type | Metafunction to get a const_iterator for a given type |
| reverse_iterator_type | Metafunction to get a reverse_iterator for a given type |
| const_reverse_iterator_type | Metafunction to get a const_reverse_iterator for a given type |
#include <vector>
#include <string>
#include "cpp98/array.h"
#include "cpp98/iterator.h"
template<typename T>
void PrintRangeData(const T& range){
if(dp::empty(range)) return;
typedef typename dp::const_iterator_type<T>::type Iter; //Provided iterator type
for(Iter it = dp::cbegin(range); it != dp::cend(range); ++it){
Print(*it);
}
Print("The range has size " + dp::size(range));
}
int main(){
std::vector<int> vec(5,5); //Vector of five fives
std::string str("Hello world");
dp::array<double, 10> arr;
arr.fill(50);
float c_arr[5] = {1,2,3,4,5};
PrintRangeData(vec);
PrintRangeData(str);
PrintRangeData(arr);
PrintRangeData(c_arr);
}