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: \n";
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

猜你喜欢

转载自www.cnblogs.com/jieruishu/p/9442068.html
今日推荐