C++ 如何在地图中查找字符
我需要使用map创建一个字典。我需要创建一个函数来显示以特定字符开头的每个单词。我创建了这个函数(start_with()),它可以工作,但是我想问一下,是否可以使用find()实现它,并且只查找和显示以该字符开头的单词C++ 如何在地图中查找字符,c++,C++,我需要使用map创建一个字典。我需要创建一个函数来显示以特定字符开头的每个单词。我创建了这个函数(start_with()),它可以工作,但是我想问一下,是否可以使用find()实现它,并且只查找和显示以该字符开头的单词 class Dictionary { std::map<std::string, std::string>p; public: Dictionary(std::string filename) { std::ifstream ifst
class Dictionary {
std::map<std::string, std::string>p;
public:
Dictionary(std::string filename) {
std::ifstream ifstream(filename, std::ios::app);
std::string map1, map2;
while (ifstream) {
ifstream >> map1>> map2;
p.insert(std::pair<std::string, std::string>(map1,map2));
}
}
void start_with(char c) {
std::string temp;
for (auto it = p.begin(); it != p.end(); it++) {
temp = it->first;
if (temp[0] == c) {
std::cout << it->first << " " << it->second << std::endl;
}
}
}
};
类字典{
std::mapp;
公众:
字典(标准::字符串文件名){
std::ifstream ifstream(文件名,std::ios::app);
std::字符串map1,map2;
while(ifstream){
ifstream>>map1>>map2;
p、 插入(std::pair(map1,map2));
}
}
以(字符c)开头的void start_{
std::字符串温度;
对于(自动it=p.begin();it!=p.end();it++){
温度=它->第一;
如果(温度[0]==c){
std::cout firststd::for_每个
都是解决方案:
std::map<std::string, std::string> map {{"a","a"}, {"b", "b"}, {"c", "b"}};
char ch = 'b';
std::for_each (map.begin(), map.end(),
[ch](auto e) {
if (e.first[0] == ch)
std::cout << e.first << " "
<< e.second << std::endl;
}
);
std::map map{{“a”,“a”},{“b”,“b”},{“c”,“b”};
char ch='b';
std::for_each(map.begin(),map.end(),
[ch](自动e){
如果(例如,第一个[0]==ch)
std::我能不能怀疑一个字符串的机会,并将在这方面派上用场。甚至