Working with Temporary Buffers - C++ STL Algorithm

C++ examples for STL Algorithm:uninitialized_copy

Description

Working with Temporary Buffers

Demo Code

#include <iostream>
#include <vector>
#include <memory>
#include <algorithm>
#include <string>
using namespace std;

int main()//from   w w  w . j a  v  a2  s.  c  o m
{
    vector<string> Words;

    Words.push_back("Blue");
    Words.push_back("Green");
    Words.push_back("Teal");
    Words.push_back("Brick");
    Words.push_back("Purple");
    Words.push_back("Brown");
    Words.push_back("LightGray");

    int Count = Words.size();
    cout << "Words contains: " << Count << " elements." << endl;

    // Create the buffer and copy the data to it.
    pair<string*, ptrdiff_t> Mem = get_temporary_buffer<string>(Count);

    uninitialized_copy(Words.begin(), Words.end(), Mem.first);

    // Perform a sort and display the results.
    sort(Mem.first, Mem.first+Mem.second);

    for (int i = 0; i < Mem.second; i++)
        cout << Mem.first[i] << endl;

    return 0;
}

Result


Related Tutorials