Solved

question about manipulating vectors of my objects

Posted on 2004-10-01
4
177 Views
Last Modified: 2010-04-01
Here is my code. I need help adding 3 columns from vector a to vector b.

class Column
{
public:
     Column(const string& ColumnName):m_ColumnName(ColumnName){}
     Column(const string& ColumnName, const vector<string>& ColumnData):m_ColumnName(ColumnName), m_ColumnData(ColumnData){}
     string m_ColumnName;
     vector<string> m_ColumnData;
};
typedef vector<Column> BigFileColumnVector;
void loadBigFile(const char* fileName, BigFileColumnVector& FileData);
void saveBigFile(const char* fileName, BigFileColumnVector& FileData);
void AppendToAppPort(BigFileColumnVector& AppPortVector, BigFileColumnVector& P_LVector);

int main(int argc, char* argv[])
{

      const char* fileName_AppPort = "c:\\tmp\\f05.csv";
      const char* fileName_P_L = "c:\\tmp\\p_l.csv";
      const char* fileName_AppPort_output = "c:\\tmp\\f05_output.csv";
      BigFileColumnVector AppPortVector, P_LVector;
      loadBigFile(fileName_AppPort, AppPortVector); // load vector1 from a file1
      loadBigFile(fileName_P_L, P_LVector); // load vector2 from a file2
      AppendToAppPort(AppPortVector, P_LVector); // now I need to append 3 columns from P_LVector to AppPortVector
      saveBigFile(fileName_AppPort_output, AppPortVector); //here I save the vector to a file
      AppPortVector.

  return 0;

}


void loadBigFile(const char* fileName, BigFileColumnVector& FileData)
{
     fstream read_file(fileName, ios::in);
     string Line;
     string Field;
     getline(read_file, Line);
     stringstream ss(Line);

       while(getline(ss, Field, ','))
     {
          FileData.push_back(Column(Field));
     }
     
       while(getline(read_file, Line))
     {
          stringstream ss(Line);
          int i = 0;
          while(getline(ss, Field, ','))
          {
              FileData[i++].m_ColumnData.push_back(Field);
          }
     }
     read_file.close();

}

void saveBigFile(const char* fileName, BigFileColumnVector& FileData)
{
     //Now write it to a file
     fstream write_file(fileName, ios::out|ios::trunc );
     //First write all the column titles
     for (vector<Column>::iterator i = FileData.begin();i != FileData.end();++i)
     {
          write_file << i->m_ColumnName << ",";
     }
     write_file.seekp(-1, ios::cur);// Reverse current position to delete ',' comma
     write_file << endl; //End the line

     for(int row = 0;row < FileData[0].m_ColumnData.size();++row)
     {
          for (vector<Column>::iterator ColumnItem = FileData.begin();ColumnItem != FileData.end();++ColumnItem)
          {
               write_file << ColumnItem->m_ColumnData[row] << ",";
          }
          write_file.seekp(-1, ios::cur);// Reverse current position to delete ',' comma
          write_file << endl;
     }
     write_file.close();

}

// I need help implementing this method
void AppendToAppPort(BigFileColumnVector& AppPortVector, BigFileColumnVector& P_LVector)
{



}
0
Comment
Question by:healingtao
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12203729
If I have understood correctly, you will need this:

void AppendToAppPort(BigFileColumnVector& AppPortVector, BigFileColumnVector& P_LVector)
{
    vector<Column>::iterator it = P_LVector.begin();    
   
    AppPortVector.push_back(*(it++));    
    AppPortVector.push_back(*(it++));    
    AppPortVector.push_back(*(it++));    
}

0
 

Author Comment

by:healingtao
ID: 12204460
jaime_olivares,

This code seems to work but when I call a method to save it to a file, it crashes
void saveBigFile(const char* fileName, BigFileColumnVector& FileData)
{
     //Now write it to a file
     fstream write_file(fileName, ios::out|ios::trunc );
     //First write all the column titles
     for (vector<Column>::iterator i = FileData.begin();i != FileData.end();++i)
     {
          write_file << i->m_ColumnName << ",";
     }
     write_file.seekp(-1, ios::cur);// Reverse current position to delete ',' comma
     write_file << endl; //End the line

     for(int row = 0;row < FileData[0].m_ColumnData.size();++row)
     {
          for (vector<Column>::iterator ColumnItem = FileData.begin();ColumnItem != FileData.end();++ColumnItem)
          {
              // IT CRASHES here for the last run when it gets to the new columns added
               write_file << ColumnItem->m_ColumnData[row] << ",";
          }
          write_file.seekp(-1, ios::cur);// Reverse current position to delete ',' comma
          write_file << endl;
     }
     write_file.close();

}

I'm not sure why
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 400 total points
ID: 12204990
Dont have so much time to make a full-test right now. Please try with:

void AppendToAppPort(BigFileColumnVector& AppPortVector, BigFileColumnVector& P_LVector)
{
    vector<Column>::iterator it = P_LVector.begin();    

    AppPortVector.push_back(Column(it->m_ColumnName, it->m_ColumnData)); it++;  
    AppPortVector.push_back(Column(it->m_ColumnName, it->m_ColumnData)); it++;  
    AppPortVector.push_back(Column(it->m_ColumnName, it->m_ColumnData));  
}

0
 

Author Comment

by:healingtao
ID: 12209695
thanks jaime_olivares , that did it
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

719 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