如何正确地将uint8 *复制到vector <uint8>

时间:2016-09-23 21:36:33

标签: c++ sdl wav uint8t uint

我试图从Uint8 *制作Uint8的矢量,但是由于某些值不同。这是我的代码。

std::vector<Uint8> wav_vector = {}; 
Uint8* wav_buffer_;   
for (unsigned int i = 0; i < wav_length_; i++) {
  wav_vector.push_back(wav_buffer_[i]);
}

然后我试图验证这些值:

for (unsigned int i = 0; i < wav_length_; i++){
    if (wav_buffer_[i]!=wav_vector[i]){
      cout<<i<<endl;
      printf("Orignal Buffer %u\n", wav_buffer_[i]);
      printf("Vector Buffer %u\n", wav_vector[i]);

    }
  }

这些是我的示例输出:

Index: 0
Orignal Buffer 208
Vector Buffer 40

Index: 1
Orignal Buffer 72
Vector Buffer 3

Index: 2
Orignal Buffer 111
Vector Buffer 183

Index: 3
Orignal Buffer 1
Vector Buffer 97

Index: 4
Orignal Buffer 0
Vector Buffer 79

Index: 5
Orignal Buffer 0
Vector Buffer 127

Index: 8
Orignal Buffer 120
Vector Buffer 40

Index: 9
Orignal Buffer 251
Vector Buffer 3

Index: 10
Orignal Buffer 182
Vector Buffer 183

Index: 16
Orignal Buffer 0
Vector Buffer 176

Index: 17
Orignal Buffer 0
Vector Buffer 200

Index: 18
Orignal Buffer 0
Vector Buffer 109

Index: 19
Orignal Buffer 0
Vector Buffer 1

Index: 24
Orignal Buffer 0
Vector Buffer 176

Index: 25
Orignal Buffer 0
Vector Buffer 200

Index: 26
Orignal Buffer 0
Vector Buffer 109

Index: 27
Orignal Buffer 0
Vector Buffer 1

Index: 32768
Orignal Buffer 16
Vector Buffer 120

任何帮助都会非常感激。

注意: 我试过这个并且它有效。但我想使用for循环,因为我有一些其他音频格式需要位移。

std::vector<Uint8> wav_vector(&wav_buffer_[0], &wav_buffer_[wav_length_]);

1 个答案:

答案 0 :(得分:1)

看起来有std::vector的构造函数可以从迭代器(或指针)复制。

您可以执行以下操作:

std::vector<uint8_t> my_vector(&wave_buffer[0], &wave_buffer[N]);