P
pandit
Hello i dont understand how to Deal with iterator. its little bit
confusing ?
how they work??
confusing ?
how they work??
pandit said:Hello i dont understand how to Deal with iterator. its little bit
confusing ?
how they work??
Hello i dont understand how to Deal with iterator. its little bit
confusing ?
how they work??
iterators are for _iterating_ through a container. I wouldn't worry about
how they work, but I'd learn how to use them.
Just think of it as a "special" kind of pointer to an element in a
container. You do not know how the underlying implementation of the
container works, so they provide you with a way to access an element no
matter how it has been allocated.
Almost all stl containers have a begin() and end() method.
If you visualize your stl container like an array, then begin points to
element [0] and end points to the next element out of bounds.
example:
#include<vector>
#include <iostream>
int main()
{
std::vector<int> bunchonumbers;
// Fill it up with numbers
for(int i =0; i < 5; i++)
bunchonumbers.pushback(i);
// Visualize an array [0][1][2][3][4]
// Now you can _iterate_ through it
// begin points to element [0]
// end points to some memory after [4], containing something unknown
// notice how you can perform arithmetic just like pointers: it++
for( std::vector<int>::iterator it = bunchonumbers.begin(); it !=
bunchonumbers.end(); it++)
{
// Notice how you can dereference the iterator just like a pointer
std::cout << *it;
}
return 0;
}
Iterators are just a special way of accessing elements in a container.
There is more to iterators than that, but it is the basics. For example,
there are different flavors of iterators with different rules, but that
comes later.
Hello i dont understand how to Deal with iterator. its little bit
confusing ?
how they work??
iterators are for _iterating_ through a container. I wouldn't worry about
how they work, but I'd learn how to use them.
Just think of it as a "special" kind of pointer to an element in a
container. You do not know how the underlying implementation of the
container works, so they provide you with a way to access an element no
matter how it has been allocated.
Almost all stl containers have a begin() and end() method.
If you visualize your stl container like an array, then begin points to
element [0] and end points to the next element out of bounds.
example:
#include<vector>
#include <iostream>
int main()
{
std::vector<int> bunchonumbers;
// Fill it up with numbers
for(int i =0; i < 5; i++)
bunchonumbers.pushback(i);
// Visualize an array [0][1][2][3][4]
// Now you can _iterate_ through it
// begin points to element [0]
// end points to some memory after [4], containing something unknown
// notice how you can perform arithmetic just like pointers: it++
for( std::vector<int>::iterator it = bunchonumbers.begin(); it !=
bunchonumbers.end(); it++)
{
// Notice how you can dereference the iterator just like a pointer
std::cout << *it;
}
return 0;
}
Iterators are just a special way of accessing elements in a container.
There is more to iterators than that, but it is the basics. For example,
there are different flavors of iterators with different rules, but that
comes later.
Christopher said:iterators are for _iterating_ through a container. I wouldn't worry about
how they work, but I'd learn how to use them.
Just think of it as a "special" kind of pointer to an element in a
container. You do not know how the underlying implementation of the
container works, so they provide you with a way to access an element no
matter how it has been allocated.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.