Using <vector> stl

Posted on 2005-05-02
Last Modified: 2010-04-24

I have a question related to using vector stl.
I have a  function  X that adds to a vector. The function returns the size of the vector. I am using a variable Z in another function Y which calls function X and initializes the variable Z to X.  Then when I call X again it seems that the size of the vector adds upon the previous size. I am using vector.clear() at the beginning of function X so that the size of the vector becomes 0 again. Can somebody explain to me how is this possible?

Question by:gjgomez
    LVL 55

    Accepted Solution

    how do you get vector size ?
    size() will return expected size
    capacity() will return memory allocation size, not a good function for you.


    Author Comment

    This is part of the function that uses .size() function. Notice that it holds a series of integers I am interested in, and at the beginning I called .clear() to make size equal 0 and eliminate the elements from the vector when I previously called the function.

    int BinPacking::nextFit()
          Bin b;
          int j=0, size = randomNums.size();
          for(unsigned int i=0; size > 0; i++)
                if(randomNums[i] <= bin[j].capacity)
                      bin[j].capacity -= randomNums[i];
                      bin[j].capacity -= randomNums[i];
          return bin.size();

    This the other function when I try to initialize the variable to the value returned by function nextFit(). As I would expect when I call the function a second time or more, the function should return the size of the vector for the new series of numbers at that stage of the program but it seems to build the size of the array upon what was there before, and it is confusing since I called .clear() on the vector at the beginning of the function.

    int nfBins=0, ffBins=0, bfBins=0, ffdBins=0, bfdBins=0;
          for(int i=0; i < 10; i++)
                nfBins = nextFit();
                ffBins = firstFit();
                bfBins = bestFit();
                ffdBins = firstFitDecreasing();
                bfdBins = bestFitDecreasing();
          cout << "Next fit algorithm\n";
          cout << "Average number of bins used: " << static_cast<double>(nfBins)/10 << endl << endl;

          cout << "First fit algorithm\n";
          cout << "Average number of bins used: " << static_cast<double>(ffBins)/10 << endl << endl;

          cout << "Best fit algorithm\n";
          cout << "Average number of bins used: " << static_cast<double>(bfBins)/10 << endl << endl;

          cout << "First fit decreasing algorithm\n";
          cout << "Average number of bins used: " << static_cast<double>(ffdBins)/10 << endl << endl;

          cout << "Best fit decreasing algorithm\n";
          cout << "Average number of bins used: " << static_cast<double>(bfdBins)/10 << endl << endl;


    Author Comment

    Thanks for your comments.
    I already found the answer. The function .clear() I was calling was right, but the scope of my problem wasn't that function. It was in the function I used to generate the random numbers.
    Anyways. Thanks a lot.
    You are always there to answer my questions.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
    In Easy String Encryption Using CryptoAPI in C++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now