Mega Search
23.2 Million


Sign Up

Make a donation  
iterate through n elements at a time  
News Group: comp.lang.c++

I am trying to create a string, containing the values of n elements, in 
a list, at a time, until I've gone through the entire list.

Is there a more efficient way to do this? I am a bit concerned with my 
use of std::next here.

#include 
#include 
#include 
#include 
#include 

#include 

int main()
{
     std::list orderIDs;

     // Test data
     for(size_t index = 0; index < 50; ++index)
     {
         std::string value;
         std::stringstream converter;
         converter << index;
         converter >> value;

         orderIDs.push_back(value);
     }

     // Attempt to get 10 elements at a time,
     // Make a comma seperated string out them
     // and act upon that string
     const unsigned GET_PARAM_LIMIT = 10;

     for(size_t index = 0; index < orderIDs.size();)
     {
         size_t indexStartChunk = index;
         size_t indexEndChunk   = index + GET_PARAM_LIMIT;
         std::string orderIDsAsString;

         for(;index < indexEndChunk && index < orderIDs.size(); ++index)
         {
             if( index != indexStartChunk )
             {
                 orderIDsAsString += ",";
             }

             orderIDsAsString += *(std::next(orderIDs.begin(), index));
         }

         std::cout << orderIDsAsString << std::endl;
     }

     // Done
     system("Pause");
     return 0;
}

Vote for best question.
Score: 0  # Vote:  0
Date Posted: 28-Aug-2014, at 1:56 PM EST
From: Christopher Pisz
 
Re: iterate through n elements at a time  
News Group: comp.lang.c++
On Thu, 28 Aug 2014 13:56:22 -0500, Christopher Pisz
 wrote:

>I am trying to create a string, containing the values of n elements, in 
>a list, at a time, until I've gone through the entire list.
>
>Is there a more efficient way to do this? I am a bit concerned with my 
>use of std::next here.
>
>#include 
>#include 
>#include 
>#include 
>#include 
>
>#include 
>
>int main()
>{
>     std::list orderIDs;

If you make this a vector, you won't need use std::next at all.

>
>     // Test data
>     for(size_t index = 0; index < 50; ++index)
>     {
>         std::string value;
>         std::stringstream converter;
>         converter << index;
>         converter >> value;
>
>         orderIDs.push_back(value);
>     }
>
>     // Attempt to get 10 elements at a time,
>     // Make a comma seperated string out them
>     // and act upon that string
>     const unsigned GET_PARAM_LIMIT = 10;
>
>     for(size_t index = 0; index < orderIDs.size();)
>     {
>         size_t indexStartChunk = index;
>         size_t indexEndChunk   = index + GET_PARAM_LIMIT;

If you adjust indexEndChunk here if it runs past orderIDs.size(), you
won't have to test it in each iteration of the following for loop.

>         std::string orderIDsAsString;
>
>         for(;index < indexEndChunk && index < orderIDs.size(); ++index)
>         {
>             if( index != indexStartChunk )
>             {
>                 orderIDsAsString += ",";
>             }
>
>             orderIDsAsString += *(std::next(orderIDs.begin(), index));

With a vector, this would become the simpler
               orderIDsAsString += orderIDs[index];

>         }

I think the following may be more efficient than the preceding for
loop because it eliminates the test on index that is executed ten
times..
    for ( ; index < indexEndChunk; ++index)
        orderIDsAsString += orderIDs[index] + ",";
    orderIDsAsString.pop_back(); //remove final extraneous comma

It also eliminates the need for indexStartChunk.
        
>         std::cout << orderIDsAsString << std::endl;
>     }
>
>     // Done
>     system("Pause");
>     return 0;
>}

-- 
Remove del for email

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 28-Aug-2014, at 5:00 PM EST
From: Barry Schwarz