code.club

標題: 有關 vector, string, char[] 求大小和長度的用法 [打印本頁]

作者: enter    時間: 2015-11-2 18:37
標題: 有關 vector, string, char[] 求大小和長度的用法
#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;
}




歡迎光臨 code.club (http://code.club/) Powered by Discuz! X3.2