Reading the class definition file

We used the ReadClasses function in this example to load the map of objects. Here, the key was an image class index and the value was a textual class description. This function is trivial and reads the synset file line by line. In such a file, each line contains a number and a class description string, separated with the space character. The following code shows its definition:

using Classes = std::map<size_t, std::string>;
Classes ReadClasses(const std::string& file_name) {
Classes classes;
std::ifstream file(file_name);
if (file) {
std::string line;
std::string id;
std::string label;
std::string token;
size_t idx = 1;
while (std::getline(file, line)) {
std::stringstream line_stream(line);
size_t i = 0;
while (std::getline(line_stream, token, ' ')) {
switch (i) {
case 0:
id = token;
break;
case 1:
label = token;
break;
}
token.clear();
++i;
}
classes.insert({idx, label});
++idx;
}
}
return classes;
}

Notice that we used the std::getline function in the internal while loop to tokenize a single line string. We did this by specifying the third parameter that defines the delimiter character value.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.103.210