Polynomials sorting using maps

This code works perfectly and sorted weather if I use linked lists or maps, but based on maps features I believe the sorting of this code should be done in another way (as they count a sorted container).
I tried the code which is in comment shape, it results in no error but doesn't run because of a breakpoint in if (it != PolynomialMap.end())
What would you recommend? is this code efficient in your view? or I missed using the benefits of maps?

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  #include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <vector>
#include <map>
using namespace std;

typedef struct Node
{
	double  cof;      // coefficient
	int     deg;      // degree

	Node()
	{
		deg = 0;
		cof = 0.0;
	}

	Node(const pair<const int, double> & nodePair)
	{
		deg = nodePair.first;
		cof = nodePair.second;
	}
} Node;

class CPolynomial
{
private:
	std::map<int, double> PolynomialMap;

public:
	CPolynomial();
	CPolynomial(const string& file);
	virtual ~CPolynomial();

private:
	void AddOneTerm(Node term);   // add one term into m_Polynomial 
};

int main()
{
	CPolynomial p1("P3.txt");
	CPolynomial p2("P4.txt");
	CPolynomial p3;

	system("pause");
	return 0;
}

CPolynomial::CPolynomial()
{
	;
}

CPolynomial::CPolynomial(const string& file)
{
	Node term;
	fstream MyFile;
	string p;
	int num;

	MyFile.open(file);

	if (!MyFile.is_open())
	{
		cerr << "Unable to open input file" << endl;
		exit(EXIT_FAILURE);
	}
	else
	{
		MyFile >> p >> num;

		map <int, double>::iterator it = PolynomialMap.begin();

		for (int i = 0; i < num; i++)
		{
			MyFile >> term.deg >> term.cof;
			AddOneTerm(term);
		}
		MyFile.close();
	}
}



void CPolynomial::AddOneTerm(Node term)
{
	auto it = PolynomialMap.begin();
	while (it != PolynomialMap.end() && it->first < term.deg)
	{
		++it;
	}

	if (it != PolynomialMap.end() && term.deg == it->first)
	{
		it->second += term.cof;
	}
	else
	{
		PolynomialMap.emplace_hint(it, term.deg, term.cof);
	}
}
	/*
	{
	std::map<int, double>::iterator it;
	PolynomialMap.find(term.deg);
	if (it != PolynomialMap.end())
	{
	it->second += term.cof;
	}
	else
	{
	PolynomialMap.insert(std::pair<int, double>(term.deg, term.cof));
	}
	}
	*/


CPolynomial::~CPolynomial()
{
	PolynomialMap.clear();
}
The commented code is more efficient. std::map is usually implemented as a red-black tree. It is more efficient to let map navigate the tree via find than it is to walk each element yourself.

The reason you're trapping is that you don't set the iterator at line 107.
 
  it = PolynomialMap.find(term.deg);


Last edited on

Thank you @AbstractionAnon
Solved
Topic archived. No new replies allowed.