2 dimensional arrays

hi, so i have been tasked with creating a dictionary application using c++ and im a beginner to coding. the task comprises of creating an application where 106,187 words are in a text file with their meaning and types and we need to search load all the words and be able to search them to find out their meanings and types(lie noun, adverb etc....) i have created a code, it seams to get all the lines correctly on to the 3 different variables but i cannot store them on any stl containers as i dont know how to. when i looked up online i found out about 2d arrays even thou i searched for it i could find any thing where they state that you can load data on to an array from a text file. please help..

my text file format is as below:
word
definition
type

word
definition
type

and so on



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
  #include <iostream>
#include <fstream>
#include <map>
#include <string>

using namespace std;


class files {
private:
	string words;
	string definitions;
	string types;
public:
	
	void read();
	
};

void files::read()
{


		const int i = 18;
		//106187;
		const int j = 3;



		string arr[i];
		ifstream myfile("dictionary.txt");
		do
		{
			getline(myfile, words, '\n');
			getline(myfile, definitions, '\n');
			getline(myfile, types, '\n');



			cout << words << '\n' << definitions << '\n' << types << endl;

		} while (!myfile.eof());



	

}
int main()
{
	
	files d;
	
	d.read();
}
If you can use struct's or classes and learn how to use <map>'s, none of which is too hard in your case, then that is the way to go rather than a 2D (3D would be better) synchronised/parallel arrays.

The reason <map>'s are better is searching and retrieving the data is much easier and quicker.

struct's and classes group/envelope/encapsulate each word/definition/type entry to make a single object.

Your choice.
One option is use a map:
1
2
3
4
5
6
7
8
struct WordInfo
{
  string definition;
  string type;
};

map<string, WordInfo> words;
words["name"] = WordInfo{"bla bla", "noun"};
First, I recommend you to create a std::map for your dictionary.

1
2
typedef std::pair<std::string, std::string> Content;
std::map<std::string, Content> dict;


Then use fstream to read and parse your file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
std::string line;
std::ifstream file("path/location");
file.open();
if (file.bad()) return;
while (std::getline(file, line)) {
    std::string word = line;
    std::getline(file, line);
    std::string desc = line;
    std::getline(file, line);
    std::string type = line;
    dict[word] = Content(desc, type);
    std::getline(file, line); // to ignore the blank line
}
file.close();
Last edited on
Your class should be an entry for a single word:
1
2
3
4
5
6
class DictEntry {
public:
    string word;
    string definition;
    string type;
};

Your prof may want you to make the data members private and add accessor methods. To me, they just get in the way for a simple program like this.

You can put these into a std::set, but std::set needs to know how to compare them. To do that, you add a less-than operator to the class. One entry is less than the other if the first's word is less than the second's:
1
2
3
4
5
6
class DictEntry {
...
    bool operator < (const DictEntry &rhs) const {
        return (word < rhs.word);
    };
};

With this operator defined, you can compare DictEntry objects with <. "Rhs" stands for "right hand side" and denotes the object on the right hand side of the operator. An example will help clarify this:
1
2
3
4
DictEntry a,b;
// Code to populate a and b
if (a < b) {  // calls a::operator<(b)
    std::cout << "a is less than b\n";

}

Now you can create a dictionary as a std::set of DictEntry
Topic archived. No new replies allowed.