Making a Look-up-Table (with file quotes) by MAPPING

Hi,

I am trying to make a Look Up Table (LUT) of file quotes.

I have several .txt files with different names. Every txt file is full of fingerprints (n lines, each line is a number which goes from 0 to 1048576, every line is a fingerprint).

Example, "TheBlackWizard.txt" is:


218487
228012
5955  
263893 
429090
...


I want to make a LUT where the fingeprint number is used as the table's index,at that precise index there will be several quotes.

For Example, after inserting in the lut TheBlackWizard.txt, the LUT will be:

LUT[218487] = TheBlackWizard, 1
LUT[228012] = TheBlackWizard, 2
LUT[5955] = TheBlackWizard, 3
LUT[263893] = TheBlackWizard, 4
LUT[429090] = TheBlackWizard, 5

...

I managed to run few insertions in the LUT using Push and Pop function, but now I am stucked.
I want to see the whole current LUT[5955]on the display, using front() or back() let me see just the first or the last elements.
How can I see the full list ?
I searched a lot on the C++ Programming book and over the internet with no results.

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
#include <iostream>
#include <string>
#include <list>
#include <fstream>
using namespace std;



class coord{
string nomefilm;
long index;
long finga;

public:

//Setters & Getters
long set_finga(long f){
	this->finga = f;
}
long get_finga(){
	return this->finga;	
}
void set_nomefilm( string n){
	this->nomefilm = n;
}

string get_nomefilm(){
	return this->nomefilm;
}

void set_index(long i){
	this->index = i;
}

long get_index(){
	return this->index;
}

};



int main(int argc, char** argv)
{
	cout << "Start! " << endl;
	list<coord> LUT[1048576];
	cout << "I am not crashed!" << endl;

	cout << "Tell me the name "<<endl;
	string nome;
	cin >> nome;
	
	int m;
	long index;
	cout << "From what point u wanna start? "<< endl;
	cin >> m;
	for (index=m;index<99999999;index++){
		
		cout << "Insert  (0-1048576) for line# " << index<< endl;
		long finga;
		cin >> finga;
	
	
		coord myCoord;
		myCoord.set_finga(finga);
		myCoord.set_index(index);
		myCoord.set_nomefilm(nome);
		LUT[finga].push_back(myCoord);
	
		
		string text ;
		text = "hello"  ;
		cout << "String is  : " << text << endl ;

		cout << endl;
		cout << "LUT["<<finga<<"]: nomefilm \t"<<LUT[finga].front().get_nomefilm() <<"\t index \t"<<LUT[finga].front().get_index()<<LUT[finga].back().get_index()<<endl;
		cout << LUT[finga] << endl;

	
				
	
											}
	return 0;
}


I am using Ubuntu and Geany to compile it.

It says:
77: error: no match for ‘operator<<’ in ‘std::cout << LUT[finga]’
Last edited on
One way to iterate over the elements of a container:
for (auto& entry : LUT[finga])cout << entry;


You'll need to overload operator<< to work for your coord class.
Regarding your 'coord' class -- make it a struct with public members, and ditch the getters/setters. There's no point in having an encapsulated class that has nothing but getters and setters. You're just making yourself write more tedious code.

Also I think you are misunderstanding the idea of a list. A list is a collection that can contain any number of elements.

list<coord> LUT[1048576];

So why do you need 1048576 different lists?



What you might want is a map. A map lets you store any number of objects and index them using an arbitrary 'key':

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
struct Fingerprint
{
  int code;   // fingerprint code
  string name;
  int index;
};


...

map< int, Fingerprint > lut;

Fingerprint myFingerprint;
// .. load myFingerprint from the file
lut[ myFingerprint.code ] = myFingerprint;  // put the fingerprint in the map


// to iterate over the entire map:
for( auto i = lut.begin(); i != lut.end(); ++i)
{
  cout << "code = " << i->code << ", name = " << i->name << ", index = " << i->index << endl;
}

// to fetch a specific entry by its fingerprint code:
Fingerprint foo = lut[ 12345 ];  // gets fingerprint code 12345
  // note that this will create a new fingerprint if code 12345 does not exist 
Thanks for your replies.

I dived in discovering the map function. I am currently reading the map and multimap references (I will use multimap for sure later, cause I have multiple quotes on a single fingerprint key).

@Disch
I am starting from your code. I have a problem iterating the map, your for loops seems unfortunately inadequate.
I initialized the "i" counter with no big success and removed the "auto" from the brackets as suggested by warning.

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
#include <iostream>
#include <string>
#include <list>
#include <fstream>
#include <map>
using namespace std;


struct Fingerprint
{
  long code;   // fingerprint code
  string name;
  int index;
};


int main()
{
map <int, Fingerprint> lut;
Fingerprint myFingerprint;


// .. load myFingerprint from the file

//Input test data
cout << "Inserisci Fingerprint " << endl;
cin >> myFingerprint.code;
cout << "Inserisci nomefilm " << endl;
cin >> myFingerprint.name;
cout << "Inserisci punto " << endl;
cin >> myFingerprint.index;


lut[myFingerprint.code] = myFingerprint;  // put the fingerprint in the map


// to iterate over the entire map:
long i;
i=0;
for(i = lut.begin(); i != lut.end(); ++i) //<--- Here is the error
{
  cout << "code = " << i->code << ", name = " << i->name << ", index = " << i->index << endl;
}

// to fetch a specific entry by its fingerprint code:
Fingerprint foo = lut[ 12345 ];  // gets fingerprint code 12345
  // note that this will create a new fingerprint if code 12345 does not exist 


return 0;  
}


Geany says:
UsingMap.cpp:40: error: cannot convert ‘std::_Rb_tree_iterator<std::pair<const int, Fingerprint> >’ to ‘int’ in assignment


Of course I cannot compare an int index with a Fingerprint struct..

How can I output the whole map ?
Last edited on
1
2
3
long i;
i=0;
for(i = lut.begin();


This is wrong. lut.begin() returns an iterator, not a long.

Notice in my example I used for(auto i = lut.begin(), not long i = begin().

If your compiler doesn't support auto... update it. Otherwise the syntax is kind of ugly:

1
2
3
typedef map<int,Fingerprint>::iterator iter_t;

for(iter_t i = lut.begin(); i != lut.end(); ++i)


EDIT:

actually I noticed a different problem in my code. The correct way to iterate over the map is:

1
2
3
4
for(auto i = lut.begin(); i != lut.end(); ++i)
{
  cout << "code = " << i->second.code << ", name = " << i->->second.name << ", index = " << i->->second.index << endl;
}
Last edited on
Topic archived. No new replies allowed.