64,671
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <string>
#include <map>
#include <iostream>
#include <vector>
static const char* arr[][2] = {
{"brb", "be right back"},
{"k", "okey?"},
{"y", "why"},
{"r", "are"},
{"u", "you"},
{"pic", "picture"},
{"thk", "thanks!"},
{"18r", "later"},
};
int split(const std::string& src, const std::string &pattern, std::vector<std::string>& vct) {
if (src.empty()) {
return 0;
}
int start = 0;
int idx = src.find_first_of(pattern, start);
while (idx != std::string::npos) {
if (start != idx) {
std::string tmp = src.substr(start, (idx - start));
vct.push_back(tmp);
}
start = idx + 1;
idx = src.find_first_of(pattern, start);
}
if (!src.substr(start).empty()) {
vct.push_back(src.substr(start));
}
return 0;
}
int main()
{
std::string strSrc = "where r u y dont u send me a pic k thk 18r";
std::map<std::string, std::string> mapping;
int size = sizeof(arr) / sizeof(*arr);
for (int i = 0; i < size; i++) {
mapping.insert(std::make_pair(arr[i][0], arr[i][1]));
}
std::vector<std::string> words;
split(strSrc, " ", words);
std::string result = "";
for (int i = 0; i < words.size(); i++) {
std::string& tmp = words[i];
std::map<std::string, std::string>::iterator it = mapping.find(tmp);
if (it != mapping.end()) {
tmp = it->second;
}
result += tmp;
if (i != (words.size() - 1)) {
result += " ";
}
}
std::cout << result;
return 0;
}