Bmp赋值
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
float Light = Imagedata[j][i];
if (step == 1)
{
outImagedata[i*width + j] = Light;
}
else if (step == 3)
{
outImagedata[i*width * 3 + j * 3 + 0] = Light;
outImagedata[i*width * 3 + j * 3 + 1] = Light;
outImagedata[i*width * 3 + j * 3 + 2] = Light;
}
}
}
简单判断大小端
#include <stdio.h>
static union{ char c[4];unsigned long mylong;} endian_test = { {'l','?','?','b'} };
#define ENDIANNESS ( (char) endian_test.mylong )
int main()
{
printf("type: %c\n",ENDIANNESS);
}
用字符串模拟多维数组
static string stringJoin(std::vector<string> &vs, const string &delim)
{
string s;
std::vector<string>::const_iterator citer = vs.begin();
while (citer != vs.end())
{
s += *citer;
if (++citer != vs.end())
{
s += delim;
}
}
return s;
}
static std::vector<std::string> stringSplit(const std::string& src, const std::string& delim)
{
std::vector<std::string> dst;
if (src.empty() || delim.empty())
return dst;
int nCount = 0;
std::string temp;
size_t pos = 0, offset = 0;
while ((pos = src.find_first_of(delim, offset)) != std::string::npos)
{
temp = src.substr(offset, pos - offset);
if (temp.length() > 0) {
dst.push_back(temp);
nCount++;
}
offset = pos + 1;
}
temp = src.substr(offset, src.length() - offset);
if (temp.length() > 0) {
dst.push_back(temp);
nCount++;
}
return dst;
}
//三维数组转字符串
static string allPointsJoin(std::vector<std::vector<std::vector<float>>> &allPoints)
{
//string
std::vector<std::string> allPointsStrVec;
for (auto var : allPoints)
{
std::vector<std::string> varStrVec;
for (auto var1 : var)
{
std::vector<std::string> var1StrVec;
for (auto var2 : var1)
{
//LOG_VAR(var2);
var1StrVec.push_back(std::to_string(var2));
}
string tmp2Str = stringJoin(var1StrVec, ",");
varStrVec.push_back(tmp2Str);
}
string tmp1Str = stringJoin(varStrVec, ";");
allPointsStrVec.push_back(tmp1Str);
}
string tmpallPointsStr = stringJoin(allPointsStrVec, "#");
return tmpallPointsStr;
}
static std::vector<std::vector<std::vector<float>>> allPointsSplit(string tmpallPointsStr)
{
std::vector<std::vector<std::vector<float>>> allPoints2;
std::vector<std::string> allPointsStrVec2 = stringSplit(tmpallPointsStr, "#");
for (auto var : allPointsStrVec2)
{
std::vector<std::vector<float>> allPoints2Temp1;
std::vector<std::string> varVec = stringSplit(var, ";");
for (auto var2 : varVec)
{
std::vector<float> allPoints2Temp2;
std::vector<std::string> var2Vec = stringSplit(var2, ",");
for (auto var3 : var2Vec)
{
float dtemp = std::stof(var3);
allPoints2Temp2.push_back(dtemp);
}
allPoints2Temp1.push_back(allPoints2Temp2);
}
allPoints2.push_back(allPoints2Temp1);
}
return allPoints2;
}
一维数组模拟多维数组
#if 0
{
std::cout << "//------------ 一维数组模拟二维数组 ------------// \n";
int a0row = 3;
int a0col = 5;
MYTYPE **a0 = new MYTYPE*[a0row];
for (int i = 0; i < a0row; i++)
a0[i] = new MYTYPE[a0col];
for (int i = 0; i < a0row; i++)
{
for (int j = 0; j < a0col; j++)
{
a0[i][j] = i*j;
std::cout << a0[i][j] << " ";
}
std::cout << "\n";
}
MYTYPE *b0 = new MYTYPE[a0row*a0col];
std::cout << "二维转一维 \n";
for (int i = 0; i < a0row; i++)
{
for (int j = 0; j < a0col; j++)
{
b0[i*a0col + j] = a0[i][j];
}
}
for (int i = 0; i < a0row*a0col; i++)
{
std::cout << b0[i] << " ";
}
std::cout << "\n";
int c0row = a0row;
int c0col = a0col;
MYTYPE **c0 = new MYTYPE*[c0row];
for (int i = 0; i < c0row; i++)
c0[i] = new MYTYPE[c0col];
std::cout << "一维再转二维 \n";
for (int i = 0; i < c0row; i++)
{
for (int j = 0; j < c0col; j++)
{
c0[i][j] = b0[i*c0col + j];
}
}
for (int i = 0; i < c0row; i++)
{
for (int j = 0; j < c0col; j++)
{
std::cout << c0[i][j] << " ";
}
std::cout << "\n";
}
//goto mainEnd;
}
{
std::cout << "//------------ 一维数组模拟三维数组 ------------// \n";
int num[5][4][3] = {
{ { 0, 1, 2 },{ 3, 4 ,5 },{ 6, 7, 8 },{ 9,10, 11 } },
{ { 12,13,14 },{ 15,16,17 },{ 18,19,20 },{ 21,22,23 } },
{ { 24,25,26 },{ 27,28,29 },{ 30,31,32 },{ 33,34,35 } },
{ { 36,37,38 },{ 39,40,41 },{ 42,43,44 },{ 45,46,47 } },
{ { 48,49,50 },{ 51,52,53 },{ 54,55,56 },{ 57,58,59 } },
};
int a0i = 5;
int a0j = 4;
int a0k = 3;
MYTYPE ***a0 = new MYTYPE**[a0i];
for (int i = 0; i < a0i; i++)
{
a0[i] = new MYTYPE*[a0j];
for (int j = 0; j < a0i; j++)
{
a0[i][j] = new MYTYPE[a0k];
}
}
for (int i = 0; i < 5; ++i)
{
for (int j = 0; j < 4; ++j)
{
for (int k = 0; k < 3; ++k)
{
a0[i][j][k] = num[i][j][k];
}
}
}
std::cout << "//--------------------//\n";
for (int i = 0; i < a0i; ++i)
{
for (int j = 0; j < a0j; ++j)
{
for (int k = 0; k < a0k; ++k)
{
printf("%4d", a0[i][j][k]);
}
printf("\n");
}
}
MYTYPE *b0 = new MYTYPE[a0i*a0j*a0k];
std::cout << "三维转一维 \n";
for (int i = 0; i < a0i; i++)
{
for (int j = 0; j < a0j; j++)
{
for (int k = 0; k < a0k; k++)
{
// b[i*(l*n)+j*l+k%l] = a[i][j][k];
b0[i*(a0k*a0j) + j*a0k + k] = a0[i][j][k];
}
}
}
for (int i = 0; i < a0i*a0j*a0k; i++)
{
std::cout << b0[i] << " ";
}
std::cout << "\n";
int c0i = a0i;
int c0j = a0j;
int c0k = a0k;
MYTYPE ***c0 = new MYTYPE**[c0i];
for (int i = 0; i < c0i; i++)
{
c0[i] = new MYTYPE*[c0j];
for (int j = 0; j < c0i; j++)
{
c0[i][j] = new MYTYPE[c0k];
}
}
std::cout << "一维再转三维 \n";
for (int i = 0; i < a0i; i++)
{
for (int j = 0; j < a0j; j++)
{
for (int k = 0; k < a0k; k++)
{
// b[i*(l*n)+j*l+k%l] = a[i][j][k];
c0[i][j][k] = b0[i*(c0k*c0j) + j*c0k + k];
}
}
}
std::cout << "//--------------------//\n";
for (int i = 0; i < c0i; ++i)
{
for (int j = 0; j < c0j; ++j)
{
for (int k = 0; k < c0k; ++k)
{
printf("%4d", c0[i][j][k]);
}
printf("\n");
}
}
goto mainEnd;
}
#endif // 0
struct MyStruct
{
int x;
int y;
MyStruct() {
x = 0;
y = 0;
}
bool operator==(const MyStruct&my)const {
return ((this->x == my.x) && (this->y == my.y));
}
bool operator!=(const MyStruct&my)const {
return ((this->x != my.x) || (this->y != my.y));
}
bool operator<(const MyStruct&my)const {
return ((this->x * 10000 + this->y) < (my.x * 10000 + my.y));
}
MyStruct& operator=(const MyStruct&my) {
this->x = my.x;
this->y = my.y;
return *this;
}
};
std::map<MyStruct, int> sMap;
MyStruct MyST1;
MyST1.x = 10000000;
MyST1.y = 10000001;
sMap.insert(std::pair<MyStruct, int>(MyST1, MyST1.x + MyST1.y));
MyStruct MyST2;
MyST2.x = 10000001;
MyST2.y = 10000000;
sMap.insert(std::pair<MyStruct, int>(MyST2, MyST2.x + MyST2.y));
#pragma pack(push) //保持对齐状态
#pragma pack(4) //设定为4字节对齐
strust test
{
char m1;
double m4;
int m3;
}
#pragma pack(pop) //恢复对齐状态
c++之代码片段(一)
于 2021-09-29 09:30:54 首次发布