Re: std::vector : begin, end and insert - Using Objects instead of ints
- From: Gerry Quinn <gerryq@xxxxxxxxx>
- Date: Tue, 15 May 2007 17:29:04 +0100
In article <uUryFYAfHHA.1252@xxxxxxxxxxxxxxxxxxxx>, Nobody@xxxxxxxxx
says...
Hi,
I thought I would experimenting with vectors.
I am trying to use CPoint objects instead the examples, which use ints.
So, I am a having a few problems.
Question 1.
How do I get the integer value from _Iter or
How do I use _Iter to get the object?
for (_Iter = pt.begin(); _Iter < pt.end(); _Iter++)
{
CPoint& pt = Pts.at(??);
}
I am not sure about the use of begin and end.
Would 0 and size() be the equivalents?
Would there ever be a difference between them?
Method 1 (works for both vectors and lists):
vector< CPoint > vec;
// put some CPoints in it
vector< CPoint >::iterater it;
for ( it = vec.begin(); it != vec.end(); it++ )
{
CPoint & pt = *it;
// iterators are overloaded pointers
// they use pointer syntax
}
Method 2 (if you used a vector because you want something like an
array, only better):
vector< CPoint > vec;
// put some CPoints in it
for ( int i = 0; i < vec.size(); i++ )
{
CPoint & pt = vec[ i ];
// or alternatively
CPoint & samething = vec.at( i );
}
Method 2 works on vectors and similar collection classes, but not on
lists, which only have iterators. But it makes for more readable code
when you're using vectors.
Get the above sorted out in your head before doing anything
complicated.
- Gerry Quinn
.
- Follow-Ups:
- Re: std::vector : begin, end and insert - Using Objects instead of ints
- From: Doug Harrison [MVP]
- Re: std::vector : begin, end and insert - Using Objects instead of ints
- Prev by Date: Re: BringWindowToTop: "Simulate" "ALT-Tab" keys
- Next by Date: Re: Microsoft support for MFC42 and Visual C++
- Previous by thread: Re: deriving from CListCtrl
- Next by thread: Re: std::vector : begin, end and insert - Using Objects instead of ints
- Index(es):
Relevant Pages
|
Loading