Now I want to use std::vector<char>*vec and std::vector<char>&vec in above block code instead of std::vector<char>vec, I tried a lot but not getting any desired solution, please help me out.
Note that vec[i] = 255-vec[i]; may be out side the range of values that char can hold.
Consider using unsigned char to represent the bytes in the file.
That doesn't tell me what standard you're using. To compile the code given by JLBorges you need to have your compiler set to use the C++11 or higher standard. It appears that you're probably compiling as a C++98 standard.
I am sorry to bother you guys again, I am very much thankful to get the code, it is instructed from the University like :
Pass the file as reference as std:vector<char> to the function and invert it,
Pass the file as Pointer to std:vector<char> to the function and invert it,
Pass the file as character array the function and invert it.
They only diifer from each other in passing way.
Q-1) Where it was done using std:vector<unsigned char> , code is above, so I am confused is that assignment wrong or it is possible according to requirement ?
Q-2) Using command line argument they are saying to call the application like below :
ImageConvert InputImage.bmp ResultImage.bmp USE_REFERENCE // Code I got above.
ImageConvert InputImage.bmp ResultImage.bmp USE_POINTER //Trying
ImageConvert InputImage.bmp ResultImage.bmp USE_VECTOR // Done by me
ImageConvert InputImage.bmp ResultImage.bmp USE_CHRACTERARRAY //Trying
> is that assignment wrong or it is possible according to requirement ?
Technically, the assignment is wrong; the value of 255-vec[i] may not fit into a char
> it is possible according to requirement ?
Yes; narrowing by itself is not an error. Because char is an octet and because the representation is a two's complement representation on every platform, we can get away with using a vector of char
This would be typical for mainstream implementations:
#include <iostream>
#include <limits>
int main()
{
using limits = std::numeric_limits<char> ;
std::cout << std::boolalpha << "properties of the type char:\n"
<< " signed integer type: " << limits::is_signed << '\n'
<< " minimum integer value: " << int( limits::min() ) << '\n'
<< " maximum integer value: " << int( limits::max() ) << "\n\n" ;
for( unsignedchar byte : { 20, 200 } ) // say, the values of two bytes in the bmp file
{
char ch = byte ;
std::cout << "\ninteger value of byte is " << int(byte) << '\n'
<< " integer value of ch is " << int(ch) << '\n'
<< " integer value of 255-byte is " << (255-byte) << '\n'
<< " integer value of 255-ch is " << (255-ch) << '\n' ;
ch = 255 - ch ;
std::cout << " integer value of 255-ch narrowed to char is " << int(ch)
<< "\n---------------------------------\n" ;
}
}
properties of the type char:
signed integer type: true
minimum integer value: -128
maximum integer value: 127
integer value of byte is 20
integer value of ch is 20
integer value of 255-byte is 235
integer value of 255-ch is 235
integer value of 255-ch narrowed to char is -21
---------------------------------
integer value of byte is 200
integer value of ch is -56
integer value of 255-byte is 55
integer value of 255-ch is 311
integer value of 255-ch narrowed to char is 55
---------------------------------
properties of the type char:
signed integer type: true
minimum integer value: -128
maximum integer value: 127
integer value of byte is 20
integer value of ch is 20
integer value of 255-byte is 235
integer value of 255-ch is 235
integer value of 255-ch narrowed to char is -21
---------------------------------
integer value of byte is 200
integer value of ch is -56
integer value of 255-byte is 55
integer value of 255-ch is 311
integer value of 255-ch narrowed to char is 55
---------------------------------