简体   繁体   中英

How do I only display the first 10 elements of a Vector using an Iterator?

I'm trying to use displayVectorVer2() to have it only display the first 10 elements, but I don't know how to do it with iterators. I did try a few dumb things just to see what would happen: I compared the iterator to displayLimit in my for loop. I played around by subtracting vobj.end()-5 since my professor is only having me use 15 elements, but I fully well knew this was not a good idea.

#include <iostream>
#include <vector>
#include <ctime>

template <class T>
void fillVector(std::vector<T>& vobj, int n);

template <class T>
void displayVectorVer2(std::vector<T>& vobj, typename std::vector<T>::iterator ptr);

template <class T>
void fillVector(std::vector<T>& vobj, int n)
{
    srand((unsigned int)time(NULL));
    for (int i=0; i<n; ++i)
    {
        vobj.push_back(rand()%99999+1);
    }
}

template <class T>
void displayVectorVer2(std::vector<T>& vobj, typename std::vector<T>::iterator ptr)
{
    std::cout << "Vector object contains " << vobj.size() << " values which are" << std::endl;

    const unsigned displayLimit = 10;
    if (vobj.size()>displayLimit)
    {
        for (ptr=vobj.begin(); ptr<vobj.end(); ++ptr)
        {
            std::cout << "  " << *ptr;
        }
        std::cout << "  ..." << std::endl;
    }
    else
    {
        for (ptr=vobj.begin(); ptr<vobj.end(); ++ptr)
        {
            std::cout << "  " << *ptr;
        }
        std::cout << std::endl;
    }
}

int main()
{
    std::vector<int> vobj;
    std::cout << "Before calling fillVector(...): vobj contains " 
              << vobj.size() << " values." << std::endl;

    std::cout << "\nEnter # of random values you'd like to store in vobj: ";
    int n;
    std::cin >> n;

    std::cout << "\n*** Calling fillVector(...) ***" << std::endl;
    fillVector(vobj, n);

    std::cout << "\n*** Calling displayVectorVer2(...) ***" << std::endl;
    std::vector<int>::iterator ptr;
    displayVectorVer2(vobj,ptr);
}

Maybe I am thinking too simple but, that wold solve your question:

I'm trying to use displayVectorVer2() to have it only display the first 10 elements

without knowing your full exercise, that would be my answer:

...
    const unsigned displayLimit = 10;
    if (vobj.size()>displayLimit)
    {
        for (ptr=vobj.begin(); ptr<vobj.begin()+displayLimit; ++ptr) 
        {
            std::cout << "  " << *ptr;
        }
        std::cout << "  ..." << std::endl;
    }
    else
...

edit:

That worked, but why does it work? I remember adding to vobj.begin() and getting extra empty elements appended to the original vector.

Not sure what exactly you did but maybe that helps you understanding your code:

...
   const unsigned displayLimit = 10;
    if (vobj.size()>displayLimit)
    {
        //Init ptr outside the for loop
        ptr = vobj.begin();

        //What the for loop is seeing with a more familiar syntax: 
        //for( ; i < 0 +displayLimit; ++i)
        //what you are seeing
        for (/*ptr init*/; ptr < vobj.begin() +displayLimit; ++ptr)
        {
            std::cout << "  " << *ptr;
        }
        std::cout << "  ..." << std::endl;
    }
...

The iterators just gives you the int value and you can use it with what ever "eats" int values. In your case the for loop.

If you tell the program to use an iterator you tell the program: "Just give me the number the Vector begins with and add 10".

In your case 0 "...and add 10"

You could also write a code like that with n passed to the function for being able to use.end - input + 10 for showing 10 lines:

...
template <class T>
void displayVectorVer2(std::vector<T>& vobj, typename std::vector<T>::iterator ptr,int n)
{
    std::cout << "Vector object contains " << vobj.size() << " values which are" << std::endl;

    const unsigned displayLimit = 10;
    if (vobj.size()>displayLimit)
    {
       ptr=vobj.begin();
        for (; ptr<vobj.end() -n +displayLimit; ++ptr)
        {
            std::cout << "  " << *ptr;
        }
        std::cout << "  ..." << std::endl;
    }
    else
    {
        for (ptr=vobj.begin(); ptr<vobj.end(); ++ptr)
        {
            std::cout << "  " << *ptr;
        }
        std::cout << std::endl;
    }
}

int main()
{
    std::vector<int> vobj;
    std::cout << "Before calling fillVector(...): vobj contains "
              << vobj.size() << " values." << std::endl;

    std::cout << "\nEnter # of random values you'd like to store in vobj: ";
    int n;
    std::cin >> n;

    std::cout << "\n*** Calling fillVector(...) ***" << std::endl;
    fillVector(vobj, n);

    std::cout << "\n*** Calling displayVectorVer2(...) ***" << std::endl;
    std::vector<int>::iterator ptr;
    displayVectorVer2(vobj,ptr,n);
}
...

You also shouldn't use srand in modern code anymore since it is depricated for more than 10 years since c++11 introduced <random> and srand can harm your program fe if used for generating seeds for sensitive code. Also srand provides not the "randomness" it should provide, srand generates some numbers more often than others - that's not random.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM