简体   繁体   中英

c++ array to vector issue

Im trying to copy an array to a vector, however, when the data is copied to the vector its different from that of the original array.

int arraySize = 640000;

std::vector<unsigned char> vector_buffer;
unsigned char buffer[arraySize];

populateArray(&buffer);

for(int i = 0; i < arraySize; i++)
     cout << buffer[i];  // this prints out data


std::copy ( buffer, buffer + arraySize, std::back_inserter(vector_buffer)); 


for(int i = 0; i < arraySize; i++)
     cout << vector_buffer[i];  // this prints out different data   

The data seems to get compressed somehow. Any approach at copying the array to a vector does the same thing.

Im using it to create a video from images. If i use the array data all is well, but if i use the vector data it doesn't work.

Any help would be highly appreciated.

Cheers

The

int arraySize = 640000;

needs to be const in standard C++. g++ allows variable length arrays as a C99-inspired language extension. It's best to turn that extension off. :-)

std::vector<unsigned char> vector_buffer;
unsigned char buffer[arraySize];

OK when arraySize is const , but will not compile with eg Visual C++ with your original code.

populateArray(&buffer);

This should most probably be populateArray(buffer) , unless you have a really weird declaration of populateArray .

for(int i = 0; i < arraySize; i++)
     cout << buffer[i];  // this prints out data

The above prints the data with no spacing between the elements. Better add some spacing. Or newlines.

std::copy ( buffer, buffer + arraySize, std::back_inserter(vector_buffer)); 

Better just use the assign method of std:.vector , like vector_buffer.assign( buffer, buffer + arraySize ) .

for(int i = 0; i < arraySize; i++)
     cout << vector_buffer[i];  // this prints out different data   

Again, this displays the elements with no spacing between.

Is the apparent problem there still when you have fixed these things?

If so, then please post also your populateArray function.

I can see nothing wrong with your code. The following code

#include <iostream>
#include <vector>

int main()
{
    const std::size_t arraySize = 640000;

    unsigned char buffer[arraySize];

    for(std::size_t idx = 0; idx < arraySize; ++idx)
        buffer[idx] = idx;

    std::vector<unsigned char> vector_buffer(buffer, buffer + arraySize);
    //std::vector<unsigned char> vector_buffer;
    //std::copy (buffer, buffer + arraySize, std::back_inserter(vector_buffer)); 

    for(std::size_t idx = 0; idx < arraySize; ++idx)
        if( buffer[idx] != vector_buffer[idx] )
        {
            std::cout << "error @" << idx << '\n';
            return 1;
        }
    std::cout << "Ok.\n";

    return 0;
}

prints Ok. for me. (Even if I use the less-than-optimal way of copying into the vector.)

From the fact that the code you showed wouldn't compile I conclude that you're not showing the real code. Please do so. Somewhere in the differences between your real code and my code must be the problem.

I've written a complete compilable program for you. The code appears fine. I run it and get expected output. Perhaps you need to re-check the code you posted against the real code.

#include <cstdlib>
#include <vector>
#include <iostream>
#include <iterator>
using namespace std;

void populateArray(unsigned char* buf, size_t buf_size)
{
    unsigned char* buf_end = &buf[buf_size];
    for( unsigned char c = 'A'; buf != buf_end; c = (c=='Z'?'A':c+1), ++buf )
        *buf = c;
}

int main()
{

    static const int arraySize = 64;

    std::vector<unsigned char> vector_buffer;
    unsigned char buffer[arraySize];

    populateArray(buffer, sizeof(buffer));

    for(int i = 0; i < arraySize; i++)
             cout << buffer[i];  // this prints out data

    cout << endl;


    std::copy ( buffer, buffer + arraySize, std::back_inserter(vector_buffer)); 


    for(int i = 0; i < arraySize; i++)
             cout << vector_buffer[i];  // this prints out different data   

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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