zoukankan      html  css  js  c++  java
  • c++笔记3

    一基本语法:

    1.1 字符串:支持标准C的 const char* pch=0/"";//不指向任何对象和指向空字符串。C++提供的string类可提供字符串的所有操作,最好是融合操作,string str;  const char *pc = "a character array"; str=pc;//先后关系很重要,这里是将C字符串扩展至C++类型
    ;C++串向C串转化必须用显示函数const char *str = s1.c_str();

        1.1.1判断空:if ( st.empty() )  或者if ( ! st.size() ) 

        1.1.2赋值:直接用赋值“=”或者初始化时的拷贝string st3( st );

         1.1.3判断相等、字符串拼接等直接用==  +=    +  + +等

         1.1.4字符串中的字符替代:用迭代器replace( str.begin(), str.end(), '.', '_' );

    1.2向量vector:有2种用法分别是数组习惯和 STL 习惯 :

    1.2.1数组用法:vector< int > ivec( 10, -1 ); //相当于int ivec[10]={-1};或者另一种初始化方法:int ira[10]={0,-1,-2,-3,-4,-5,-6,-7,-8,-9}; vector< int > ivec(ira,iar+10 );或者直接用另个向量赋值本向量;

     1.2.2STL用法:

    vector< string > text; //空Vector

    string word;

    while ( cin >> word ) {
    text.push_back( word );//添加元素个数和内容
    // ...
    }

    cout << "words read are: ";
    for ( vector<string>::iterator it = text.begin();//迭代器输出,it为指针类型。
    it != text.end(); ++it )
    cout << *it << ' ';

    cout<<endl;
    1.3复数(通过操作符重载支持加减乘除,用cout输出时是"(a+bi)"格式:如a+bi,complex< float /double/long double  > purei( a, b ); complex< float > real_num(a ); //虚部为0   complex< float > zero; zero//0,

    1.4位操作bitset:#include <bitset> 支持将某一位/全部置位、清零、翻转、读某位、判断某位是否为0,二进制字符串赋值、转字符串和转long型整数等。

    1.5 pair类型:类似于创造键值对

    typedef pair< string, string > Authors;
    Authors proust( "marcel", "proust" );
    Authors joyce( "james", "joyce" );
    Authors musil( "robert", "musil" );

    1.6链接指示符 extern “C” :告诉链接器后面的语言是用某种语言编写的,单一语句一般放在头文件中。

    1.7 函数的指针和指向函数的指针数组:typedef  返回值类型    (*pfuntypedef)(参数列表), ppfuntypedef   funarry[]={fun1,fun2}

     

  • 相关阅读:
    CentOS 7 下Emacs无法录入中文的问题
    GPS文件中的C1--->P1转换
    centos7上搭建http服务器以及设置目录访问
    在Linux和Windows之间的远程控制的实现
    Emacs中的代码折叠控制
    Fortran程序调试中的“吐核”错误
    CentOS 7.6 系统上添加最新版 NetCDF 4.6.1
    迁移 Emacs 的自定义设置
    CentOS 7系统上制作Clonezilla(再生龙)启动U盘并克隆双系统
    [CentOS 7] TexLive2017中kpsewhich Bug的修复
  • 原文地址:https://www.cnblogs.com/jieruishu/p/9442068.html
Copyright © 2011-2022 走看看