STL algorithm算法copy_n(9)

原文地址:http://www.cplusplus.com/reference/algorithm/copy_n/

function template
<algorithm>

std::copy_n

template <class InputIterator, class Size, class OutputIterator>
  OutputIterator copy_n (InputIterator first, Size n, OutputIterator result);
Copy elements
Copies the first n elements from the range beginning at first into the range beginning at result.
从first开始复制n哥元素到result的位置。

例子:

#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
using namespace std;
void copyn(){
    vector<int> vi{1,2,3,4,5,6};
    array<double,3> ai{7.7,8.8,9.9};
    cout<<"at first:vi=";
    for(int &i:vi)
        cout<<i<<" ";
    cout<<endl;

    vi.resize(12);
    auto it=copy_n(ai.begin(),3,vi.end()-4);
    cout<<"after     copy_n(ai.begin(),3,vi.end()-3) \nvi=";
    //for(double &i:vi)  //error: invalid initialization of reference of type 'double&' from expression of type 'int'|
    for(int &i:vi)
        cout<<i<<" ";
    cout<<endl;
    cout<<"the return values is it="<<*it<<endl;

}
运行截图:



The function returns an iterator to the end of the destination range (which points to one past the last element copied).

该函数返回一个指向目标序列最后一个被复制元素的下一个元素的迭代器。


If n is negative, the function does nothing.

如果n是负数,函数不会做任何事情。


If the ranges overlap, some of the elements in the range pointed by result may have undefined but valid values.

如果范围越界,即result后面不够存放时会导致未定义但状态依旧有效。

例子:

#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
using namespace std;
void copyn2(){
    vector<int> vi{1,2,3,4,5,6};
    array<double,3> ai{7.7,8.8,9.9};
    cout<<"at first:vi=";
    for(int &i:vi)
        cout<<i<<" ";
    cout<<endl;

    vi.resize(12);
    auto it=copy_n(ai.begin(),3,vi.end());
    cout<<"after     copy_n(ai.begin(),3,vi.end()) \nvi=";
    //for(double &i:vi)  //error: invalid initialization of reference of type 'double&' from expression of type 'int'|
    for(int &i:vi)
        cout<<i<<" ";
    cout<<endl;
    cout<<"the return values is it="<<*it<<endl;

}
运行截图:

STL algorithm算法copy_n(9)_第1张图片

The behavior of this function template is equivalent to:
1 2 3 4 5 6 7 8 9 10 
template<class InputIterator, class Size, class OutputIterator> OutputIterator copy_n (InputIterator first, Size n, OutputIterator result) { while (n>0) { *result = *first; ++result; ++first; --n; } return result; }



Parameters

first
Input iterators to the initial position in a sequence of at least n elements to be copied.
InputIterator shall point to a type assignable to the elements pointed by OutputIterator.
序列开始复制的位置。
n
Number of elements to copy.
If this value is negative, the function does nothing.
Size shall be (convertible to) an integral type.
要被复制的元素个数。
如果是负数,不会做任何事情。


result
Output iterator to the initial position in the destination sequence of at least n elements.
This shall not point to any element in the range [first,last).
序列开始被覆盖的位置。

Return value

An iterator to the end of the destination range where elements have been copied.

该函数返回一个指向目标序列最后一个被覆盖元素的下一个元素的迭代器。



Example

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 
// copy_n algorithm example #include <iostream> // std::cout #include <algorithm> // std::copy #include <vector> // std::vector int main () { int myints[]={10,20,30,40,50,60,70}; std::vector<int> myvector; myvector.resize(7); // allocate space for 7 elements std::copy_n ( myints, 7, myvector.begin() ); std::cout << "myvector contains:"; for (std::vector<int>::iterator it = myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; }
Edit & Run


Output:
myvector contains: 10 20 30 40 50 60 70

Complexity

Linear in the distance between first and last: Performs an assignment operation for each element in the range.

Data races

The objects in the range of n elements pointed by first are accessed (each object is accessed exactly once).
The objects in the range between result and the returned value are modified (each object is modified exactly once).

Exceptions

Throws if either an element assignment or an operation on iterators throws.
Note that invalid arguments cause undefined behavior.

——————————————————————————————————————————————————————————————————

//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155

author:天下无双

Email:[email protected]

2014-9-11

于GDUT

——————————————————————————————————————————————————————————————————






你可能感兴趣的:(Algorithm,算法,STL,C++11)