std::map и алгоритмы STL.
| Код | #include <iostream> #include <map> #include <cstdlib> #include <ctime> #include <vector> #include <string> #include <algorithm> #include <sstream> #include <functional> #include <iterator>
typedef std::vector<std::string> string_vector; typedef std::map<std::string, string_vector> synonyms_map;
struct transformer : public std::binary_function<std::string, synonyms_map, std::string> { std::string operator () (const std::string& word, const synonyms_map& m) const { synonyms_map::const_iterator it = m.find(word); if (it != m.end()) { string_vector::const_iterator itv = it->second.begin(); return *(itv + rand() % it->second.size()); } return word; } };
int main() { srand(static_cast<unsigned int>(time(0)));
synonyms_map synonyms;
synonyms["Ser"].push_back("Mister"); synonyms["Ser"].push_back("Msye"); synonyms["Ser"].push_back("Tovarish"); synonyms["Ser"].push_back("Ey ty kozel"); synonyms["sobaka"].push_back("psina"); synonyms["sobaka"].push_back("suka"); synonyms["vasha"].push_back("tvoya");
std::string line = "Ser , vasha sobaka napisala na moyu obuv";
std::istringstream stream(line); std::transform( std::istream_iterator<std::string>(stream), std::istream_iterator<std::string>(), std::ostream_iterator<std::string>(std::cout, " "), std::bind2nd(transformer(), synonyms)); }
|
вот пример, дополняй, изменяй. |