enter 發表於 2015-11-2 18:37:11

有關 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= "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;
}
頁: [1]
查看完整版本: 有關 vector, string, char[] 求大小和長度的用法