C++學習day8之string

#include<iostream>
#include<cstring>
using namespace std;
/*
 *string 其實不是關鍵字,而是std下面的一個類。
 *
 *
 */ 
int main()
{
    /*
     *類的初始化的方式還有很多,比如string str("china");
     *string str="hello"
     */ 
    //string str("china");
    /*
     *由於C語言中沒有string類型,故必須通過string類對象的成員函數c_str把
     *string對象轉換成C中的字符串樣式
     *C++裏面string裏面還有個交換的成員函數swap,用於string的交換
     *用法如下
     */


    /*
     *再講一下查找吧。
     *int find(char c,int pos=0);
     *查找一個字符的位置,第一個參數就是你要查找的字符,第二個就是我
     *要查找的下標開始的位置。
     *返回第一次出現的下標。
     *
     *int find(char *s,int pos=0);
     *
     */
    string str1="hello";
    string str2="world";
    int i;
   // char c[10];
   // strcpy(c,str1.c_str());
    str1.swap(str2);
    i=str1.find("or",0);
    cout<<i<<endl;
    cout<<str1<<"........"<<str2<<endl;
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章