數組長度計算

來源:互聯網
上載者:User

標籤:計算   vector容器   大小   簡單   back   寫代碼   test   length   rgs   

在C語言中,有sizeof()這個關鍵字。可以用來計算數組的大小(數組所佔位元組數),但不是長度。

不過可以用它除以數組單個元素所佔位元組數計算長度:

a[7] = {1,2,3,4,5,,6,7};

length =  sizeof(a)/sizeof(a[0]);

C++中可以用上述方法,不過還可以:

a[7] = {1,2,3,4,5,6,7} 

int *abeg = beg(a);

int *aend = end(a);

auto dis  = aend - abeg;

cout<<dis<<endl;

在C++中,建議用vector容器來代替數組,其長度直接用name.size()就可以算出。

java中計算數組大小:

public class TestJava {
    public static void main(String[] args) {
        int a[] = {1,2,3,4,5};
        System.out.println(a.length);
    }

 

python中計算數組大小:

a =  [1,2,3,4,5]

print( len(a) )

很簡單的東西,但是太久沒寫代碼後竟然還是忘了。真的要多練習才行。

 

數組長度計算

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.