Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Traverse and Sum Elements | Iterator Fundamentals
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C++ STL Iterators
Sectionย 1. Chapterย 4
single

single

bookChallenge: Traverse and Sum Elements

Swipe to show menu

Task

Swipe to start coding

Write a function that traverses a container using iterators and returns the sum of all elements.

  • The function signature must be int sum_elements(const std::list<int>& numbers);.
  • Start iteration at numbers.begin() and stop when the iterator equals numbers.end().
  • Use ++it to move the iterator forward.
  • Only dereference the iterator when it != numbers.end().
  • Do not use size(), empty(), or indices.
  • Do not use ranged-for loops or algorithms like std::accumulate.
  • The function should return the sum of all elements in the list.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Sectionย 1. Chapterย 4
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt