Vectors, objects and sorting

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  #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;
}
Last edited on
So you want to fill and sort a vector with pointers to IPRecord objects? May I ask why pointers, and not the objects themselves?

In the for-loop, you need to create a pointer to an IPRecord object using the new operator and a constructor (to send in any data from the file that you need to), then insert that pointer into the vector. You also need to create some kind of comparison function to sort your vector of pointer to objects.

It would also help if you could post the IPRecord class.
Last edited on
vector<IPRecord*> v is supposed to store pointer to IPRecord. But, it seems to be that you are trying to store integer variable in it in Line 24. Types do not match.

Answer these questions for me please.
1. What are you trying to accomplish by storing the pointers in the vectors instead of actual objects?

2. How is IPRecord constructed? You'd have to construct an IPRecord object first and then either insert or push its address into the vector.
Last edited on
here's the class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class IPRecord
{
public:
	IPRecord(unsigned int ip) :
		m_ip(ip),
		m_frequency(1)
	{
	}

	unsigned int getIP() { return m_ip; }
	unsigned int getFrequency() { return m_frequency; }

	void setIP(unsigned int ip) { m_ip = ip; }
	void incrementFrequency() { m_frequency++; }

private:
	unsigned int m_ip;
	unsigned int m_frequency;
};


i'm not sure what we are trying to accomplish but he wants us to use a vector of pointers.
Great, all you need to do is allocate an objects using the new operator, sending val to the constructor. Push_back the pointer returned into the vector, like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	for (int i = 0; i < fileSize; i++)
	{
		int val;
		inputFile >> val;
                 
                // allocate memory for object and catch return in pointer
		IPRecord* p = new IPRecord(val);
		v.push_back(p) // push_back pointer onto stack
	}

	//print
	for (int i = 0; i < v.size(); i++)
	{
		cout << "ip: " << v[i]->getIP() << endl;
		cout << "frequency: " << v[i]->getFrequency() << endl;
	}


This works but it is not sorted. If you want to use insert and lower bound, you need to specify how you want to sort your vector. You need to provide some kind of comparison function.
Last edited on
The code below fully sorts the vector by m_ip (and not frequency). I used a functor, but its also possible to do this via function pointer or the lambda function (c++ 11).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  #include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>

class IPRecord
{
public:
	IPRecord(unsigned int ip) :
		m_ip(ip),
		m_frequency(1)
	{
	}

	unsigned int getIP() { return m_ip; }
	unsigned int getFrequency() { return m_frequency; }

	void setIP(unsigned int ip) { m_ip = ip; }
	void incrementFrequency() { m_frequency++; }

private:
	unsigned int m_ip;
	unsigned int m_frequency;
};

struct CompareRecords
{
    
    bool operator()(IPRecord* p1, IPRecord* p2)
	{
	    return p1->getIP() < p2->getIP();
	}
};

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;

		IPRecord* p = new IPRecord(val);
		v.insert(lower_bound(v.begin(), v.end(), p, CompareRecords() ), p);
	}

	//print
	for (int i = 0; i < v.size(); i++)
	{
		cout << "ip: " << v[i]->getIP() << endl;
		cout << "frequency: " << v[i]->getFrequency() << endl;
	}

	// delete memory associated with each pointer
	for(int i = 0; i < v.size(); i++) delete v[i];


	inputFile.close();

	return 0;
}


Note that memory allocated with new needs to be deleted, as I've done above.
Last edited on
Topic archived. No new replies allowed.