answersLogoWhite

0

What is an iterator in c plus plus?

Updated: 8/11/2023
User Avatar

Wiki User

10y ago

Best Answer

C++ allows programmers to express ideas in code. Classes are the principal method we use to express those ideas, classifying both data and functions to produce objects that behave in an obvious, intuitive and consistent manner.

If were to define a 3D drawing system we might start with some primitive objects such as spheres, pyramids and cuboids. We could simply define three separate classes for these objects but then we would miss a fundamental aspect: all three share something in common. They are all shapes. While spheres, pyramids and cuboids are all examples of real-world objects, a shape is not a real world object at all, it is merely the concept of an object. Concepts are abstract classes -- we cannot construct objects from them. They don't exist in the real-world but we can certainly imagine they exist and we can easily say that a sphere is a type of shape. If we can imagine it exists then we can express that idea in our code. Our shape class simply describes everything that is common to all shape objects, whether spheres, pyramids, cuboids or any other type of real-world shape. This means we can place all types of shape in containers and treat them as a single entity -- a collection of shape. Each type of shape has its own specialised properties but they also share a common interface: they can all be drawn, erased, moved, rotated, coloured and so on.

Thus we can use the shape class to declare the common interface while each specific type of shape provides the actual implementation. In this way our code does not need to know what specific type of shape it is working with, it is enough to know that it is a shape (of some kind) and we can invoke its draw, erase, move, rotate or colour methods and let the object itself decide how to implement that method.

In short, a concept is an abstraction that allows us to express something that is common between objects that would otherwise be treated separately. It allows us to generalise our code in such a way that we can cater for new objects that do not yet exist. So long as they are conceptually the same, they can be treated the same, without having to alter the code that provides the treatment in any way.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

An iterator is a type of pointer that is used to iterate or traverse an array or list of objects.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is an iterator in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is front functions in c plus plus?

Front functions such as vector::front(), list::front() and queue::front() return the first object in a container class. Not to be confused with begin() functions such as vector::begin() which return an iterator object which is typically used in iterative functions (loops) to traverse the objects within a container.


What is difference Iterator and Enumeration?

1.Iterator has remove method while enumeration doesn't have it. 2.Iterator is new in Java API but Enumeration is older one. 3. Iterator extends functionality of Enumeration. 4. Iterator is more secure and safe as compared to Enumeration because it does not allow other thread to modify the collection object while some thread is iterating over it and throws ConcurrentModificationException.


How can write a C plus plus programming on matrix addition only?

#include<iostream> #include<array> template<typename T, const size_t c> class Matrix1D { public: using data_type = std::array<T, c>; using iterator = typename data_type::iterator; using const_iterator = typename data_type::const_iterator; private: data_type m_data; public: ~Matrix1D () = default; Matrix1D () = default; Matrix1D (const Matrix1D&) = default; Matrix1D& operator= (const Matrix1D&) = default; Matrix1D (Matrix1D&& source): m_data (std::move (source.m_data)) {} Matrix1D& operator= (Matrix1D&& source) { m_data = std::move (source.m_data); return *this; } Matrix1D& operator+= (const Matrix1D&); iterator begin() { return m_data.begin(); } iterator end() { return m_data.end(); } const_iterator begin() const { return m_data.begin(); } const_iterator end() const { return m_data.end(); } }; template<typename T, const size_t c> Matrix1D<T, c>& Matrix1D<T, c>::operator+= (const Matrix1D<T, c>& rhs) { iterator ita; const_iterator itb; for (ita=begin(), itb=rhs.begin(); ita!=end(); ++ita, ++itb) *ita += *itb; return *this; } template<typename T, const size_t c> std::ostream& operator<< (std::ostream& os, const Matrix1D<T, c>& m) { for (Matrix1D<T, c>::const_iterator it=m.begin(); it!=m.end(); ++it) os << *it << '\t'; return os; } template<typename T, const size_t r, const size_t c> class Matrix2D { public: using data_type = std::array<Matrix1D<T, c>, r>; using iterator = typename data_type::iterator; using const_iterator = typename data_type::const_iterator; private: data_type m_data; public: ~Matrix2D () = default; Matrix2D () = default; Matrix2D (const Matrix2D&) = default; Matrix2D& operator= (const Matrix2D&) = default; Matrix2D (Matrix2D&& source): m_data (std::move (source.m_data)) {} Matrix2D& operator= (Matrix2D&& source) { m_data = std::move (source.m_data); return *this; } Matrix2D& operator+= (const Matrix2D&); Matrix2D operator+ (const Matrix2D&); iterator begin() { return m_data.begin(); } iterator end() { return m_data.end(); } const_iterator begin() const { return m_data.begin(); } const_iterator end() const { return m_data.end(); } }; template<typename T, const size_t r, const size_t c> Matrix2D<T, r, c>& Matrix2D<T, r, c>::operator+= ( const Matrix2D<T, r, c>& rhs) { iterator ita; const_iterator itb; for (ita=begin(), itb=rhs.begin(); ita!=end(); ++ita, ++itb) *ita += *itb; return *this; } template<typename T, const size_t r, const size_t c> Matrix2D<T, r, c> Matrix2D<T, r, c>::operator+ (const Matrix2D<T, r, c>& rhs) { Matrix2D<T, r, c> result (*this); result += rhs; return result; } template<typename T, const size_t r, const size_t c> std::ostream& operator<< (std::ostream& os, const Matrix2D<T, r, c>& m) { for (Matrix2D<T, r, c>::const_iterator it=m.begin(); it!=m.end(); ++it) os << *it << '\n'; return os; } int main() { const size_t rows = 2, cols = 3; Matrix2D<int, rows, cols> A, B; Matrix2D<int, rows, cols>::iterator it; Matrix1D<int, cols>::iterator in; int value = 1; for (it=A.begin(); it!=A.end(); ++it) { for (in=(*it).begin(); in!=(*it).end(); ++in) (*in) = value++; } std::cout << "Matrix A:\n\n" << A << std::endl; for (it=B.begin(); it!=B.end(); ++it) { for (in=(*it).begin(); in!=(*it).end(); ++in) (*in) = ++value; } std::cout << "Matrix B:\n\n" << B << std::endl; std::cout << "Matrix A + B:\n\n" << A + B << std::endl; }


Is Iterator a class or Interface?

Its an interface.


What is the hasnext method?

Iterator.hasNext() is one of the two main methods of the Iterator interface. It can be used on an Iterator to find out if there are any remaining objects to iterate over. For example, it would be used like this: Iterator iter = ...; while (iter.hasNext()) { // do something with iter.next()... }


What is b plus b plus b plus c plus c plus c plus c?

b+b+b+c+c+c+c =3b+4c


What is c plus c plus 2c plus c plus c equal?

c + c + 2c + c + c = 6c


B plus b plus b plus c plus c plus c plus c equals?

b + b + b + c + c + c + c = 3b + 4c


Symplify c plus c plus c plus c?

4c


What is c plus c plus c plus c plus c?

c + c + c + c + c = 5 * c.


How do you write method pubic boolean containsString s Return whether the given string is in the array or not?

Return an iterator to the string if it exists, or the container's end iterator if it does not.


Primary and secondary key in c and c plus plus?

There are no "primary and secondary keys" in c and c plus plus.