cpp_trivial

  • C++中的零碎
  • 比较有意思的

1.字符串

  • stof 第一个float型的字符串转为float

  • 1
    2
    string str = "1.23_*_2.34_*";
    cout << stof(str)<< endl;//1.23
  • stoi 第一个int型的字符串转为int

  • 1
    2
    string str = "1.23_*_2.34_*";
    cout << stoi(str)<< endl;//1
  • stringstream 仅能处理空格

  • 1
    2
    3
    4
    5
    6
    7
    8
    #include <sstream>//!!!!!!!!!!!!
    string str = "1.23 2.34";
    stringstream ss(str);
    float num;
    while (ss >> num)
    {
    cout <<num << endl;//1.23 2.34//仅能处理空格
    }
  • 1
    2
    3
    4
    5
    6
    7
    8
    string str = "1.23_2.34";
    // cout << stoi(str)<< endl;//1
    stringstream ss(str);
    float num;
    while (ss >> num)
    {
    cout <<num << endl;//1.23
    }
  • strtok

    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      char* strtok( char* str, const char* delim );
      /*
      分解字符串为一组字符串。s为要分解的字符,delim为分隔符字符(如果传入字符串,则传入的字符串中每个字符均为分割符)。首次调用时,s指向要分解的字符串,之后再次调用要把s设成NULL。
      第一次分割之后,原字符串str是分割完成之后的第一个字符串,剩余的字符串存储在一个静态变量中,因此多线程同时访问该静态变量时,则会出现错误。
      */
      char input[100] = "A bird came down the walk";
      char *token = std::strtok(input, " ");
      while (token != NULL) {
      std::cout << token << '\n';
      token = std::strtok(NULL, " ");
      }
      /*
      A
      bird
      came
      down
      the
      walk
      */

2.常用函数

  • fastpow

  • 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
    double mypow3(double x, int n)
    {
    bool positive = n>0;

    long nn = n;
    nn = abs(nn);
    double ans = 1;
    double ret = x;
    for (long i = nn; i>0; i /= 2)
    {
    if ((i % 2) == 1)// 在最初为奇数 以及最后为奇数时 生效
    ans = ans*ret;
    ret = ret*ret;
    }
    if (!positive)
    ans = 1 / ans;
    return ans;
    }

    double myPow(double x,int n)
    {
    long nn=n;
    nn=abs(nn);
    bool positive=n>0;
    double ret= fastpow(x,nn);
    if(!positive)
    ret=1/ret;
    return ret;
    }
    double fastpow(double x,long n)
    {
    if(n==0)
    return 1;
    double ret=fastpow(x,n/2);
    if(n%2==0)
    ret=ret*ret;
    else
    ret=ret*ret*x;

    return ret;
    }

3.成员初始化

  • 静态常量成员

    • 1
      2
      3
      4
      5
      6
      7
      class B {
      public:
      static const int NUM = 5;// 静态int常量成员初始化,仅仅为声明
      };

      const int B::NUM;//类外定义!!!!
      nt bb = B::NUM;
    • 1
      2
      3
      4
      5
      6
      class B {
      public:
      static const int NUM ;// 静态int常量成员初始化
      };

      const int B::NUM=5;//类外定义
    • 1
      2
      3
      4
      class B {
      public:
      static const float NUM = 5;// 静态float常量成员初始化失败!!!!!!!
      };
    • 1
      2
      3
      4
      5
      class B {
      public:
      static const float NUM ;// 静态float常量成员初始化失败!!!!!!!
      };
      const float B::NUM=5;

4. 操作符重载

  • 运算符重载是针对C++原有运算符进行的,不可能通过重载创造出新的运算符

    除了

    . (成员访问运算符)

    .* (成员指针访问运算符)

    :: (域运算符)

    sizeof (长度运算符)

    ?: (条件运算符)这5个运算符外,其他运算符都可以重载

5. 取余操作

  • 除号的正负取舍和一般的算数一样,符号相同为正,相异为负
    求余符号的正负取舍和被除数符号相同
    -3/16=0 16/-3=-5 -3%16=-3 16%-3=1

6 函数重载

  • 原则是 函数名称,参数个数,参数类型

  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    class B {
    public:
    void ff() {}
    void ff() const {}
    void ff(int a) {}
    void ff(int a)const {}


    void ff(const int* p) {}
    void ff( int* const p) {} // void ff(int* p) {}// 冲突
    //void ff(int* p) {}// error
    };
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    void ff(int a)const {} //修饰this指针
    //this 原型Type * const this;
    //修饰后 const Type * const this;
    /*
    C++规定
    重载函数的指针形参
    如果要以const进行区分
    只有当修饰的const为底层const
    而非顶层const时才可以区分
    也就是说 const必须修饰指针指向的对象而非指针本身
    */
文章目录
  1. 1. 1.字符串
  2. 2. 2.常用函数
  3. 3. 3.成员初始化
  4. 4. 4. 操作符重载
  5. 5. 5. 取余操作
  6. 6. 6 函数重载