STL question
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: STL question

  1. #1
    Join Date
    Apr 2002
    Posts
    10

    STL question

    Hi All,

    I have the following code which inserts elements into a
    vector<list<vector<int> > > .

    void bucketInsert(int n, vector<list<vector<int> > > &vliBuckets)
    {
    list<vector<int> > templst;
    list<vector<int> >::iterator it;
    vector<int> newvec(3);
    int t = n%10;

    newvec.push_back(n);
    newvec.push_back(int(n/10));
    newvec.push_back(0);
    vliBuckets[t].push_back(newvec);

    }

    I am running into problems when I am trying to display the elements which I store. Could anybody help with a code snippet doing this.
    Also, in case like the above one how do you check memory storage. In other words how can one see the elements which are getting stored.
    Any help will be appreciated.
    Thanks.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,427

    Re: STL question

    Originally posted by shivank
    Hi All,

    I have the following code which inserts elements into a
    vector<list<vector<int> > > .
    Use typedef's. You'll see why later on.
    Code:
    #include <vector>
    #include <list>
    typedef std::vector<int> VectInt;
    typedef std::list<VectInt> ListVectInt;
    typedef std::vector< ListVectInt > VectList;
    
    void PrintVectorInt( VectInt &vVect )
    {
    
        VectInt::iterator it = vVect.begin();
        VectInt::iterator it2 = vVect.end();
        while (it != it2 )
        {
            std::cout << *it << " ";
            ++it;
        }
        std::cout << std::endl;        
    }
    
    void PrintBucketInfo( ListVectInt& vList )
    {
        std::for_each(vList.begin(), vList.end(), PrintVectorInt );
    }
    
    void bucketInsert(int n, VectList& vliBuckets)
    {
        ListVectInt templst;
        ListVectInt::iterator it;
        VectInt newvec(3);  // <--Did you *really* want to do this?
    
        int t = n%10;
    Code:
        newvec.push_back(n);
    Did you really want to push_back() here? This will add another item to newvec, giving you 4 items altogether. You reserved room for 3 items in the constructor for newvec.
    Code:
       newvec.push_back(int(n/10));  // Same here.
    No need for int cast. n / 10 is already an int. BTW, you now have 5 items in newvec.
    Code:
       newvec.push_back(0);
    You now have 6 items in newvec.
    Code:
      
    vliBuckets[t].push_back(newvec);
    
    // Print information
    for_each(vliBuckets.begin(), vliBuckets.end(), PrintBucketInfo );
    }
    The reason for the typedefs is that it increases readability and lessens typos.
    Also, in case like the above one how do you check memory storage. In other words how can one see the elements which are getting stored.
    This depends on your debugger. Or you have to write code to dump out the elements.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Apr 2002
    Posts
    10
    Thanks a lot for your reply. It really helped a lot.
    I have my code giving the correct output. I have a function which calls these two functions(given below). Both of these functions at the time of returning are causing some memory problem (Unhandled exception error). If I comment out the statements which call these functions from the other function then it runs fine. So the problem is being caused by these functions. However, they are giving me the correct output.

    Here are the functions.

    void bucketShow(bool bFinalShow,int k,vector<list<vector<int> > >& vliBuckets, ofstream ofs)
    {
    list<vector<int> > lst;
    vector<int> vect;
    list<vector<int> >::iterator lstIt;
    vector<int>::iterator vit;
    vector<list<vector<int> > >::iterator IT;
    IT = vliBuckets.begin();
    while (IT!= vliBuckets.end())
    {
    lstIt = (*IT).begin();
    while(lstIt != (*IT).end())
    {
    vect = *lstIt;
    vit = vect.begin();
    cout<<*vit<<" ";
    ++lstIt;
    }
    IT++;
    }//while

    }

    void bucketSort(vector<list<vector<int> > > &vliBuckets, int nPass, ofstream ofs)
    {
    int index, size;
    int counter = 1;
    list<vector<int> >::iterator lstIt;
    vector<int> v;
    while(counter < nPass)
    {
    for(int i=0;i<10;i++)
    {
    list<vector<int> >& inilst = vliBuckets[i];
    size = inilst.size();
    if(size>0)
    {
    lstIt = inilst.begin();
    while(lstIt!= inilst.end())
    {
    v = *lstIt;
    if(v[2] < counter)
    {
    list<vector<int> >& finlst = vliBuckets[v[1]%N];
    v[1] = v[1]/N;
    v[2] = v[2] + 1;
    v[0] = v[0];
    finlst.push_back(v);
    lstIt = inilst.erase(lstIt);
    }
    else
    {
    lstIt++;
    }
    }
    }//if
    }//for

    counter++;

    }//while
    bucketShow(true, 1, vliBuckets, ofs);
    }

    Also, when I am trying to Debug, the debugger is taking me to the following lines in the /include/XSTRING file.

    else if (_Refcnt(_Ptr) == 0 || _Refcnt(_Ptr) == _FROZEN)
    allocator.deallocate(_Ptr - 1, _Res + 2);


    Any help would be appreciated.
    Thanks again

  4. #4
    Join Date
    Apr 1999
    Posts
    27,427
    You should post a small, compilable, and runnable version of a program that demonstrates the error. This way, all that is needed is for someone here to take your code, compile, run, and see what the error could be.

    One thing I did notice -- you are using indices:
    Code:
    if(v[2] < counter)
    {
    list<vector<int> >& finlst = vliBuckets[v[1]%N];
    v[1] = v[1]/N;   // <-- Index used here
    v[2] = v[2] + 1; // <-- Index used here too
    v[0] = v[0];
    finlst.push_back(v);
    Are you sure there is really a v[0], v[1], and a v[2]? When you use operator [], STL does not check array boundaries. You should call V.size() to make sure that these items exist. For example
    Code:
    int ThisSize = V.size();
    if ( ThisSize >= 3 ) { // Indices are OK }
    else { // There is no such thing as v[2]!! }
    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Apr 2002
    Posts
    10
    I got it. I was missing & in the ofstream in the function header.

    Should have been

    void bucketSort(vector<list<vector<int> > > &vliBuckets, int nPass, ofstream& ofs)

    instead of

    void bucketSort(vector<list<vector<int> > > &vliBuckets, int nPass, ofstream ofs)


    Thanks a lot for your time and help.
    regards,
    Shivank

  6. #6
    Join Date
    Apr 2002
    Posts
    10
    I got it. I was missing & in the ofstream in the function header.

    Should have been

    void bucketSort(vector<list<vector<int> > > &vliBuckets, int nPass, ofstream& ofs)

    instead of

    void bucketSort(vector<list<vector<int> > > &vliBuckets, int nPass, ofstream ofs)


    Thanks a lot for your time and help.
    regards,
    Shivank

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center