Fabrizio
Fabrizio

Reputation: 939

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. 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> >&)’:
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) ;

Upvotes: 2

Views: 461

Answers (2)

Mandy007
Mandy007

Reputation: 441

If you follow the recommendations of @SamVarshavchik your code should look like:

 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. If no such element is found, the function returns last.

Upvotes: 2

ChrisMM
ChrisMM

Reputation: 10022

I think you are trying to add all values from amm_vals which have keys which are equal to a single character of s? If so, then you can convert s[i] to a string by concatenating it to an empty string.

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;
}

Upvotes: 2

Related Questions