【问题标题】:Using std::copy to copy into an std::deque使用 std::copy 复制到 std::deque
【发布时间】:2015-01-05 07:19:44
【问题描述】:

我有一个 C 风格的数组 unsigned char Temp[SERVER_BUFFER_SIZE],我想将它复制到最后的 std::deque 中(更像是附加它)

std::deque<unsigned char>* buffer = new std::deque<unsigned char>();
std::copy(Temp[0], Temp[SERVER_BUFFER_SIZE], buffer->cend());

我遇到了很多这样的错误:

错误 4 错误 C2825: '_Iter': must be a class or namespace when 后跟 '::' c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 371 1 a Error 5 error C2039: 'iterator_category' : is not a member of '`global 命名空间'' c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 371 1 a 错误 9 错误 C2825: '_Iter': 必须是类或命名空间后跟 '::' c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 372 1 a 错误 12 错误 C2602: 'std::iterator_traits<_init>::value_type' 不是 'std::iterator_traits<_init>' c:\program 基类的成员 文件 (x86)\microsoft visual studio 12.0\vc\include\xutility 372 1 a

【问题讨论】:

  • 你为什么使用new 来实例化你的队列?
  • 这样我就可以将它传递给该函数范围之外的其他地方
  • 最好使用std::unique_ptr&lt;std::deque&lt;unsigned char&gt;&gt; 来实现这一点。
  • 您可以通过从该函数返回将其传递到该函数范围之外的其他位置。

标签: c++


【解决方案1】:

我认为在堆中分配缓冲区没有任何意义。然而,有效的代码看起来像

std::deque<unsigned char>* buffer = new std::deque<unsigned char>();
std::copy( Temp, Temp + SERVER_BUFFER_SIZE, std::back_inserter( *buffer ) );

这是一个演示程序

#include <iostream>
#include <deque>
#include <iterator>

int main() 
{
    const size_t SERVER_BUFFER_SIZE = 6;
    unsigned char Temp[SERVER_BUFFER_SIZE] = "Hello";

    std::deque<unsigned char> *buffer = new std::deque<unsigned char>();

    std::copy( Temp, Temp + SERVER_BUFFER_SIZE, std::back_inserter( *buffer ) );

    for ( unsigned char c : *buffer ) std::cout << c;
    std::cout << std::endl;

    delete buffer;

    return 0;
}

输出是

Hello

【讨论】:

  • 更好地教人们也使用相关的构造函数,避免copy + back_inserter 的不必要的复杂性、冗长和低效。我想知道,当最简单的方法通常也是最有效的等等时,为什么他们会提出所有这些 Rube Goldberg 方案。啊。
  • @ 干杯和hth。 - Alf 我们不知道这些结构用于哪个任务。可能是双端队列已经创建并附加到其他地方。
  • 我的意思是“也”。还有insert 成员函数。根据上下文,构造函数或insert 将是合理的选择。
  • @Cheers 和 hth。 - Alf 我同意应该提到成员函数插入。但无论如何他必须知道如何正确使用标准算法 std::copy.:)
【解决方案2】:

错误告诉你你做错了什么。 std::copy 接受迭代器,你给它字符。 Temp[0] 和 Temp[SERVER_BUFFER_SIZE] 只是 unsigned char。

但你不需要std::copy,deque 本身需要一个两个迭代器的构造函数:

std::deque<unsigned char>* buffer = new std::deque<unsigned char>(Temp, 
                                                Temp + SERVER_BUFFER_SIZE);

或者只是:

std::deque<unsigned char> buffer(Temp, Temp + SERVER_BUFFER_SIZE);

【讨论】:

    【解决方案3】:

    您不需要std::copy 来使用std::deque 执行此操作。 std::deque 具有成员函数 std::deque::insert,您可以通过以下方式将范围附加到它:

    #include <iostream>
    #include <deque>
    #include <memory>
    
    int main() {
      std::deque<unsigned char> dq;
      unsigned char buf[] = { 1, 2, 3, 4 };
    
      dq.insert(dq.end(), buf, buf + 4);
    
      for (auto i : dq) std::cout << i << " ";
      std::cout << std::endl;
    
      return 0;
    }
    

    如果 c++11 是一个选项,最好不要使用原始指针,而是使用像 std::unique_ptr 这样的智能指针:

    #include <iostream>
    #include <deque>
    #include <memory>
    
    int main() {
      std::unique_ptr<std::deque<unsigned char>> dq(new std::deque<unsigned char>);
      unsigned char buf[] = {1, 2, 3, 4};
    
      dq->insert(dq->end(), buf, buf + 4);
    
      for(auto i : *dq) std::cout << i << " ";
      std::cout << std::endl;
    
      return 0;
    }
    

    【讨论】:

      【解决方案4】:

      我会这样做

      #include <iostream>
      #include <deque>
      #include <iterator>
      
      int main()
      {
        const size_t SERVER_BUFFER_SIZE = 6;
        unsigned char Temp[SERVER_BUFFER_SIZE] = "Hello";
      
        std::deque<unsigned char> buffer(Temp, Temp+SERVER_BUFFER_SIZE);
      
        for ( auto c : buffer ) 
          std::cout << c;
      }
      

      我用一个范围来构建它,好东西,它实际上在牌组中放置了值。如果你有大对象而不是字符,它会比你的方法更快。 专家提示: 不要在不需要的地方使用原始指针。在 C++ 中这是不好的做法。

      【讨论】:

      • 你为什么使用难看的大写字母。考虑宏的命名约定及其用途。那又是什么?
      • 与 OP 的命名约定保持一致。
      猜你喜欢
      • 1970-01-01
      • 2011-05-05
      • 2010-10-12
      • 1970-01-01
      • 1970-01-01
      • 2021-08-25
      • 1970-01-01
      • 2011-09-24
      • 2012-11-25
      相关资源
      最近更新 更多