简体   繁体   English

复制算法与容器构造函数

[英]copy algorithm vs. container constructor

What advantages if any does the copy algorithm have over using the constructor of a container directly? 与直接使用容器的构造函数相比,复制算法有什么优势?

This example is from cplusplus.com: 此示例来自cplusplus.com:

// copy 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 (7);

  std::copy ( myints, 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;
}

Why should the copy approach be preferred to 为什么应该首选复制方式

std::vector<int> myvector(myints, myints+7);

This is an example of a poor example on cplusplus.com. 这是cplusplus.com上一个较差示例的示例。 If you had such an array and were really going to copy it into a vector, you would use the constructor. 如果您有这样的数组,并且确实要将其复制到向量中,则可以使用构造函数。 std::copy is useful in many other ways (eg if you wanted to output the array to a file or the console), but construction is not one of them. std::copy在许多其他方式中很有用(例如,如果要将阵列输出到文件或控制台),但构造方法不是其中之一。

Well, firstly, since you are using C++11: 好吧,首先,由于您使用的是C ++ 11:

int myints[]={10,20,30,40,50,60,70};
std::vector<int> myvector(myints, myints+7);

for (std::vector<int>::iterator it = myvector.begin(); it!=myvector.end(); ++it)
    std::cout << ' ' << *it;

could actually be written the following way: 实际上可以通过以下方式编写:

std::vector<int> myVector = {10,20,30,40,50,60,70};

for (const int i : myVector )
    std::cout << ' ' << i;
Secondly, you are comparing: 其次,您正在比较:
  • direct construction of vector using iterators vs. 使用迭代器直接构建向量
  • construction of vector, elements of which are going to be overwritten using std::copy . 向量的构造,其元素将使用std::copy覆盖。
If you want to construct a vector using C-style array / iterators, then: 如果要使用C样式数组/迭代器构造向量,则:
 std::vector<int> myVector(myints, myints+7); 
If you want to build another vector using existing one, then one of the following: 如果要使用现有矢量构建另一个矢量,请执行以下操作之一:
 std::vector<int> myVector2(myVector); std::vector<int> myVector3 = myVector; // invokes copy constructor anyway... 
If you have 2 vectors already and you want to overwrite one with another, then: 如果您已经有两个向量,并且想要覆盖另一个向量,则:
 myVector2 = myVector; 

... just don't optimize prematurely and let your code reflect your purpose. ...只是不要过早优化,而让您的代码反映您的目的。

It's not useful for building a new container because most of the time the container itself can take iterators 这对于构建新容器没有用,因为大多数情况下容器本身可以使用迭代器

You may have to use it if you're working with raw arrays. 如果使用原始数组,则可能必须使用它。 Eg: 例如:

std::vector<int> my_vect;
int* array_for_a_low_level_lib = new int[my_vect.size()];
std::copy(std::begin(my_vect), std::end(my_vect), array_for_a_low_level_lib);

Here are two of my favorites usage examples, adding iterator header: 这是我最喜欢的两个用法示例,其中添加了iterator头:

template <typename InnerType, typename Container>
void fill_container(Container& cont)
{
  std::copy(std::istream_iterator<InnerType>(std::cin), 
            std::istream_iterator<InnerType>(), 
            std::inserter(cont, cont.end()));
}

template <typename InnerType, typename Container>
void print_container(const Container& cont)
{
  std::copy(std::begin(cont), std::end(cont), 
    std::ostream_iterator<InnerType>(std::cout, " "));
}

http://ideone.com/4MOhSE http://ideone.com/4MOhSE

Also used with boost::range adapters you can do beautiful things, like filling vectors from maps in one quite readable line. 也可以与boost :: range适配器一起使用,您可以做一些漂亮的事情,例如将地图中的向量填充到一条可读的行中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM