Replacement Algorithms

Use replace() with non-random access iterators

Is it possible to use std::ranges::replace() with containers that do not support random access iterators?

Abstract art representing computer programming

Yes, std::ranges::replace() can be used with containers that do not support random access iterators.

This includes containers like std::list or std::forward_list. The std::ranges::replace() algorithm works with any container that provides input iterators.

Here's an example using std::list:

#include <algorithm>
#include <iostream>
#include <list>

int main() {
  std::list<int> Source{1, 2, 3, 3, 3, 4, 5};

  std::ranges::replace(Source, 3, 0);

  std::cout << "Modified list: ";
  for (const auto &num : Source) {
    std::cout << num << ", ";
  }
}
Modified list: 1, 2, 0, 0, 0, 4, 5,

Using std::ranges::replace() with Different Containers

  1. The std::list container: Supports bidirectional iterators, allowing std::ranges::replace() to traverse and modify elements.
  2. The std::forward_list container: Supports forward iterators, making it compatible with std::ranges::replace().

Example with std::forward_list:

#include <algorithm>
#include <forward_list>
#include <iostream>

int main() {
  std::forward_list<int> Source{
    1, 2, 3, 3, 3, 4, 5};

  std::ranges::replace(Source, 3, 0);

  std::cout << "Modified forward_list: ";
  for (const auto &num : Source) {
    std::cout << num << ", ";
  }
}
Modified forward_list: 1, 2, 0, 0, 0, 4, 5,

Considerations

  • Performance: While std::ranges::replace() works with non-random access iterators, the performance may differ compared to random access containers like std::vector.
  • Algorithm Compatibility: Ensure the container type supports the operations required by the algorithm. For instance, containers must allow traversal and element modification through iterators.

Using std::ranges::replace() with non-random access iterators expands its versatility, making it applicable to a wide range of container types in C++.

This Question is from the Lesson:

Replacement Algorithms

An overview of the key C++ standard library algorithms for replacing objects in our containers. We cover replace(), replace_if(), replace_copy(), and replace_copy_if().

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Replacement Algorithms

An overview of the key C++ standard library algorithms for replacing objects in our containers. We cover replace(), replace_if(), replace_copy(), and replace_copy_if().

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 125 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved