STL/algorithm/copy

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

[править] Печать всех элементов коллекции с помощью std::copy

 
#include <iostream>
#include <algorithm>
#include <vector>
 
int main()
{    
    std::vector<int> coll;
    for( int n = 1; n < 16; ++n) coll.push_back( n);
    std::copy( coll.begin(), coll.end(), std::ostream_iterator<int>(std::cout, " ") );
    return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

[править] Копирование в пустую коллекцию с помощью std::copy

 
#include <iostream>
#include <algorithm>
#include <vector>
 
int main()
{    
    std::vector<int> coll;
    std::vector<int> coll_empty;
    for( int n = 1; n < 16; ++n) coll.push_back( n);
    std::copy( coll.begin(), coll.end(), std::back_inserter(coll_empty));
    std::copy( coll_empty.begin(), coll_empty.end(), std::ostream_iterator<int>(std::cout, " ") );
 
 
    return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15