• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • About
  • Life
  • Tech
  • Travel
  • Work
  • Questions
  • Contact

Welcome

.

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

April 11, 2020 by

Questions › How do I only display the first 10 elements of a Vector using an Iterator?
0
Vote Up
Vote Down
Garmaine asked 4 years ago

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);
}
Are you looking for the answer?
Original Question and Possible Answers can be found on `http://stackoverflow.com`

Question Tags: c++, iterator, vector

Please login or Register to submit your answer




Primary Sidebar

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Newsletter

Complete the form below, and we'll send you all the latest news.

Footer

Footer Funnies

Who knew that reading the footer could be such a hilarious adventure? As we navigate websites, books, and documents, we often stumble upon the unassuming space at the bottom, only to discover a treasure trove of amusement. In this side-splitting compilation, we present 100 jokes that celebrate the unsung hero of content – the footer. Get ready to chuckle, giggle, and maybe even snort as we dive into the world of footnotes, disclaimers, and hidden comedic gems. Brace yourself for a wild ride through the footer!

Recent

  • Unveiling the Enigma: Almost-Magical Lamp Lights Highway Turns
  • The Impact of Young Onset Dementia on Employment and Finances: Optimizing Post-Diagnostic Approaches
  • 11 Wonders of 2023 Technological Breakthrough – Unveiling the Future
  • Work from Home and Stay Mentally Sane – Achieve Productivity and Well-being
  • Hidden Gems of New York City – Uncover the Must-See Treasures!

Search

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Copyright © 2023