Solved

question about manipulating vectors of my objects

Posted on 2004-10-01
4
171 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
  • 2
  • 2
4 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks jaime_olivares , that did it
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

763 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

8 Experts available now in Live!

Get 1:1 Help Now