Re: What type for iterator of for-loops?

Daniel Rentz <[email protected]> Tue, 10 May 2011 18:31:04 +0200
Newsgroups gmane.comp.openoffice.devel.general
Message-ID <[email protected]>
Am 10.05.2011 17:48, schrieb Regina Henschel:
> Hi,
>
> I'm not well-versed in C++, so I need some help in programming in
> Splines.cxx in chart2.
>
> There are already
> typedef ::std::pair< double, double > tPointType;
> typedef ::std::vector< tPointType > tPointVecType;
> typedef tPointVecType::size_type lcl_tSizeType;
>
> My data is in
> tPointVecType aPointsIn;
> and the last valid index is
> lcl_tSizeType n;
>
> Now I need a lot of for-loops from 0 to n. They iterate over a matrix of
> elements of type double, over an array of elements of type double, and
> over a vector of type tPointVecType, sometimes in the same loop. What is
> the correct type for the iterator? Should it be lcl_tSizeType or
> sal_uInt32? What is the implication of one or the other?

The correct index type for vectors is lcl_tSizeType here, which is 
typedefed to "size_t" by the vector class. On 32-bit systems, this is an 
unsigned 32-bit type, but on 64-bit systems, it is an unsigned 64-bit 
type. Using sal_uInt32 may cause compiler warnings on these systems, 
e.g. when using

sal_uInt32 i = n;


I recommend to use real iterators. If your "n" contains the size of the 
vector, i.e. you want to iterate all elements of the vector:

tPointVecType aPointsIn;
...
for (tPointVecType::const_iterator it = aPointsIn.begin(),
      it_end = aPointsIn.end(); it != it_end; ++it)
{
     /* "it" points to a pair of double */

     it->first;  // this is the first value of the pair
     it->second; // this is the second value of the pair
}


> n can be as large as the count of Spreadsheet rows, theoretically, but
> will be smaller than hundred in most cases.

If "n" is less than aPointsIn.size(), you can modify the loop header:

for (tPointVecType::const_iterator it = aPointsIn.begin(),
      it_end = it + n; it != it_end; ++it)


If you still prefer iterating by index: :-)

for (lcl_tSizeType i = 0; i < n; ++i)
{
     aPointsIn[i].first;
     aPointsIn[i].second;
}




Regards
Daniel
-- 
-----------------------------------------------------------------
To unsubscribe send email to [email protected]
For additional commands send email to [email protected]
with Subject: help