mardi 25 décembre 2018

How to count the total number of elements in a vector in c++? Why is it showing multiple repeating numbers?

#include <vector>
#include <iostream>

void countnumbers (std::vector<int> arr1)
{
    for (int j = 0; j < arr1.size(); j++)
    {

        if (arr1[j] > 0)
        {
           std::cout<<arr1.size();
        }
    }
}

int main()
{
    countnumbers({1,2,3});
}

Hello. I'm not really good at vectors so please help me. I want to count the total number of elements in a vector. Why is the output 333, instead of 3?

Aucun commentaire:

Enregistrer un commentaire