Standard Library Views

Using Views with Custom Container Classes

Can I use views with custom container classes?

Abstract art representing computer programming

Yes, you can use views with custom container classes, provided that your custom container adheres to the range concept requirements. The custom container should define begin() and end() methods that return iterators.

Here's an example of a custom container and how to use views with it:

#include <iostream>
#include <ranges>
#include <vector>

class CustomContainer {
  using iterator = std::vector<int>::iterator;

  void Add(int value) {

  iterator begin() {
    return data.begin();

  iterator end() {
    return data.end();

  std::vector<int> data;

int main() {
  CustomContainer MyContainer;

  auto View = std::views::take(MyContainer, 3);

  for (int Num : View) {
    std::cout << Num << ", ";
1, 2, 3,


  • CustomContainer: This class uses an internal std::vector to store elements. It defines begin() and end() methods that return iterators.
  • Add() Method: This method allows adding elements to the container.
  • Using Views: We create a view with std::views::take() to take the first three elements of the custom container.

Requirements for Custom Containers

For a custom container to be used with views:

  • It should provide begin() and end() methods returning iterators.
  • The iterators should conform to standard iterator requirements.

This allows seamless integration with the views library, enabling powerful and efficient data manipulation.

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

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