我想從Uint8 *製作Uint8的矢量,但由於某些值不相同。這是我的代碼。如何正確地將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_]);
你'wav_buffer_'指針被正確初始化或只是指着的地方? –
旁註:從數組中初始化一個'vector'不需要顯式循環:'std :: vector wav_vector(wav_buffer_,wav_buffer_ + wav_length _);'完成。 –
ShadowRanger
它指向構造函數的某處。 Uint8 * wav_buffer_; 的AudioFile ::的AudioFile(SDL_AudioSpec * wav_file,UINT8 * wav_buffer, UINT32 wav_length):wav_file_(wav_file),wav_buffer_(wav_buffer) wav_length_(wav_length){// SDL_FreeWAV(wav_buffer); } –