I have a function with the following declaration:
void Add(string_view source, string_view target);
What should it do: it adds a pair of words from string_views and saves them for the later usage:
- User types source word -> target word should be returned
- User types target word -> source word should be returned
I have a constraint, that I can have only 1 copy of each string. So my idea was to save string_views to maps (map<source string_view, target string_view> and the opposite) and these string_views should be views of the words, saved, for example in vector.
So the idea of void Add(string_view source, string_view target) is the following:
void Add(string_view source, string_view target){
- save source string and target string to the vector<string> words;
- create views from these words and save them into map<string_view source, string_view target>
- create views from these words and save them into map<string_view target, string_view source>
}
From my previous post [https://stackoverflow.com/questions/78230725/translator-exercise-by-using-string-view-c/78230782#78230782][1] I understood, that strings (inserted into the function) are temporary and they got destroyed after they are out of their scope.
My question is: how can I save strings from the string_view (efficiently), such that I can later use string_views on them
EDIT: Assignment
Develop a class called Translator that allows saving bilingual word pairs and then translating words from one language to another using the already added pairs:
class Translator {
public:
void Add(string_view source, string_view target);
string_view TranslateForward(string_view source) const;
string_view TranslateBackward(string_view target) const;;
private:
//
};
More specifically, an object t of type Translator should support the following actions:
Creation using the default constructor. In this case, the translation base is empty.
Adding a bilingual pair:
t.Add(source, target). After this call, it is assumed that the wordsourcein language 1 corresponds to the wordtargetin language 2. The strings passed to theAddmethod may be destroyed before thetobject.Translation from language 1 to language 2:
t.TranslateForward(source). If there was a previous callt.Add(source_copy, target)for some stringtargetand a stringsource_copyequal tosource(or being the same), it should returntarget. If there was no such call, it should return an empty string. If there were multiple such calls, it should returntargetfor the last one.Translation from language 2 to language 1:
t.TranslateBackward(target). If there was a previous callt.Add(source, target_copy)for some stringsourceand a stringtarget_copyequal totarget(or being the same), it should returnsource. If there was no such call, it should return an empty string. If there were multiple such calls, it should returnsourcefor the last one.
Constraints:
- Each string should be stored in the
Translatorclass instance no more than once. If this constraint is violated, you will receive a "Memory limit exceeded" error.
EDIT 2: Just for the record, it's not the university assignment, but Online Course (if it's important). [1]: Translator Exercise by using string_view (c++)
Read the constraint carefully.
You can use
std::stringwithinTranslator, and because the strings passed toAddmight not outlivet, you must.We'll use a vector of pairs, to hold each string only once. As the requirements don't specify complexity requirements, we can just linearly search in each direction.