An array of vectors

Just wondering how to have an array of vectors.
I tried reading this....
https://www.codespeedy.com/array-of-vectors-in-cpp-stl/

I have something like this.
1
2
3
4
5
6
const unsigned width = 256, height = 256, no_elems = 262144; // no_elems is the size of the png image array.
    std::vector<unsigned char> image(no_elems);

string combined_file_path(string(filepath) + to_string(X) + ".png");
                encodeOneStep(combined_file_path.c_str(), image, width, height);  // Write the .png image file.


I want to have multiple images dynamically.
Last edited on
This is what I have so far but it doesn't work...It does not give errors but it outputs a blank image.
What I need is a vector within a vector.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    // https://www.geeksforgeeks.org/vector-of-vectors-in-c-stl-with-examples/



    const unsigned width = 256, height = 256, no_elems = 262144; // no_elems is the size of the png image array.
    std::vector<unsigned char> image(no_elems);
    std::vector<unsigned char> imageB(no_elems);

    vector<vector<unsigned char> > vec{ { image, image, image } };


    CString File_Path;
    char sztmp[1024];
    const char* filepath = " ";

    string combined_file_path(string(filepath) + to_string(X) + ".png");
    encodeOneStep(combined_file_path.c_str(), vec[1], width, height);  // Write the .png image file.
Last edited on
Looks like image would be empty here

I am trying to create an array of image vectors.

Line 14 gives an error. I'm trying do type casting all stuff. but getting conversion errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const unsigned width = 256, height = 256;
    const unsigned char no_elems = 262144; // no_elems is the size of the png image array.

   const unsigned char c = static_cast<char>(no_elems);

    vector<const unsigned char> vect{ c, c, c};


    CString File_Path;
    char sztmp[1024];
    const char* filepath = " ";

    string combined_file_path(string(filepath) + to_string(X) + ".png");
    encodeOneStep(combined_file_path.c_str(), vect[0], width, height);  // Write the .png image file. 
vector<const unsigned char> vect{ c, c, c};

That is a vector of characters that has three characters in it.
(What character has value 262144? Seems to be a bit outside the ASCII table.)

The 262144 is "magic". This is more obvious:
1
2
const size_t width = 256, height = 256;
const size_t no_elems = width * height * 4;

then,
1
2
using Image = std::vector<char>;
std::vector<Image> vect( 3, Image(no_elems) );

This vector has three "images". Each image is a vector that has no_elems characters.

The vect[0] is one vector that has no_elems characters.
Thanks for your reply.
Line 12 gives an error though.
C++ a reference of type (not const-qualified) cannot be initialized with a value of type

1
2
3
4
5
6
7
8
9
10
11
12
13
const size_t width = 256, height = 256;
    const size_t no_elems = width * height * 4;

    using Image = std::vector<char>;
    std::vector<Image> vect(3, Image(no_elems));

    CString File_Path;
    char sztmp[1024];
    const char* filepath = " ";

    string combined_file_path(string(filepath) + to_string(X) + ".png");
    encodeOneStep(combined_file_path.c_str(), vect[0], width, height);  // Write the .png image file.
We have no idea what the encodeOneStep() is and surely there is more in the error message too?
This is the encodeOneStep() function
1
2
3
4
5
6
7
8
9
//Encode from raw pixels to disk with a single function call
//The image argument has width * height RGBA pixels or width * height * 4 bytes
void encodeOneStep(const char* filename, std::vector<unsigned char>& image, unsigned width, unsigned height) {
    //Encode the image
    unsigned error = lodepng::encode(filename, image, width, height);

    //if there's an error, display it
    if (error) std::cout << "encoder error " << error << ": " << lodepng_error_text(error) << std::endl;
}



