C++语法基础--标准库类型--string(1)

来源:互联网 发布:node http 模块 编辑:程序博客网 时间:2024/06/13 05:12
1.命名空间的using声明
  作用:
      提供一种更简洁的方式来使用命名空间成员,一旦用了using声明,就可以直接引用名字,而不需要引用该名字的命名空间
  格式:
      using namespace::name;
   eg:
     #include<iostream> 
        using std::cout;
      int main(void)
      {
         cout<<"hello";      
//如果没有using std::cout声明,则要写成std::cout<<"hello";
         return 0;
       }


2.在自定义的头文件中,建议使用完全限定的标准库名字


3.stirng
  *常见构造函数
          default (1) string();
              copy (2)        string (const string& str);
      substring (3)        string (const string& str, size_t pos, size_t len = npos);
 from c-string (4)       string (const char* s);
   from buffer (5)        string (const char* s, size_t n);
                  fill (6)         string (size_t n, char c);
             range (7)        template <class InputIterator>
                                     string  (InputIterator first, InputIterator last);

  eg:

   std::string s0 ("Initial string");


 // 对应上文的顺序
  std::string s1;
  std::string s2 (s0);
  std::string s3 (s0, 8, 3);
  std::string s4 ("A character sequence", 6);
  std::string s5 ("Another character sequence");
  std::string s6 (10, 'x');
  std::string s7a (10, 42);
  std::string s7b (s0.begin(), s0.begin()+7)
  
   //Sn的对应结果
   s1: 
   s2: Initial string
   s3: str
   s4: A char
   s5: Another character sequence
   s6: xxxxxxxxxx
   s7a: **********
   s7b: Initial




4.C风格的字符窜函数
  #include<cstring>
  int strlen(const char*);
  int strcmp(char *str1, char *str2);
  char *stpcpy(char *destin, char *source);
  char *strcat(char *destin, char *source);





5.C风格的字符长度可以为0

   eg:

     const char *pstr="";



6.#include<string>
  method:
        size(),empty()

eg:
  string str("hello");
//size为5


7.string类型能够自动将C风格的字符串转换成string对象。
    int main()
   {
string str("hello");
str+=",";
str+="world";
cout<<str<<endl;                                   
//输出hello,world
        const char *cstr=str.c_str();                 //cstr必须定义为const char*
cout<<cstr<<endl;                                //输出hello,world
        replace(str.begin(),str.end(),',','_');      //replace()包含在#include<algorithm>
cout<<str<<endl;                                  //输出hello_world
return 0;
   }


8.find(),find_first_of():返回匹配子串的第一个字符的索引位置。
 int main()
{
string str("hello,how are you!");
string::size_type pos=str.find("how");
if(pos!=string::npos)
{
cout<<"substring pos: "<<pos<<endl; //substring pos:6
}
string str1("aeiou");

string::size_type pos1=0;


//从str中找出所有aeiou出现的位置
while((pos1=str.find_first_of(str1,pos1))!=string::npos)
{
str[pos1]='*';  //把str的所有a,e,i,o,u替换成*
pos1++;


}
cout<<str;   //输出h*ll*,h*w *r* y**!
return 0;
}


9.substr(int,int)
  应用:去掉字符串空格和标点
   int main()
{
string str("hello,how are you!");
string str1(", !");
vector<string> vec;
string::size_type pos=0,prepos=0;
while((pos=str.find_first_of(str1,pos))!=string::npos)
{
vec.push_back(str.substr(prepos,pos-prepos));
//取得str的子串
prepos=++pos;


}


for(vector<string>::iterator it=vec.begin();it!=vec.end();it++)
{
cout<<*it<<"[]";//为了证明所有空格已别去除,这里用[]代替原空格和,!
                          //输出hello[]how[]are[]you[]
}


return 0;

}


10.rfind():查找最后(最右)的指定子串的出现
  find_first_not_of():查找第一个不与要搜索字符串的任意相匹配的字符
  find_last_of():查找字符串中的与搜索字符串任意元素相匹配的最后 一个字符
  find_last_not_of():查找字符串中的与搜索字符串任意元素全不相匹配的最后 一个字符
   int main()
  {
string str("hello,how are you");
string::size_type pos1,pos2,pos3,pos4;
pos1=str.rfind('o');
//15
pos2=str.find_first_not_of('o');//0
pos3=str.find_last_not_of('o');//16
pos4=str.find_last_of('o');//15
return 0;

    }


11.一个C风格字符串的类型是const char*
原创粉丝点击