Using TCVectorImp and TCVectorIteratorImp

This is a discussion on Using TCVectorImp and TCVectorIteratorImp within the C++ Programming forums, part of the General Programming Boards category; I'm loading a text file using fstream. I do not want to use an array to hold the data so ...

  1. #1
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,597

    Using TCVectorImp and TCVectorIteratorImp

    I'm loading a text file using fstream. I do not want to use an array to hold the data so I chose to use a counted vector like this.

    Code:
    #include <cstring.h>
    #include <vectimp.h>
    
    TCVectorImp<string> Buffer;
    
    fstream fl(filename,ios::in);
    
    //Max line length of one line of text is 32767 characters
    char *text=new char[32767];
    
    while (!fl.eof())
    {
      fl.getline(text,32767);
      string currentline(text);
      Buffer.Add(currentline);
    }
    
    delete [] text;
    
    TVectorIteratorImp<string> it(Buffer);
    Question
    Now I need to use the iterator it to iterate through the vector Buffer. How do I go about doing this? Also does not matter if each string is diff size because that is handled by the string class, not the Vector class. The Vector class just holds string objects.
    I've tried some implementations and got them to compile, but did not do what I want it to.

    Purpose
    This function is responding to a WM_PAINT message for my window - it iterates through the current vector and writes the line of text (line by line) to the Client DC.

    Benefits
    This is much faster in loading and iteration than using a plain old char array.

    Correct class used?
    Should I use a vector for this or a list?

    Vector in memory
    Now is the time for all (first vector string)
    good men to come to (second vector string)
    the aid of their country. (third vector string)

    File on disk
    Now is the time for all <cr> good men to come to <cr>
    the aid of their country. <cr>

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,597
    Never mind, found some examples that came with my compiler. Trying to interpret the mess now.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,597
    Here's the code.

    Code:
    #include <cstring.h>
    #include <classlib\vectimp.h>
    
    
    class TString
    {
      public:
        TString() {};
        TString(const string &s):myString(s) {};
        TString(const TString &sc):myString(sc.myString) {};
        
        TString& operator=(const TString &sc)   
          {myString=sc.myString;return *this;};
        
        int operator==(const TString &sc) const 
          {return myString==sc.myString;};
        
        int operator<(const TString &sc) const 
          {return myString<sc.myString;};
       
       private:
         string myString;
    };
    
    typedef TCVectorImp<TString> TStringContainer;
    typedef TCVectorIteratorImp<TString> TStringIterator;
    
    class TEditWindow:public TMDIChild
    {
      TClientDC *CDC;
      TFont Font;
      TEXTMETRIC FontMetrics;
      int numstrings;
      TStringContainer Buffer;
    
      public:
       TEditWindow(TMDIClient& parent, const char far* title = 0,
       TWindow* clientWnd = 0, bool shrinkToClient = false,
       TModule* module = 0);
       virtual void SetupWindow(void);
       bool EvEraseBkgnd( HDC ) {return TRUE;};
       virtual void Paint(TDC &dc,bool erase,TRect &rect);
    };
    
    TEditWindow::TEditWindow(TMDIClient& parent, 
                                                 const char far* title, 
                                                 TWindow* clientWnd, 
                                                 bool shrinkToClient, 
                                                 TModule* module):
                                                 TMDIChild(parent,title,
                                                 clientWnd, shrinkToClient,module)
    {
      Attr.Style|=WS_VSCROLL|WS_HSCROLL;
      fstream fl(title,ios::in);
      char text[256];
      do
      {
        fl.getline(text,256);
        Buffer.Add(TString(text));
      } while (!fl.eof());
    }
    
    void TEditWindow::SetupWindow(void)
    {
      CDC=new TClientDC(HWindow);
      CDC->GetTextMetrics(FontMetrics);
      CreateCaret(false,1,FontMetrics.tmHeight);
      SetCaretPos(0,0);
      ShowCaret();
    }
    
    void TEditWindow::Paint(TDC &dc,bool erase,TRect &rect)
    {
      TStringIterator it(Buffer);
    
      while (it)
      {
       //Iterate through vector of strings and output to window here 
      }  
    
    }


    For some reason this code does not add TString objects to the Vector of TString objects. If I retrieve the count, it is always 0. I've typedef'ed TCVectorImp and TCVectorIteratorImp to TStringContainer and TStringIterator respectively.

    Please ignore all of the Windows relevant code (and 3rd party library code which encapsulates Windows API) and just help me with the STL classes.

    Why doesn't this add TString objects to the vector??

  4. #4
    Registered User Liam Battle's Avatar
    Join Date
    Jan 2002
    Posts
    114
    hmm i like personally to use sizeof,
    and embed structs to do the container...

    you dont have to use arrays, you can have a struct with a set of strings, and then do a size of the struct that u used and it will distrobute the right data into each array in one line...
    only in BINARY MODE.. TEXT mode is not full proof
    LB0: * Life once school is done
    LB1: N <- WakeUp;
    LB2: N <- C++_Code;
    LB3: N >= Tired : N <- Sleep;
    LB4: JMP*-3;

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,597
    I know how to do this in C/C++ w/o using the STL and a million other ways. However, I'm trying to use the pre-coded C++ container libraries to do this for practice sake.

  6. #6
    Registered User Liam Battle's Avatar
    Join Date
    Jan 2002
    Posts
    114
    oh im c ...
    well dont !

    haha
    i dont use them so sorry cant help.. i like my own code..
    i like to customize
    LB0: * Life once school is done
    LB1: N <- WakeUp;
    LB2: N <- C++_Code;
    LB3: N >= Tired : N <- Sleep;
    LB4: JMP*-3;

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21