C/C++开发指南

一、基本操作

字符串char*

转string

const char* s = "你好";
string str = string(s);

比较strncmp

C 库函数 int strncmp(const char *str1, const char *str2, size_t n) 把 str1 和 str2 进行比较,最多比较前 n 个字节。
参数
str1 – 要进行比较的第一个字符串。
str2 – 要进行比较的第二个字符串。
n – 要比较的最大字符数。
返回值
该函数返回值如下:

如果返回值 < 0,则表示 str1 小于 str2。
如果返回值 > 0,则表示 str1 大于 str2。
如果返回值 = 0,则表示 str1 等于 str2。
参考:https://www.runoob.com/cprogramming/c-function-strncmp.html

传递char*作为参数赋值

定义

void ApiManager::response_json(char** body, int code ,string& msg)
{
    
    

    cJSON* root = cJSON_CreateObject();
    cJSON_AddNumberToObject(root, "code", code);
    cJSON_AddStringToObject(root, "msg", msg.data());
    char *data = cJSON_Print(root);
     // 设置返回值
    char *temp = (char *)malloc(strlen(data)+1); 
	strcpy(temp,data);  //间接赋值
	*body = temp;  
    cJSON_Delete(root);
}
char* body;
string r = "ok";
response_json(body, 0, r);

调用

字符串string

方法

查找子串find

find() 函数用于在 string 字符串中查找子字符串出现的位置,如果没有查找到子字符串,那么会返回一个无穷大值 4294967295。它其中的两种原型为:

size_t find (const string& str, size_t pos = 0) const;
size_t find (const char* s, size_t pos = 0) const;

示例:

#include <iostream>
#include <string>
using namespace std;
int main(){
    
    
    string s1 = "first second third";
    string s2 = "second";
    int index = s1.find(s2,5);
    if(index < s1.length())
        cout<<"Found at index : "<< index <<endl;
    else
        cout<<"Not found"<<endl;
    return 0;
}

rfind() 函数

rfind() 和 find() 很类似,同样是在字符串中查找子字符串,不同的是 find() 函数从第二个参数开始往后查找,而 rfind() 函数则最多查找到第二个参数处,如果到了第二个参数所指定的下标还没有找到子字符串,则返回一个无穷大值4294967295。

请看下面的例子:

#include

#include <string>
using namespace std;
int main(){
    
    
    string s1 = "first second third";
    string s2 = "second";
    int index = s1.rfind(s2,6);
    if(index < s1.length())
        cout<<"Found at index : "<< index <<endl;
    else
        cout<<"Not found"<<endl;
    return 0;
}

运行结果:

Found at index : 6

find_first_of() 函数

find_first_of() 函数用于查找子字符串和字符串共同具有的字符在字符串中首次出现的位置。请看下面的代码:

#include <iostream>
#include <string>
using namespace std;
int main(){
    
    
    string s1 = "first second second third";
    string s2 = "asecond";
    int index = s1.find_first_of(s2);
    if(index < s1.length())
        cout<<"Found at index : "<< index <<endl;
    else
        cout<<"Not found"<<endl;
    return 0;
}

运行结果:

Found at index : 3

本例中 s1 和 s2 共同具有的字符是 ’s’,该字符在 s1 中首次出现的下标是3,故查找结果返回3。

截取字符串substr

头文件:

#include <string> //注意没有.h
#include <iostream>
#include <string>
using namespace std ;
void main()
{
    
    
    string s="ABCD";
    cout << s.substr(2) <<endl ; //从字符串下标为2的地方开始截取,截取到末尾,输出CD
    cout << s.substr(0,2) <<endl ; //从字符串下标为0的地方开始截取,截取长度为2,输出AB
    cout << s.substr(1,2) <<endl ; //输出BC
}

int 转字符串

int i = 0;
to_string(i);

long 转字符串

unsigned long i = 0;
to_string(i);

函数返回值值string

参考:https://wenku.baidu.com/view/bdbd512b874769eae009581b6bd97f192279bfc7.html

#include<iostream>
#include<string>
using namespace std;
string TestStringC_STR()
{
    
    
    string strTest = "This is a test.";
	return strTest;
}
int main()
{
    
    
    string str1 = TestStringC_STR();
	const char *pc = str1.c_str();
    cout << pc << std::endl;
	return0;
}
    

转换char*

string cpu = "test";
char *temp = (char *)malloc(cpu.length()+ 1 ); 
strcpy(temp,id);  //间接赋值
*cpu_uid = temp;

注意:一定要用一个string变量先接收函数返回值,不然因为strTest是局部变量,会立马为空

int

字符串转int

const char* v = "80";
atoi(v);

结构体

定义

typedef struct NALU {
    
    
    int naluType;
    int naluLen;
    unsigned char *pData;
} T_NALU, *PT_NALU;

赋值memset

T_NALU nalu;
memset(&nalu, 0, sizeof(T_NALU));
nalu->naluLen = pos-startCodeLen;
nalu->pData = realloc(pNalu->pData, nalu->naluLen);
// char* 赋值
memcpy(nalu->pData, gBuf+startCodeLen, nalu->naluLen);

指针方式赋值calloc

nalu = (T_NALU *)calloc(1, sizeof(T_NALU));

文件操作

写文件

fp = fopen(CONFIG_GB28181.data(),"w");
            fwrite(data,sizeof(char),strlen(data)+1,fp);
            fclose(fp);
            sync();

日期计算

引入头文件

#include <iostream>
#include <time.h>

获取当前日期时间

// 日期
time_t tm_time = time(0);
tm current_time = *localtime(&tm_time);
char time_str[100] = {
    
    0};
sprintf(time_str,"%d-%02d-%02d %02d:%02d:%02d", (1900 + gt->tm_year), (1 + gt->tm_mon), gt->tm_mday, gt->tm_hour, gt->tm_min, gt->tm_sec); 

// 时间
time_t tm_time = time(0);
tm current_time = *localtime(&tm_time);
char time_str[100] = {
    
    0};
sprintf(save_name,"%d-%02d-%02d", (1900 + gt->tm_year), (1 + gt->tm_mon), gt->tm_mday);    

比如计算3个月前的日期,计算60天后的日期,计算两年前的日期

https://blog.csdn.net/luchengtao11/article/details/107079786

打印

打印字符串

char* str = "112";
printf("%s",str);

打印数字

int d = 1;
printf("%d",d);

打印16进制

for(int i = 0;i<sps_nalu.naluLen;i++)
{
    
    
     printf("%2x ",sps_nalu.pData[i]);
}

猜你喜欢

转载自blog.csdn.net/Blueeyedboy521/article/details/124253900