Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

question about manipulating vectors of my objects

Posted on 2004-10-01
4
Medium Priority
?
181 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 1600 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

609 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