zoukankan      html  css  js  c++  java
  • c++面试(一)

    1.在c++中可以通过"::"来直接操作全局变量。

    2.i++与++i效率的比较。

    (1)內建数据类型时,他们的效率差别不大。

    (2)自定义数据类型(类等)的情况,(++i)可以返回对象的引用,而(i++)必须返回对象的值,所以导致在大对象的时候产生了较大的复制开销,效率低。

    3.假设浮点变量的名字为x,它与0.0比较:

    if((x<=-EPSINON)&&(x<=EPSINON))

    if((x<-EPSINON)||(x>EPSINON))

    4.注:当表达式中存在无符号类型时,所有的操作数都自动转换成无符。

    char getChar(int x,int y)

    {

      char c;

      unsigned int a=x;

      (a+y)>10?(c=1):(c=2);

      return c;

    }

    (1)(7,-7):-7转化成一个很大的数,与7相加正好溢出,其值为0。

    (2)(7,-8):-8转化成一个很大的数,与7相加差1就溢出了,返回1。

    5.两数交换,推荐使用按位异或的方式。

    void swap(int &a,int &b)    //采用引用传参的方式

    {

      a^=b;

      b^=a;

      a^=b;

    }

    6.atexit()函数,在mian()函数执行完后被执行。

    头文件:#include <stdlib.h>

    atexit()被调用的顺序与他们在main()中注册的顺序相反。

    7.sizeof()  //结构体、类等,大小需要对齐。

    (1)普通函数不占内存,只用虚函数占用一个指针大小的内存。  //virtual void print() {}

    (2)空类:class A{},编译器会安排一个指针大小的内存。

    (3)#pragma pack()  //强行改变对其大小

    #include <iostream.h>

    #pragma pack(2)

    struct test{

      char c;

      short s1;

      short s2;

      int i;

    }

    int main()

    {

      cout << sizeof(test)<<endl;

      return 0;

    }

    =>10

    8.inline 内联函数的适用场合:用来定义存取函数(set/get函数等)。

  • 相关阅读:
    concrete maths ch4 number theory
    Aho-Corasick algorithm
    Finding repetitions
    Suffix Automaton
    Z-function and its calculation
    mongodb安装与启动
    centos redis 集群搭建
    nginx实现热部署(平滑升级)
    nacos集群部署安装
    springboot+zookeeper+dubbo整合
  • 原文地址:https://www.cnblogs.com/define-ray/p/5393205.html
Copyright © 2011-2022 走看看