code.club

 找回密碼
 立即註冊
搜索
查看: 6680|回復: 0

有關 vector, string, char[] 求大小和長度的用法

[複製鏈接]
發表於 2015-11-2 18:37:11 | 顯示全部樓層 |閱讀模式
#include <iostream>
#include <vector>
using namespace std;

int main(int argc, char *argv[]) {
       
        vector<string> str1(3); //str1 has 3 elements
        string str2 = "hello";
        char str3[] = "HELLO";
        str1[0]= "Hello";
       
        cout<<"The size of vector str1 is "<<sizeof(str1)<<endl; //the size of vector str1
        cout<<"The size of string str2 is "<<sizeof(str2)<<endl; //the size of string str2
        cout<<"The size of char str3, including the ending \'\\0\' is "<<sizeof(str3)<<endl; //the size of char[] str3, including the ending '\0'
       
        cout<<"The elements of vector str1 is "<<str1.size()<<endl; //the elements of vector str1;
        cout<<"The lenght of string str2 is "<<str2.size()<<endl; //the length of string str2;
        cout<<"The length of string str2 is "<<str2.length()<<endl; //the length of string str2;
        cout<<"The length of char[] str3, excluding the ending \'\\0\' is "<<strlen(str3)<<endl; //the length of char[] str3, excludes the '\0'
               
        return 0;
}
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

小黑屋|手機版|Archiver|code.club  

GMT+8, 2024-3-29 08:53 , Processed in 0.106157 second(s), 20 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表