0

I have a large data in text file in form:

1 0.933 2 0.865 3 0.919 4 0.726 
2 0.854 3 0.906 4 0.726 
2 0.882 5 0.853 4 0.897
.
. etc

every integer number follows with its float value. I want to read this file line by line, and store each line in list. I know how to do that using array of linked lists, but, I couldn't fix the memory leak. I read that it is better to use STL instead. Example:

    list 1=  1 0.933 2 0.865 3 0.919 4 0.726 
    list 2=  2 0.854 3 0.906 4 0.726 
    list 3=  2 0.882 5 0.853 4 0.897
.
.

I'm not sure if the (vectors of lists) is similar to the (array of linked lists). I have tried different ways but all my attempts failed.

#include "stdafx.h"
#include <vector>
#include <list>
#include <iostream>
#include <map>
#include <sstream>
#include <iterator>
#include <fstream>

struct MyData{
    int Item;
    float Prob;
};
std::istream& operator>>(std::istream &is, MyData&d)
{
    return is >> d.Item >> d.Prob;
}


int main()
{

    std::ifstream in("DataF.txt");
    std::string line;
    int i = 0;
    while (std::getline(in, line)) {
            typedef std::istream_iterator<MyData> MyDataIstrIt;
            std::stringstream ss(line);             
        std::vector< std::list< MyData> >  data3{ MyDataIstrIt(ss), MyDataIstrIt() }; //  here I couldn't fix the  extractor definition for the vectors of lists

// another attempt
/*std::vector<MyData> data{ MyDataIstrIt(ss), MyDataIstrIt() };
  std::vector< std::list< MyData> > data2;
  data2.push_back(std::vector<MyData>()); // I read this is important for memory leak
  data2.push_back(data);*/
 // data2[i].push_back(data);
 // ++i;



    }
    system("PAUSE");
    return 0;
}
1
  • I would use std::vector<std::unordered_map<int, float>> for this if the order of each line's items isn't mandatory. If it is required to be sorted I would use std::vector<std::map<int, float>>. If the original exact order is required, then use std::vector<std::vector<MyData>>> or a similar construct. The first two maintains the int as an index, which has its benefits. Commented Feb 22, 2014 at 3:40

1 Answer 1

3

There are better ways to do it of course but for the sake of showing you how to use a list or read into one and then place that into a vector..

#include <vector>
#include <list>
#include <iostream>
#include <map>
#include <sstream>
#include <iterator>
#include <fstream>

struct MyData
{
    int Item;
    float Prob;
};

std::istream& operator>>(std::istream &is, MyData&d)
{
    return is >> d.Item >> d.Prob;
}

int main()
{
    std::string line;
    std::vector<std::list<MyData>> data;

    std::ifstream in("DataF.txt"); //don't forget to check `if (in.is_open())`

    while(std::getline(in, line))
    {
        std::stringstream ss(line);
        std::list<MyData> inner;
        MyData info;

        while(ss >> info)
        {
            inner.push_back(info);
        }

        data.push_back(inner);
    }

    for (auto &list : data)
    {
        for (auto &mdata : list)
        {
            std::cout<<mdata.Item<<"   "<<mdata.Prob<<"\n";
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, when you said "There are better ways to do it" do you mean more efficient? if it's yes can you tell what the best way since I have very large file, and before processing i want to save the data file in the format (store each line inside container, ex. the first line in list, the second line in second list and so on)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.