-1

I'm trying to sort and populate a vector of objects with integers from a file. int fileSize reads the first line to determine how many numbers I should read after that. I'm kind of having trouble understanding pointers so could somebody at least help me get this working?

I got it to work when my vector type is <int> but i can't seem to populate a vector with a class IPRecord as the object.

#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
#include "IPRecord.hpp"

using namespace std;

int main()
{
vector<IPRecord*> v;

ifstream inputFile("input.txt");

int fileSize;

inputFile >> fileSize;

//populate vector and sort
for (int i = 0; i < fileSize; i++)
{
    int val;
    inputFile >> val;
    v.insert(lower_bound(v.begin(), v.end(), val), val);
}

//print
for (int i = 0; i < v.size(); i++)
{
    cout << v[i] << endl;
}




inputFile.close();

return 0;
}
Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
  • 1
    You're not creating any objects of type `IPRecord`. The problem has nothing to do with pointers at this point. Also you have not explained what the problem really is. Errors in compilation? Running? Working wrong? How? – Sami Kuhmonen Sep 25 '16 at 04:00
  • Sorry about that, yes, the error is in the compilation. I'm also very new to this concept of using a class Object as a vector. I'm used to using int and double. so I don't even know how I should create IPRecord objects. – Chappy Khmao Sep 25 '16 at 04:08
  • Then you need any C++ tutorial to get started. They all will go through objects. – Sami Kuhmonen Sep 25 '16 at 04:10

1 Answers1

-2

This is just a code snippet and not tested. Code is commented and hence self explanatory.

 int main()
 {
    vector<int*> v;  // pointer of object means you have to destroy yourself. Better replace with smart pointers later

    ifstream inputFile("input.txt");

    char buf[8];
    int filesize;

    int* storage;

    if (inputFile)
    {
        inputFile.getline(buf, 8, '\n'); // assuming numbers on separate lines
        filesize = atoi(buf);

        //allocating for filesize objects. This need to exist since you are storing pointer
        storage = (int*)malloc(filesize * sizeof(int));
        //populate vector and sort
        int count = 0;

        while (!inputFile.eof())
        {
            int val;
            inputFile.getline(buf, 8, '\n'); // assuming numbers on separate lines
            storage[count] = atoi(buf);
            v.push_back(storage+count);
            count++;
        }
        inputFile.close();


        //print
        for (auto i = v.begin(); i != v.end(); i++)
        {
            cout << (*i) << endl;
        }

        free(storage);


    }

    return 0;
}
seccpur
  • 4,996
  • 2
  • 13
  • 21
  • 2
    This: `storage = (int*)malloc(filesize * sizeof(int));` should be replaced with `storage = new int[filesize];`, why are you recommending an unprotected pointer in place of a vector, and this `while (!inputFile.eof())` will always read past the end of the file, giving a line of undefined behaviour mucking up the program and miss all non-EOF error casses, possibly leading to infinite loop. More on that here: http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong. Yes this is untested code, but it's also very bad advice. – user4581301 Sep 25 '16 at 06:18