zoukankan      html  css  js  c++  java
  • 有意思的记录C++(持续更新)

    1.数组指针

    void main(){
      int a[5]={1,2,3,4,5};
      int *ptr=(int *)(&a+1);
      printf("%d,%d",*(a+1),*(ptr-1));
    }

    输出结果为:2,5

    &a是数组指针,其类型为 int (*)[5];

    而指针加1要根据指针类型加上一定的值,不同类型的指针+1之后增加的大小不同。
    &a是长度为5的int数组指针,所以要加 5*sizeof(int),ptr实际是a[5]。
    但是prt与(&a+1)类型是不一样的(强制转换过的),所以prt-1只会减去sizeof(int*)。

    a,&a的地址是一样的,但意思不一样,a是数组首地址,也就是a[0]的地址,&a是对象(数组)首地址,a+1是数组下一元素的地址,即a[1],&a+1是下一个对象的地址,即a[5].

    2.string转换大小写

    #include <algorithm>
    #include <cctype>
    string s("hello");
    cout << "orginal:" << s << endl;
    //tolower
    transform(s.begin(), s.end(), s.begin(), (int(*)(int))tolower);
    cout << "lower:" << s << endl;
    //toupper
    transform(s.begin(), s.end(), s.begin(), (int (*)(int))toupper);
    cout << "upper:" << s << endl;

    3.获取当前时间的ms值

    #include <time.h>
    #include <sys/time.h>
    struct timeval tv;
    gettimeofday (&tv, NULL);
    uint64_t mseconds=tv.tv_sec * 1000 + tv.tv_usec / 1000;

    timeval用于指定时间值,结构如下

    timeval{
    time_t tv_sec; //秒 [long int]
    suseconds_t tv_usec; //微秒 [long int]
    };

    常用于计算一个方法的响应时间。

    4.数组的大小

    char a[] = "hello world";
    cout<< sizeof(a) << endl;// 12字节
    void Func(char a[100]){
    cout<< sizeof(a) << endl;// 4字节而不是100字节
    }

    a的类型是数组,所以sizeof(a)为数组大小。

    但当数组作为函数的参数进行传递时,该数组自动退化为同类型的指针。

    5.linux判断文件的存取权限

    #include <unistd.h>
    int access(const char *pathname, int mode);

    mode有F_OK(文件是否存在), R_OK(读?),W_OK(写?), X_OK(执行?)4钟,使用“或|“可进行权限组合。 

    返回值:所有权限都满足,返回0;如果不满足或者有错误,则返回-1.

    #include <unistd.h>
    if (-1 == access(file_name.c_str(), F_OK)) {
        cout << "file no exists" << endl;
    }

    6.linux下获取文件状态

    使用stat命令,对应函数为

    #include <sys/stat.h>
    #include <unistd.h>
    int stat(const char *file_name, struct stat *buf);

    返回值0表示成功,-1失败

    stat结构为

    struct stat {
        dev_t         st_dev;       //文件的设备编号
        ino_t         st_ino;       //节点
        mode_t        st_mode;      //文件的类型和存取的权限
        nlink_t       st_nlink;     //连到该文件的硬连接数目,刚建立的文件值为1
        uid_t         st_uid;       //用户ID
        gid_t         st_gid;       //组ID
        dev_t         st_rdev;      //(设备类型)若此文件为设备文件,则为其设备编号
        off_t         st_size;      //文件字节数(文件大小)
        unsigned long st_blksize;   //块大小(文件系统的I/O 缓冲区大小)
        unsigned long st_blocks;    //块数
        time_t        st_atime;     //最后一次访问时间(秒)
        time_t        st_mtime;     //最后一次修改时间(秒)
        time_t        st_ctime;     //最后一次改变时间(指属性)
    };

    使用

    #include <sys/stat.h>
    using namespace std;
    
    int main() {
      struct stat file_stat;
      stat("./test", &file_stat);
      cout << "mode:" << file_stat.st_mode << endl;
      cout << "size:" << file_stat.st_size << endl;
      cout << "last access time:" << file_stat.st_atime << endl;
      cout << "last modify time:" << file_stat.st_mtime << endl;
      cout << "last change time:" << file_stat.st_ctime << endl;
      return 1;
    }

    可以用来判断文件是否正在被使用(最后一次访问时间来判断)

    7.指针常量和常量指针

    指针常量:指针是常量,那指针不能更改指向;常量指针:常量的指针,指针所指内容不能修改。

    const T* ptr:常量指针,const离内容最近(T),那用来修饰内容,内容不能被修改。

    T* const ptr:指针常量,const离指针最近,那用来修饰指针,指针不能改指向。

    8.输出序列化容器

    #include <vector>
    #include <string>
    #include <iostream>
    #include <algorithm>
    #include <iterator>
    #include <sstream>
    using namespace std;
    vector<string> vec2;
    vec2.push_back("mazi");
    vec2.push_back("test");
    ostringstream os; 
    copy(vec2.begin(), vec2.end(), ostream_iterator<string>(os, ","));
    cout << os.str() << endl;</string>

    9.将m上调至n的倍数

    (m + n - 1) & ~(n-1)

    10.多重继承

    多重继承经常碰到钻石型继承,普通继承中Derived1、Derived2会各有Base成员的一个副本,导致Derived有Base的两个副本,编译时会出现错误
    error: request for member `value' is ambiguous
    虚拟继承实现该层次(derived1/derived2虚拟继承Base),Derived就只有一个Base成员。

    11.struct对齐

    • 数据成员对齐规则:结构(struct或联合union)的数据成员,第一个数据成员放在offset为0的地方,以后每个数据成员存储的起始位置要从该成员大小的整数倍开始(比如int在32位机为4字节,则要从4的整数倍地址开始存储)。 
    • 结构体作为成员:如果一个结构里有某些结构体成员,则结构体成员要从其内部最大元素大小的整数倍地址开始存储。(struct a里存有struct b,b里有char,int,double等元素,那b应该从8的整数倍开始存储。) 
    • 收尾工作:结构体的总大小,也就是sizeof的结果,必须是其内部最大成员的整数倍,不足的要补齐。 

    12.linux下锁的静态初始化

    最近经常遇到需要静态变量的锁或者条件变量,就是不知道怎么办,实际上互斥锁和条件变量都提供了2种创建方式:静态方式和动态方式。
    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;//静态,posix定义的宏
    pthread_mutex_init(&mutex, NULL);//动态
    pthread_cond_t cond = PTHREAD_COND_INITIALIZER;//静态
    int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr)//动态

    13.文件操作

    #include <cstdio>
    //打开关闭
    FILE* fopen(const char* filename, const char* mode);
    int fclose(FILE* stream);
    //
    int fprintf(FILE* stream, const char* format, ...);//writes the c string pointed by format to the stream
    size_t fwrite(const void* ptr, size_t size, size_t count, FILE* stream);//writes an array of count elements, each one with a size of size bytes, from the block of memory pointed by ptr to the current position in the stream
    //
    size_t fread(void* ptr, size_t size, size_t count, FILE* stream);//reads an array of count elements, each one with a size of size bytes, from the stream and stores them in the block of memory specified by the ptr
    int fscanf(FILE* stream, const char* format, ...);//reads data from the stream and stores them according to the parameter format into the locations pointed by the additional arguments
    //移动游标
    int fseek(FILE* stream, long int offset, int origin);//sets the pos indicator associated with the stream to a new pos(origin+offset), origin:SEEK_SET(beginning of file),SEEK_CUR(current pos of the file pointer),SEEK_END(END of file)
    long int ftell(FILE* stream);//get current position in stream(bytes)
    //清空缓存
    int fflush(FILE* stream);//将输出缓冲区中未写入磁盘的数据写入磁盘

    实例

    FILE* fptr = fopen("test.txt", "w+");
    const* str = "test";
    fwrite(str, 1, strlen(str), fptr);
    fflush(fptr);
    fseek(fptr, 0, SEEK_END);
    long int file_size = ftell(fptr);
    fclose(fptr);

    14.round up number(向上取整)

     7取反(~7)=1111,1000,数字加7进行与运算,清零后三位

    size_t round_up(size_t size){
        return (((size)+7) &~ 7);// 按8字节对齐
    }
  • 相关阅读:
    Hive的mysql安装配置
    Linux下的MySQL安装
    Hive的安装与基础指令
    浅谈数据库和数据仓库
    Hive的学习之路(理论篇)
    Spring---bean的命名
    Spring---单例模式(Singleton)的6种实现
    Spring---加载配置文件的几种方法(org.springframework.beans.factory.BeanDefinitionStoreException)
    Spring---配置文件概述
    Spring---Bean生命周期
  • 原文地址:https://www.cnblogs.com/whuqin/p/4982056.html
Copyright © 2011-2022 走看看