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 ファースト into the range beginning at result.firstからn兄の元素をreultの位置にコピーします.
例:
#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) 
vi="; //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 レスリング may have undefined but valid values.
範囲が境界を越えています.つまり、レスリングの後ろに保管されていない場合は、未定義の状態になりますが、それでも有効です.
例:
#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()) 
vi="; //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; }
パラメータ
ファースト
Input iterators to the initial position in a sequence of at least n elements to be copied.InputIterator sharll point to a type assignable to the elements pointed by Output Iterator.シーケンスがコピーし始めた位置です.
n
Number of elemens to copy.If this value is negative,the function does nothing.Size sharll be(convertible to)an integral type.コピーされる要素の個数.負数なら、何もしません.
レスリング
Output iterator to the initial position in the destination sequence of at least n elemens.This sharll not point to any element in the range  [first,last).
シーケンスが上書きされた位置から開始します.
Return value
An iterator to the end of the destination range where elemens 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 << '
'; return 0; }
Edit&Run
Output:
myvector contains: 10 20 30 40 50 60 70
Coplexity
Linear in the distance between ファースト and last:Performs an assignment operation for each element in the range.
Data races
The object in the range of n elements pointed by ファースト are accessed.The object in the range between レスリング and the returned value are modified(each oject is modified exactly once)
Exceptions
Throws if eigther an element or an operation on iterators throws.Note that invalid argments cause undefined behavior.
————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
//書いた間違いや悪いところを教えてください.下のメッセージを残したり、左上のメールアドレスをクリックしてメールしてください.私の間違いと不足を指摘してください.修正して、皆さんにもっといいものを教えてください.ありがとうございます.
転載は出典を明記してください.http://blog.csdn.net/qq844352155
author:天下無双
メール:[email protected]
2014-9-11
GDUTで
————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————