简体   繁体   English

map 字符串和向量,如何使用查找

[英]map string and vector, how to use find

I have to generate a vector of vectors starting from a word.我必须从一个词开始生成一个向量向量。

Hereafter I have put a minimal example, with one letter (although later I should use longer words), of what I need to do.此后,我用一个字母(尽管稍后我应该使用更长的词)来举一个最小的例子来说明我需要做什么。 Unfortunately, I don't understand how the function "find" should work for the map and I get a compiling error.不幸的是,我不明白 function“查找”应该如何为 map 工作,并且出现编译错误。 Any help, please?有什么帮助吗?

#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

vector <vector <double> > assign_mat(string &s, map<string, vector<double> > &amatr){
  vector <vector <double> > res;

  for(size_t i=0; i<s.size(); ++i){
    res.push_back(amatr.find(s[i])->second)  ;

  }

  return res;
}


int main(){

map<string, vector<double> > amm_vals =     {
{"S", {-0.228,1.399,-4.760,0.670,-2.647} },
{"T", {-0.032,0.326,2.213,0.908,1.313} },
{"V", {-1.337,-0.279,-0.544,1.242,-1.262} }
                            };

string s="V";
vector <vector <double> > ares;

ares=assign_mat(s, amm_vals);
return 0;
}

The error I get is the following:我得到的错误如下:

In function ' std::vector<std::vector<double> > assign_mat(std::__cxx11::string&, std::map<std::__cxx11::basic_string<char>, std::vector<double> >&) ':在 function ' std::vector<std::vector<double> > assign_mat(std::__cxx11::string&, std::map<std::__cxx11::basic_string<char>, std::vector<double> >&) ':
main_code.cpp:15:34: error: no matching function for call to ' std::map<std::__cxx11::basic_string<char>, std::vector<double> >::find(__gnu_cxx::__alloc_traits<std::allocator<char> >::value_type&) ' res.push_back(amatr.find(s[i])->second); main_code.cpp:15:34: 错误:没有匹配 function 调用 ' std::map<std::__cxx11::basic_string<char>, std::vector<double> >::find(__gnu_cxx::__alloc_traits<std::allocator<char> >::value_type&) ' res.push_back(amatr.find(s[i])->second);

I think you are trying to add all values from amm_vals which have keys which are equal to a single character of s ?我认为您正在尝试添加amm_vals中的所有值,这些值的键等于s的单个字符? If so, then you can convert s[i] to a string by concatenating it to an empty string.如果是这样,那么您可以通过将s[i]连接到一个空字符串来将其转换为一个字符串。

std::vector<std::vector<double>> assign_mat(const std::string &s, std::map<std::string, std::vector<double>> &amatr){
  std::vector<std::vector<double>> res;

    for ( size_t i = 0; i < s.size(); ++i ) {
        std::string _s = s[i] + std::string{}; // or std::string _s( 1, s[i] );
        auto _where = amatr.find( _s );
        if ( _where != amatr.end() )
            res.push_back( _where->second );
    }

  return res;
}

If you follow the recommendations of @SamVarshavchik your code should look like:如果您遵循@SamVarshavchik 的建议,您的代码应该如下所示:

 vector <vector <double> > assign_mat(string &s, map<string, vector<double> > &amatr){
     vector <vector <double> > res;
     for(size_t i=0; i<s.size(); ++i){
         if(amatr.find(string(1, s[i])) != amatr.end())
             res.push_back(amatr[string(1, s[i])]);
     }
     return res;
}

But I think it is better to use the following idea:但我认为最好使用以下想法:

vector <vector <double> > assign_mat(vector<string> &s, map<string, vector<double> > &amatr){
    vector <vector <double> > res;
    for(size_t i=0; i<s.size(); ++i){
        if(amatr.find(s[i]) != amatr.end())
            res.push_back(amatr[s[i]]);
    }
    return res;
}

And remember that the find in std library returns an iterator to the first element in the range [first,last) that compares equal to val.请记住,std 库中的find返回一个迭代器,该迭代器指向范围 [first,last) 中比较等于 val 的第一个元素。 If no such element is found, the function returns last.如果没有找到这样的元素,则 function 最后返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM