“Rozmiar tablicy” Kod odpowiedzi

Rozmiar tablicy

#include <bits/stdc++.h>
using namespace std;     
int main()
{
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr)/sizeof(arr[0]);
    return 0;
}
Rich Rattlesnake

Rozmiar tablicy

// Alternately, use the containers from the STL, e. g. std::array or std::vector. 
// These containers behave very similar to standard arrays, but you can query their 
// size at runtime, no problem.


std::vector<int> array;
array.pushback(2);
array.pushback(7);
array.pushback(344);
array.pushback(45);
array.pushback(89);
array.pushback(28);
std::size_t length = array.size(); // easy!
Fancy Flatworm

Rozmiar tablicy

If all you have is the pointer to the first element then you can't:

int array[6]= { 1, 2, 3, 4, 5, 6 };
void main() 
  {
  int *parray = &(array[0]);
  int len=sizeof(array)/sizeof(int);
  printf("Length Of Array=%d\n", len);
  len = sizeof(parray);
  printf("Length Of Array=%d\n", len);
  getch();
}
// output: Will give two different values: 6, and 4.
Fancy Flatworm

Rozmiar tablicy

int array[] = {4,78,3,7,9,2,56,2,76,23,6,2,1,645};
std::size_t length = sizeof(array)/sizeof(int); // see solution 1
Fancy Flatworm

Rozmiar tablicy

<?php

$array = array(
    "f" => "f",
    "b" => "b",
);
print(sizeOf($array))
?>
Javasper

Rozmiar tablicy

// If you have s statically allocated array, you can determine the number of elements
//from the arrays name. However, if that is the case, and you don't use C++ functionality,
//then most likely there wouldn't be a need for this in the first place.

int array[10];
std::size_t length = 10; // big surprise, that number is right in the definition!
Fancy Flatworm

Odpowiedzi podobne do “Rozmiar tablicy”

Pytania podobne do “Rozmiar tablicy”

Więcej pokrewnych odpowiedzi na “Rozmiar tablicy” w C++

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu