c_cpp 将一个字符串复制到另一个

.c
// 1st way
display_meta->text_params[i].display_text = g_malloc0 (MAX_DISPLAY_LEN);
offset = snprintf(display_meta->text_params[i].display_text, MAX_DISPLAY_LEN, text_params[i].display_text); 

//2nd way
strcpy(str2, str1); 

c_cpp 串联/添加两个字符串

.c
strcat(frame_label_text,"Safe Zone, ");

c_cpp 在字符串中添加整数值

.c
offset = snprintf(txt_params->display_text , MAX_DISPLAY_LEN, "CLS: = %s ", frame_label_text);

c_cpp 在struct中初始化struct

.c
#include <stdio.h>
struct point{
    int x;
    int y;
};
struct student{
    int marks;
    struct point p1;
};
int main() {

    struct student s1 = { .marks=10, { .x=3,
                                .y=5}};
    printf("marks : %d\n", s1.marks );
    printf("point x : %d\n", s1.p1.x );
    printf("point y : %d\n", s1.p1.x );
    return 1;
}

c_cpp 1.两个总和

0001.cpp
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> res(2);
        unordered_map<int, int> mp;
        
        for (int i = 0; i < nums.size(); i++) {
            if (mp.count(target - nums[i])) {
                res[0] = mp[target - nums[i]];
                res[1] = i;
                return res;
            }
            else {
                mp[nums[i]] = i;
            }
        }
        
        return res;
    }
};

c_cpp PRINTF uint64_t

PRINTF uint64_t

printf64.h
#define __STDC_FORMAT_MACROS
#include <inttypes.h>

uint64_t i;
printf("%"PRIu64"\n", i);

c_cpp Big / Little Endian解压缩整数

unpack.h
/********************************************************************************
*
*  Copyright (c) Simon Downey <simon@ark.io> <https://github.com/sleepdefic1t>
*
*  The MIT License (MIT)
*
*  Permission is hereby granted, free of charge, to any person obtaining a copy of
*  this software and associated documentation files (the "Software"), to deal in
*  the Software without restriction, including without limitation the rights to
*  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
*  the Software, and to permit persons to whom the Software is furnished to do so,
*  subject to the following conditions:
*
*  The above copyright notice and this permission notice shall be included in all
*  copies or substantial portions of the Software.
*
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
*  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
*  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
*  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
*  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*********************************************************************************/
 
#include <stdint.h>

//////////////////////////////////////////////////////////////////////

uint16_t unpack2LE(uint8_t *src) {
  return ((uint16_t)src[0] & 0xFF)      |
         ((uint16_t)src[1] & 0xFF) << 8U;
}

uint32_t unpack4LE(uint8_t *src) {
  return ((uint32_t)src[0] & 0xFF)                 |
         ((uint32_t)src[1] & 0xFF)          <<  8U |
         ((uint32_t)src[2] & 0xFFFF)        << 16U |
         ((uint32_t)src[3] & 0xFFFFFFFF)    << 24U;
}

uint64_t unpack8LE(uint8_t *src) {
  return ((uint64_t)src[0] & 0xFF)                         |
         ((uint64_t)src[1] & 0xFF)                  <<  8U |
         ((uint64_t)src[2] & 0xFFFF)                << 16U |
         ((uint64_t)src[3] & 0xFFFFFFFF)            << 24U |
         ((uint64_t)src[4] & 0xFFFFFFFF)            << 32U |
         ((uint64_t)src[5] & 0xFFFFFFFFFFFFFFFF)    << 40U |
         ((uint64_t)src[6] & 0xFFFFFFFFFFFFFFFF)    << 48U |
         ((uint64_t)src[7] & 0xFFFFFFFFFFFFFFFF)    << 56U;
}

uint16_t unpack2BE(uint8_t *src) {
  return ((uint16_t)src[1] & 0xFF)  <<  8U |
         ((uint16_t)src[0] & 0xFF);
}

