博客專欄

        EEPW首頁 > 博客 > C++ string中c_str()、data()、copy(p,n)函數的用法

        C++ string中c_str()、data()、copy(p,n)函數的用法

        發布人:電子禪石 時間:2020-02-18 來源:工程師 發布文章

        1、c_str():生成一個const char*指針,指向以空字符終止的數組。
        參考自:csqlwy - 博客園
        鏈接:www.cnblogs.com/qlwy/archive/2012/03/25/2416937.html(點擊尾部閱讀原文前往)

        ①因為c_str()返回的只是一個指向某字符串的指針,因此要么現用先轉換,要么把它的數據復制到用戶自己可以管理的內存中

        int main()
        {
            const char* c;
            string s="1234";
            c = s.c_str();
            cout<<c<<endl; //輸出:1234
            s="abcd";
            cout<<c<<endl; //輸出:abcd
            return 0;
        }12345678910


        在這里插入圖片描述
        我們可以使用strcpy等函數把需要的數據拷貝到另一個內存中,就能獨立開來。(推薦)。

        int main()
        {
            char* c=new char[20];
            string s="1234";
            //c = s.c_str();
            strcpy(c,s.c_str());
            cout<<c<<endl; //輸出:1234
            s="abcd";
            cout<<c<<endl; //輸出:1234
            return 0;
        }1234567891011



        在這里插入圖片描述
        ② c_str()返回一個客戶程序可讀不可改的指向字符數組的指針,不需要手動釋放或刪除這個指針。

        2、data():與c_str()類似,但是返回的數組不以空字符終止。

        3、copy(p,n,size_type _Off = 0):從string類型對象中至多復制n個字符到字符指針p指向的空間中。默認從首字符開始,但是也可以指定,開始的位置(記住從0開始)。返回真正從對象中復制的字符。------用戶要確保p指向的空間足夠保存n個字符。
        (從size_type _Off開始,賦值n個字符到p中)

        int main( )
        {
            using namespace std;
            string str1 ( "1234567890" );
            basic_string <char>::iterator str_Iter;
            char array1 [ 20 ] = { 0 };
            char array2 [ 10 ] = { 0 };
            basic_string <char>:: pointer array1Ptr = array1;
            basic_string <char>:: value_type *array2Ptr = array2;
        
            cout << "The original string str1 is: ";
            for ( str_Iter = str1.begin( ); str_Iter != str1.end( ); str_Iter++ )
                cout << *str_Iter;
            cout << endl;
        
            basic_string <char>:: size_type nArray1;
            // Note: string::copy is potentially unsafe, consider
            // using string::_Copy_s instead.
            nArray1 = str1.copy ( array1Ptr , 12 );  // C4996
            cout << "The number of copied characters in array1 is: "
                << nArray1 << endl;
            cout << "The copied characters array1 is: " << array1Ptr << endl;
        
            basic_string <char>:: size_type nArray2;
            // Note: string::copy is potentially unsafe, consider
            // using string::_Copy_s instead.
            nArray2 = str1.copy ( array2Ptr , 5 , 2  );  // C4996
            cout << "The number of copied characters in array2 is: "
                << nArray2 << endl;
            cout << "The copied characters array2 is: " << array2Ptr << endl;
        
        }1234567891011121314151617181920212223242526272829303132



        在這里插入圖片描述


        *博客內容為網友個人發布,僅代表博主個人觀點,如有侵權請聯系工作人員刪除。

        磁控管相關文章:磁控管原理


        漏電開關相關文章:漏電開關原理
        激光二極管相關文章:激光二極管原理


        關鍵詞: C++

        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 芮城县| 榆林市| 登封市| 鹤庆县| 思南县| 方城县| 龙江县| 渝中区| 高唐县| 习水县| 八宿县| 巨鹿县| 彭泽县| 麻城市| 安宁市| 日土县| 察哈| 宜兰县| 苏尼特左旗| 手游| 清原| 汉寿县| 郑州市| 辽阳县| 麻阳| 澄迈县| 伊川县| 专栏| 宜章县| 乌拉特中旗| 克什克腾旗| 瑞丽市| 金华市| 兴海县| 广丰县| 合作市| 通城县| 大新县| 墨玉县| 澎湖县| 桦南县|