Карта сайта Kansoftware
НОВОСТИУСЛУГИРЕШЕНИЯКОНТАКТЫ
Разработка программного обеспечения

Std copy

Boost , Chapter 1. Boost.Icl , Examples

Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

PrevUpHomeNext

Стандартный алгоритм std::copy может использоваться для заполнения интервальных контейнеров из стандартных контейнеров интервальных или интервальных пар значений (сегментов). Поскольку интервалы не представляют элементы, а наборы, которые могут быть пустыми или содержать более одного элемента, использование std::копия отличается от того, что мы знаем при использовании контейнеров элементов.

  • Используйте icl::inserter#include<boost/icl/iterator.hpp> вместо std::inserter для вызова вставок в контейнере целевого интервала.
  • <>add для создания желаемых результатов агрегирования. Вы можете использовать std::copy с icl::adder вместо icl::inserter чтобы добиться этого. .

#include <iostream>
#include <vector>
#include <algorithm>
#include <boost/icl/interval_map.hpp>
using namespace std;
using namespace boost;
using namespace boost::icl;
// 'make_segments' returns a vector of interval value pairs, which
// are not sorted. The values are taken from the minimal example
// in section 'interval combining styles'.
vector<pair<discrete_interval<int>, int> > make_segments()
{
    vector<pair<discrete_interval<int>, int> > segment_vec;
    segment_vec.push_back(make_pair(discrete_interval<int>::right_open(2,4), 1));
    segment_vec.push_back(make_pair(discrete_interval<int>::right_open(4,5), 1));
    segment_vec.push_back(make_pair(discrete_interval<int>::right_open(1,3), 1));
    return segment_vec;
}
// 'show_segments' displays the source segements.
void show_segments(const vector<pair<discrete_interval<int>, int> >& segments)
{
    vector<pair<discrete_interval<int>, int> >::const_iterator iter = segments.begin();
    while(iter != segments.end())
    {
        cout << "(" << iter->first << "," << iter->second << ")";
        ++iter;
    }
}
void std_copy()
{
    // So we have some segments stored in an std container.
    vector<pair<discrete_interval<int>, int> > segments = make_segments();
    // Display the input
    cout << "input sequence: "; show_segments(segments); cout << "\n\n";
    // We are going to 'std::copy' those segments into an interval_map:
    interval_map<int,int> segmap;
    // Use an 'icl::inserter' from <boost/icl/iterator.hpp> to call 
    // insertion on the interval container.
    std::copy(segments.begin(), segments.end(),
              icl::inserter(segmap, segmap.end()));
    cout << "icl::inserting: " << segmap << endl;
    segmap.clear();
    // When we are feeding data into interval_maps, most of the time we are
    // intending to compute an aggregation result. So we are not interested
    // the std::insert semantincs but the aggregating icl::addition semantics.
    // To achieve this there is an icl::add_iterator and an icl::adder function 
    // provided in <boost/icl/iterator.hpp>.
    std::copy(segments.begin(), segments.end(),
              icl::adder(segmap, segmap.end())); //Aggregating associated values
    cout << "icl::adding   : " << segmap << endl;
    // In this last case, the semantics of 'std::copy' transforms to the 
    // generalized addition operation, that is implemented by operator
    // += or + on itl maps and sets.
}
int main()
{
    cout << ">>    Interval Container Library: Example std_copy.cpp    <<\n";
    cout << "-----------------------------------------------------------\n";
    cout << "Using std::copy to fill an interval_map:\n\n";
    std_copy();
    return 0;
}
// Program output:
/*---------------------------------------------------------
>>    Interval Container Library: Example std_copy.cpp    <<
-----------------------------------------------------------
Using std::copy to fill an interval_map:
input sequence: ([2,4),1)([4,5),1)([1,3),1)
icl::inserting: {([1,5)->1)}
icl::adding   : {([1,2)->1)([2,3)->2)([3,5)->1)}
---------------------------------------------------------*/


PrevUpHomeNext

Статья Std copy раздела Chapter 1. Boost.Icl Examples может быть полезна для разработчиков на c++ и boost.




Материалы статей собраны из открытых источников, владелец сайта не претендует на авторство. Там где авторство установить не удалось, материал подаётся без имени автора. В случае если Вы считаете, что Ваши права нарушены, пожалуйста, свяжитесь с владельцем сайта.



:: Главная :: Examples ::


реклама


©KANSoftWare (разработка программного обеспечения, создание программ, создание интерактивных сайтов), 2007
Top.Mail.Ru

Время компиляции файла: 2024-08-30 11:47:00
2025-05-20 13:57:48/0.003788948059082/0