天天看點

STL algorithm算法copy_n(9) std::copy_n

原文位址: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;

}
           

運作截圖:

STL algorithm算法copy_n(9) std::copy_n

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) std::copy_n

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

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

繼續閱讀