Using <vector> stl

Posted on 2005-05-02
Medium Priority
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
  • 2
LVL 55

Accepted Solution

Jaime Olivares earned 2000 total points
ID: 13911684
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

ID: 13914081
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

ID: 13914121
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.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

569 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