logo

Velikost vektorja C++ ()

Določa število elementov v vektorju.

postorder prehod binarnega drevesa

Sintaksa

Vzemimo vektor 'v' in število elementov 'n'. Sintaksa bi bila:

 int n=v.size(); 

Parameter

Ne vsebuje nobenega parametra.

Povratna vrednost

Vrne število elementov v vektorju.

Primer 1

Poglejmo preprost primer.

 #include #include using namespace std; int main() { vector v{&apos;Welcome to javaTpoint&apos;,&apos;c&apos;}; int n=v.size(); cout&lt;<'size of the string is :'<<n; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Size of the string is:2 </pre> <p>In this example, vector v containing two strings and size() function gives the number of elements in the vector.</p> <h2>Example 2</h2> <p>Let&apos;s see a another simple example.</p> <pre> #include #include using namespace std; int main() { vector v{1,2,3,4,5}; int n=v.size(); cout&lt;<'size of the vector is :'<<n; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Size of the vector is :5 </pre> <p>In this example, vector v containing integer values and size() function determines the number of elements in the vector.</p></'size></pre></'size>

V tem primeru vektor v vsebuje dva niza in funkcija size() podaja število elementov v vektorju.

Primer 2

Poglejmo še en preprost primer.

 #include #include using namespace std; int main() { vector v{1,2,3,4,5}; int n=v.size(); cout&lt;<\'size of the vector is :\'<<n; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Size of the vector is :5 </pre> <p>In this example, vector v containing integer values and size() function determines the number of elements in the vector.</p></\'size>

V tem primeru vektor v vsebuje celoštevilske vrednosti in funkcija size() določa število elementov v vektorju.