error message
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Build started...
1
1>main.cpp
1>C:\Users\Chris\source\repos\PNG\lodepng.cpp(705,35): warning C4267: '=': conversion from 'size_t' to 'unsigned short', possible loss of data
1>C:\Users\Chris\source\repos\PNG\lodepng.cpp(730,37): warning C4267: '=': conversion from 'size_t' to 'unsigned short', possible loss of data
1>C:\Users\Chris\source\repos\PNG\lodepng.cpp(747,38): warning C4267: '=': conversion from 'size_t' to 'unsigned short', possible loss of data
1>C:\Users\Chris\source\repos\PNG\BitmapRawPixels\BitmapRawPixels\main.cpp(37,47): error C2064: term does not evaluate to a function taking 1 arguments
1>C:\Users\Chris\source\repos\PNG\BitmapRawPixels\BitmapRawPixels\main.cpp(37,69): error C2660: 'encodeOneStep': function does not take 3 arguments
1>C:\Users\Chris\source\repos\PNG\BitmapRawPixels\BitmapRawPixels\main.cpp(15,6): message : see declaration of 'encodeOneStep'
1>C:\Users\Chris\source\repos\PNG\BitmapRawPixels\BitmapRawPixels\main.cpp(344,21): error C2065: 'image': undeclared identifier
1>C:\Users\Chris\source\repos\PNG\BitmapRawPixels\BitmapRawPixels\main.cpp(345,21): error C2065: 'image': undeclared identifier
1>C:\Users\Chris\source\repos\PNG\BitmapRawPixels\BitmapRawPixels\main.cpp(346,21): error C2065: 'image': undeclared identifier
1>C:\Users\Chris\source\repos\PNG\BitmapRawPixels\BitmapRawPixels\main.cpp(347,21): error C2065: 'image': undeclared identifier
1>C:\Users\Chris\source\repos\PNG\BitmapRawPixels\BitmapRawPixels\main.cpp(362,21): error C2065: 'imageB': undeclared identifier
1>C:\Users\Chris\source\repos\PNG\BitmapRawPixels\BitmapRawPixels\main.cpp(363,21): error C2065: 'imageB': undeclared identifier
1>C:\Users\Chris\source\repos\PNG\BitmapRawPixels\BitmapRawPixels\main.cpp(364,21): error C2065: 'imageB': undeclared identifier
1>C:\Users\Chris\source\repos\PNG\BitmapRawPixels\BitmapRawPixels\main.cpp(365,21): error C2065: 'imageB': undeclared identifier
1>C:\Users\Chris\source\repos\PNG\BitmapRawPixels\BitmapRawPixels\main.cpp(379,59): error C2065: 'imageB': undeclared identifier
1>Done building project "BitmapRawPixels.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


edit. Below is the counsel output after fixing some things.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Build started...
1
1>main.cpp
1>C:\Users\Chris\source\repos\PNG\lodepng.cpp(705,35): warning C4267: '=': conversion from 'size_t' to 'unsigned short', possible loss of data
1>C:\Users\Chris\source\repos\PNG\lodepng.cpp(730,37): warning C4267: '=': conversion from 'size_t' to 'unsigned short', possible loss of data
1>C:\Users\Chris\source\repos\PNG\lodepng.cpp(747,38): warning C4267: '=': conversion from 'size_t' to 'unsigned short', possible loss of data
1>C:\Users\Chris\source\repos\PNG\BitmapRawPixels\BitmapRawPixels\main.cpp(41,69): error C2664: 'void encodeOneStep(const char *,std::vector<unsigned char,std::allocator<_Ty>> &,unsigned int,unsigned int)': cannot convert argument 2 from '_Ty' to 'std::vector<unsigned char,std::allocator<_Ty>> &'
1>        with
1>        [
1>            _Ty=unsigned char
1>        ]
1>        and
1>        [
1>            _Ty=Image
1>        ]
1>        and
1>        [
1>            _Ty=unsigned char
1>        ]
1>C:\Users\Chris\source\repos\PNG\BitmapRawPixels\BitmapRawPixels\main.cpp(15,6): message : see declaration of 'encodeOneStep'
1>Done building project "BitmapRawPixels.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Last edited on


Got it working now...

 
I changed using Image = std::vector<char>; to using Image = std::vector<unsigned char>;


1
2
3
4
5
6
7
8
9
10
11
12
const size_t width = 256, height = 256;
    const size_t no_elems = width * height * 4;

    using Image = std::vector<unsigned char>;
    std::vector<Image> vect(3, Image(no_elems));

    CString File_Path;
    char sztmp[1024];
    const char* filepath = " ";

    string combined_file_path(string(filepath) + to_string(X) + ".png");
    encodeOneStep(combined_file_path.c_str(), vect[0], width, height);  // Write the .png image file 



Thanks so much for taking the time to help me out. Much appreciated!
Last edited on
Topic archived. No new replies allowed.