0

I have a list

list<pair<Zeitpunkt, double>> l_tempdiff;

And I only want to cout the first 5 elements.

I only know the way of couting the whole list with:

for (auto elem : l_tempdiff)
{
    cout << elem.first << elem.second << endl;
}

I dont know how to acces my elements when I use:

for (it = l_tempdiff.begin(); it != l_tempdiff.end(); ++it)
{

}

And I guess I need to change the l_tempdiff.end() to some other value but it doesnt seem to take just the number5`. How can I do this?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Craig Harrison
  • 85
  • 1
  • 1
  • 6

4 Answers4

1

Since std::list iterators are not random access you cannot just increment them like l_tempdiff.begin() + 5. What you can do is use std::next to increment the iterator the required number of times. That would looks like

for (auto it = l_tempdiff.begin(), end = std::next(l_tempdiff.begin(), 5); it != end; ++it)
{
    // use `*it` here
}

Before doing this though you should make sure the list is big enough because if it isn't then you'll have undefined behavior.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • This kind of throws me an error: Wetterdienst.cpp:128:80: error: redefinition of 'it' for (auto it = l_tempdiff.begin(), end = std::next(l_tempdiff.begin(), 5), it(HERE) != end; ++it) And still leaves me with the question on how to access my pair values as I did easily with the for (auto elem : l_tempdiff) and elem.first – Craig Harrison Aug 31 '17 at 13:33
  • @CraigHarrison do you want to just print the values or print and do some additional stuff in the loop? – pmaxim98 Aug 31 '17 at 13:37
  • @CraigHarrison Fixed. I had a `,` where there should have been a `;` – NathanOliver Aug 31 '17 at 13:53
  • I only want to print the values – Craig Harrison Aug 31 '17 at 13:53
  • @NathanOliver Okay I figured out it works with cout << it->first << it->second< – Craig Harrison Aug 31 '17 at 13:58
1

You only want to output the first five elements?
Well, a for-range-loop is a good place to start, just add the additional constraint as a break-condition:

int i = 0;
for (auto&& elem : l_tempdiff)
{
    if (5 < ++i) break;
    cout << elem.first << elem.second << endl;
}

I change auto to auto&& to avoid needless copying.

As an aside, consider reading "Why is "using namespace std" considered bad practice?" and "C++: "std::endl" vs "\n"".

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
0
 list<pair<Zeitpunkt,double> > :: iterator it;
 int m = 0;
 it = l_tempdiff.begin();

 while( it != l_tempdiff.end() && m < 5)
 {
  cout<<it->second<<"\n"; 
  m++;
  it++;
 }
akp
  • 619
  • 5
  • 12
  • 1
    While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please include an explanation for your code, as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion – Prashant Tukadiya Aug 31 '17 at 14:30
0

Try

 auto it = l_tempdiff.begin();
 auto end = l_tempdiff.end();

 for (int count = 0; count < 5 && it != end; ++count)
 {
     std::cout << it->first << it->second << std::endl;
     std::advance(it);
 }

This prints the first five pairs (or all the pairs, if there are less than 5).

count is used to control the maximum number of elements to be printed.

it is an iterator that, in each iteration of the loop, references the current pair.

Note that advancing an end iterator gives undefined behaviour. So it is necessary to terminate the loop if the end iterator is reached (hence the it != end test in the loop condition) or if the maximum number of elements (5) is reached.

Peter
  • 35,646
  • 4
  • 32
  • 74