uint32_t unpack4BE(uint8_t *src) {
  return ((uint32_t)src[3] & 0xFFFFFFFF)    << 24U |
         ((uint32_t)src[2] & 0xFFFF)        << 16U |
         ((uint32_t)src[1] & 0xFF)          <<  8U |
         ((uint32_t)src[0] & 0xFF);
}

uint64_t unpack8BE(uint8_t *src) {
  return ((uint64_t)src[7] & 0xFFFFFFFFFFFFFFFF)    << 56U |
         ((uint64_t)src[6] & 0xFFFFFFFFFFFFFFFF)    << 48U |
         ((uint64_t)src[5] & 0xFFFFFFFFFFFFFFFF)    << 40U |
         ((uint64_t)src[4] & 0xFFFFFFFF)            << 32U |
         ((uint64_t)src[3] & 0xFFFFFFFF)            << 24U |
         ((uint64_t)src[2] & 0xFFFF)                << 16U |
         ((uint64_t)src[1] & 0xFF)                  <<  8U |
         ((uint64_t)src[0] & 0xFF);
}

//////////////////////////////////////////////////////////////////////

c_cpp 数组与矢量C ++

array_vs_vector
# Defining and initializing vectors

vector<T> v1; // vector that holds object of type T, default constructor v1 is empty

vector<T> v2 (v1); // v2 is a copy of v1

vector<T> v3(n, i); // v2 has n elements with value i

vector<T> v4(n); // v4 has n copies of a value-initialized object

# The size of a vector
vector<int>::size_type

vector<int> v;

for (vector<int>::size_type ix = 0; ix != 10; ++ix) {
    // v[ix] = ix; // wrong
    v.push_back(ix);
}

# Iterator

vector<int> v(10, 1);

for (vector<int>::iterator iter = v.begin(); iter != v.end(); ++iter) {
    cout << *iter << endl;
}

c_cpp Arduino用delimer分割字符串

Split.ino
This has always worked for me

// String  var = getValue( StringVar, ',', 2); // if  a,4,D,r  would return D        
String getValue(String data, char separator, int index)
{
    int found = 0;
    int strIndex[] = { 0, -1 };
    int maxIndex = data.length();

    for (int i = 0; i <= maxIndex && found <= index; i++) {
        if (data.charAt(i) == separator || i == maxIndex) {
            found++;
            strIndex[0] = strIndex[1] + 1;
            strIndex[1] = (i == maxIndex) ? i+1 : i;
        }
    }
    return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}  // END

c_cpp 1162.地图分析

- [1162。地图分析](https://leetcode-cn.com/contest/weekly-contest-150/problems/as-far-from-land-as-possible/) <br/> <br/> - `dp [i ] [j]`表示`i`行`j`列的网格到离它最近的陆地区域的距离<br/> - `dp [i] [j] = min(dp [i] [j], min(dp [i-1] [j] + 1,dp [i] [j-1] + 1))`<br/> - `dp [i] [j] = 0,如果是grid [i-1 ] [j-1] = 1` <br/> <br/>

Solution.cpp
class Solution {
public:
	int maxDistance(vector<vector<int>>& grid) {
		int n = grid.size();
		int dp[n + 2][n + 2];
		int count = 0;
		for (int i = 0; i < n + 2; i++) {
			for (int j = 0; j < n + 2; j++) {
				dp[i][j] = 1e6;
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (grid[i][j] == 1) { 
                    dp[i + 1][j + 1] = 0; 
                    count++; 
                }
			}
		}
		if (count == 0 || count == n * n) {
            return -1;
        }
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				dp[i][j] = min(dp[i][j], min(dp[i - 1][j] + 1, dp[i][j - 1] + 1));
			}
		}
		for (int i = n; i >= 1; i--) {
			for (int j = n; j >= 1; j--) {
				dp[i][j] = min(dp[i][j], min(dp[i + 1][j] + 1, dp[i][j + 1] + 1));
			}
		}
		int ans = dp[1][1];
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				// cout << dp[i][j] << " ";
				ans = max(ans, dp[i][j]);
			}
			//  cout<<endl;
		}
		return ans;
	}
};