yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis 2.0.2
sortedvector.h
Go to the documentation of this file.
1// -*- C++ -*-
2//
3// This file is part of YODA -- Yet more Objects for Data Analysis
4// Copyright (C) 2008-2024 The YODA collaboration (see AUTHORS for details)
5//
6#ifndef YODA_SORTEDVECTOR_H
7#define YODA_SORTEDVECTOR_H
8
9#include <vector>
10#include <algorithm>
11#include <stdexcept>
12#include <iostream>
13
14namespace YODA {
15 namespace Utils {
16
17
26 template <typename T>
27 class sortedvector : public std::vector<T> {
28 public:
29
31 sortedvector() {}
32
34 sortedvector(const std::vector<T> & vec)
35 : std::vector<T>(vec) {
36 std::sort(this->begin(), this->end());
37 }
38
40 void insert(const T& val) {
41 // Dumb way:
42 // std::vector<T>::push_back(val);
43 // std::sort(this->begin(), this->end());
44 //
45 // A bit better:
46 std::vector<T>::insert(std::upper_bound(std::vector<T>::begin(), std::vector<T>::end(), val), val);
47 }
48
49
50 private:
51
53 void push_back();
54
55 };
56
57
58 }
59}
60
61#endif
Anonymous namespace to limit visibility.